Asp.net core DI with context - c#

I'm trying implement a simple dependency (in ASP.NET Core) as this:
public partial class BaseController : Controller
{
public new ITempDataDictionary TempData { get; private set; }
public override void OnActionExecuting(ActionExecutingContext context)
{
base.OnActionExecuting(context);
//preparação da tempdata
this.TempData = new TempDataDictionary(HttpContext); //todo: DI?
this.TempData.Load();
}
}
}
The problem is the fact TempDataDictionary depends of HttpContext present in this controller.
How to implement that scenario in DI, since the ServiceLocator has no knowledge of HttpContext at Startup?
As this?
services.AddScoped(); //??????
But where i fill the constructor parameter HttpContext if this present just in controller?

You should create a service to handle your state data and add it as scoped.
public class AppStateService
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ITempDataProvider _tempDataProvider;
private IDictionary<string, object> _data;
public AppStateService(IHttpContextAccessor httpContextAccessor, ITempDataProvider tempDataProvider, UserManager<EntsogUser> userManager, CompanyRepository companyRepository)
{
_httpContextAccessor = httpContextAccessor;
_tempDataProvider = tempDataProvider;
_data = _tempDataProvider.LoadTempData(_httpContextAccessor.HttpContext);
}
private void SetValue(string name, object value)
{
_data[name] = value;
_tempDataProvider.SaveTempData(_httpContextAccessor.HttpContext,_data);
}
private object GetValue(string name)
{
if (!_data.ContainsKey(name))
return null;
return _data[name];
}
}
In Startup.cs (ConfigureServices)
services.AddScoped<AppStateService>();
In your controller
public class TestController : Controller
{
protected readonly CompanyRepository _companyRepository;
public TariffsController(AppStateService appStateService)
{
_appStateService = appStateService;
}
}

you can take a dependency on IHttpContextAccessor and register it with DI
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
then use it to get the HttpContext
However in a controller you have direct access to HttpContext so it isn't clear to me why you would want to inject it there

Related

Create singleton when private constructor have parameters

I would like to implement singleton pattern in StudentProvider and then access method through interface. StudentProvider constructor accepts few parameters. Here's the sample working code without singleton.
public interface IStudentProvider
{
Task<StudentViewModel> GetStudentAsync();
}
public class StudentProvider : IStudentProvider
{
private readonly HttpContext httpContext;
private readonly IActionContextAccessor actionContextAccessor;
private readonly IConfiguration configuration;
private readonly IUnitOfWork unitOfWork;
private readonly string host;
public StudentProvider(IHttpContextAccessor _httpContextAccessor, IActionContextAccessor _actionContextAccessor, IConfiguration _configuration, IUnitOfWork _unitOfWork)
{
httpContext = _httpContextAccessor.HttpContext;
actionContextAccessor = _actionContextAccessor;
configuration = _configuration;
unitOfWork = _unitOfWork;
host = _httpContextAccessor.HttpContext.Request.Host.Host;
}
public async Task<StudentViewModel> GetStudentAsync()
{
var std = new StudentViewModel();
// httpContext, actionContextAccessor, configuration, unitOfWork and host uses here
return std;
}
}
Now i converted this into single, here's the code:
public interface IStudentProvider
{
Task<StudentViewModel> GetStudentAsync();
}
public sealed class StudentProvider : IStudentProvider
{
private readonly HttpContext httpContext;
private readonly IActionContextAccessor actionContextAccessor;
private readonly IConfiguration configuration;
private readonly IUnitOfWork unitOfWork;
private readonly string host;
private static StudentProvider instance = null;
public static StudentProvider GetInstance
{
get
{
if (instance == null)
{
instance = new StudentProvider();
}
return instance;
}
}
private StudentProvider(IHttpContextAccessor _httpContextAccessor, IActionContextAccessor _actionContextAccessor, IConfiguration _configuration, IUnitOfWork _unitOfWork)
{
httpContext = _httpContextAccessor.HttpContext;
actionContextAccessor = _actionContextAccessor;
configuration = _configuration;
unitOfWork = _unitOfWork;
host = _httpContextAccessor.HttpContext.Request.Host.Host;
}
public async Task<StudentViewModel> GetStudentAsync()
{
var std = new StudentViewModel();
// httpContext, actionContextAccessor, configuration, unitOfWork and host uses here
return std;
}
}
The issue with above singleton code is instance = new StudentProvider(); is expecting parameters which i'm not able to pass.
How do i pass parameters to constructor from singleton instance ?
It seems that you're using ASP.NET and it's dependency injection. If so, you can use AddSingleton to register your provider instead of implementing your own singleton pattern. Singleton.
BTW, your provider depends on a HttpContext which means you need to create different instance for different requests.
As #Jon Skeet suggested, it will be better to use Dependency Injection.
I will also recommend to #Xiaofeng Zheng solution to use the singleton dependency injection with factory pattern.
And if all these does not satisfy, you can go with below solution.
You will need to keep the reference of IServiceProvider as singleton in your Startup file which can be accessed globally.
public class Startup
{
public static IServiceProvider ServiceProvider { get; private set; }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider) {
...
ServiceProvider = serviceProvider;
}
}
Then, you can access the Startup.ServiceProvider within your StudentProvider to create the instance of other dependencies.
using Microsoft.Extensions.DependencyInjection;
public sealed class StudentProvider : IStudentProvider
{
private readonly HttpContext httpContext;
private readonly IActionContextAccessor actionContextAccessor;
private readonly IConfiguration configuration;
private readonly IUnitOfWork unitOfWork;
private readonly string host;
private static StudentProvider instance = null;
public static StudentProvider GetInstance
{
get
{
if (instance == null)
{
instance = new StudentProvider(
Startup.ServiceProvider.GetService<IHttpContextAccessor>(),
Startup.ServiceProvider.GetService<IActionContextAccessor>(),
Startup.ServiceProvider.GetService<IConfiguration>(),
Startup.ServiceProvider.GetService<IUnitOfWork>()
);
}
return instance;
}
}
private StudentProvider(IHttpContextAccessor _httpContextAccessor, IActionContextAccessor _actionContextAccessor, IConfiguration _configuration, IUnitOfWork _unitOfWork)
}

Initialize object without pushing parameters

I have class with constructor for logging and for access to config:
public class SendEmaiServiceProvider
{
private readonly IConfiguration _config;
private readonly IWebHostEnvironment _env;
private readonly ILogger<SendEmaiServiceProvider> _logger;
private readonly string _fromEmailAddress;
public SendEmaiServiceProvider(IConfiguration config, IWebHostEnvironment env, ILogger<SendEmaiServiceProvider> logger)
{
_config = config;
_env = env;
_logger = logger;
_fromEmailAddress = _config.GetValue<string>("AppSettings:Email:FromEmailAddress");
}
public void SayHi()
{
Console.WriteLine("Hi");
}
}
The question is - How to call method SayHi from another class without pushing logger, env and config?
No I initialize new object with parameters, but I sure that it is wrong:
var sendEmaiServiceProvider = new SendEmaiServiceProvider(_config, _env, _logger);
sendEmaiServiceProvider.SayHi();
I can create an empty constructor but I will not have _fromEmailAddress value.
Looks like this is a netcore website. Assuming so, then:
Create an interface for the dependency.
Register the dependency in Startup.cs
Request the dependency as needed from the netcore DI.
public interface ISendEmaiServiceProvider
{
void SayHi()
}
public class SendEmaiServiceProvider : ISendEmaiServiceProvider
{
public void SayHi() { }
}
Then in Startup.cs:
public void ConfigureServices( IServiceCollection services )
{
services.AddScoped<ISendEmaiServiceProvider, SendEmaiServiceProvider>();
}
Then in the Controller (or wherever else DI is used), request it in the .ctor and all the dependencies for SendEmaiServiceProvider will be filled automatically by DI.
public class HomeController : Controller
{
public readonly ISendEmaiServiceProvider _emailService;
public HomeController( ISendEmaiServiceProvider emailService )
{
_emailService = emailService
}
}
That should get you going.
You should use dependency injection here. Better you create an interface here and resolve your 'SendEmaiServiceProvider' on the startup. And then use the interface instead of creating a new instance for SayHi() method.
public interface YourInterface
{
void SayHi()
}
public class SendEmaiServiceProvider : YourInterface
{
public void SayHi()
{
//your code
}
}
On your startup,
public void ConfigureServices( IServiceCollection services )
{
services.AddScoped<YourInterface, SendEmaiServiceProvider>();
}
On your controller/service,
public class YourController : Controller
{
public readonly YourInterface _emailSenderService;
public HomeController( YourInterface emailSenderService )
{
_emailSenderService = emailSenderService
}
public IActionResult SayHI()
{
_emailSenderService.SayHi()
}
}

C# Asp net core How to call database context outside of controllers

I have a question related to the use of database contexts outside the controller, namely, how to call the database context in a regular class?
To communicate with the database, I use: EF Core
I used options like this:
private readonly MSSQLContext _context;
public BookingController(MSSQLContext context)
{
_context = context;
}
Alternative
using (MSSQLContext context=new MSSQLContext())
{
context.get_Users.ToList();
}
Startup.cs
services.AddDbContext<MSSQLContext>(options =>
options.UseSqlServer(connection));
MSSQLContext.cs
public MSSQLContext()
{
}
public MSSQLContext(DbContextOptions<MSSQLContext> options)
: base(options)
{
}
public DbSet<VIVT_Core_Aud.Models.Core.Logger_Model> Logger_Models { get; set; }
and more tables...
Inject the context into whatever class you need to call into and register that class with the DI framework in your startup class.
For instance,
services.AddTransient<YourType>();
class YourType
{
public YourType(YourDbContext context) { ... }
}
You need to inject context using DI(dependency injection). I am showing you an example of repository pattern. You can search for "Repository pattern EF Core C# examples" will give you lots of examples or blogs to follow. Check here and here
Check out below example..
MyRepository.cs
namespace MyApp.Data.Services
{
public class MyRepository: IMyRepository, IDisposable
{
private MyAppContext _context;
private readonly ILogger<MyRepository> _logger;
public MyRepository(MyAppContext context, ILogger<MyRepository> logger)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_logger = logger;
}
public IEnumerable<MyTable> GetMyTableData()
{
return _context.MyTable.ToList();
}
}
}
IMyRepository.cs
namespace MyApp.Data.Services
{
public interface IMyRepository
{
//Interface implementation
IEnumerable<MyTable> GetMyTableData();
}
}
Startup.cs of MVC project
services.AddDbContext<MyAppContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("AppDbConnString")));
//Scope of repository should be based on your project used, I recommend to check lifetime & scope based your project needs.
services.AddScoped<IMyRepository, MyRepository>();
Mycontroller.cs
using MyApp.Data.Services;
namespace MyApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IMyRepository _myRepository;
public HomeController(ILogger<HomeController> logger, IMyRepository myRepository)
{
_logger = logger;
_myRepository = myRepository ??
throw new ArgumentNullException(nameof(myRepository));
}
public IActionResult Index()
{
return View();
}
public IActionResult GetAllData()
{
var result = _myRepository.GetMyTableData();
return Json(result);
}
}
}

ASP.Net 5 (MVC 6) - Accessing session object in view via a wrapper

I have a class
public class SessionService : ISessionService
{
public const string SessionUserKey = "UserViewModel";
private readonly IHttpContextAccessor _httpContextAccessor;
private ISession _session => _httpContextAccessor.HttpContext.Session;
public SessionService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public UserViewModel LoggedInUser
{
get
{
return _session.GetObjectFromJson<UserViewModel>(SessionUserKey);
}
set
{
_session.SetObjectAsJson(SessionUserKey, value);
}
}
public void Destroy()
{
_session.Clear();
}
}
An instance of this class is injected to my controller like this:
Startup.cs
services.AddScoped<ISessionService, SessionService>();
AccountController.cs
public class AccountController : Controller
{
private readonly IUserAccountService _accountService;
private readonly ISessionService _sessionService;
public AccountController(IUserAccountService accountService, ISessionService sessionService)
{
_accountService = accountService;
_sessionService = sessionService;
}
}
However, in my view I want to access the currently logged in user like this:
#if(SomeClass.LoggedInUser != null){
// code removed for brevity
}
How can I achieve this without making the LoggedInUser property of my SessionService class static. It seems like something needs to me made static to have an access like this. But I was wondering if there was a better way to accomplish this. And no, I don't want to use the ViewBag.
I am not using ASP.Net Identity
You should use #inject if you want to inject services in your views using DI:
#inject WebProject.Services.ISessionService CurrentSessionService
#if (CurrentSessionService.LoggedInUser != null)
{
// ...
}

How to create instance of a class and inject services?

New aspnet has in-build dependency injection: Startup class instance receives services, controllers, view components do. Is there any way for me to create object of my class and pass services using aspnet functionality? Something like:
WebApplicationClassesActivator.Create(typeof(MyClass))
where MyClass contains constructor receiving IHostingEnvironment instance, for example.
Real usage:
public class MyController : Controller
{
private readonly IContext Context;
public MyController(IContext context)
{
Context = context;
}
public IActionResult Index(string className)
{
return View(WebApplicationClassesActivator.Create(Type.GetType(className)));
}
}
Where classsName is name of classes like:
public class A
{
public A(IContext context, IHostingEnvironment env)
{
...
}
}
Assembly Microsoft.Extensions.DependencyInjection.Abstractions contains static class ActivatorUtilities which has what I need:
public static object CreateInstance(IServiceProvider provider,
Type instanceType,
params object[] parameters);
and I can create instance and inject services:
private readonly IServiceProvider Provider;
public HomeController(IServiceProvider provider)
{
Provider = provider;
}
public IActionResult Index()
{
var instance = ActivatorUtilities.CreateInstance(Provider, typeof(A));
return View(instance);
}
public class A
{
public A(IContext context)
{
}
}
Well, you can do it in the following manner:
public class MyController : Controller
{
private readonly IContext Context;
private readonly IServiceProvider _Provider;
public MyController(IContext context, IServiceProvider provider)
{
Context = context;
_Provider = provider;
}
public IActionResult Index(string className)
{
return View(_Provider.GetService(Type.GetType(className)));
}
}
Surely each class you're planning to instantiate in such a way should be added in ConfigureServices something like:
services.AddTransient<MyClass, MyClass>();
Also notice - this implementation in fact service locator, which is frequently considered as design antipattern.

Categories

Resources