instantiates class using ninject ioc in .net - c#

I am using Ninject to do some IoC in my ASP.NET MVC application.
I have an interface "IService.cs" :
public interface IService
{
string method();
}
I have the corresponding implementation "Service.cs" :
public class Service
{
string method()
{
return "result";
}
}
I have done the binding in another class heriting from NinjectModule :
public class MyNinjectModule : NinjectModule
{
public override void Load()
{
RegisterServices();
}
private void RegisterServices()
{
Kernel.Bind<IService>().To<Service>();
}
}
I have my class A which use this service :
public class A
{
private readonly IService _service;
private int i;
public A(IService service, int i)
{
this._service=service;
this.i=i;
}
}
The problem is that now, I don't know how to instantiate my class A in my application. This is where am I stuck, how can I call Ninject
to tell my app to go get the implementation of my interface:
var myClass=new A(????)

The main problem is that your Service class does not implement IService.
public class Service
{
string method()
{
return "result";
}
}
It should be
public class Service : IService
{
public string method()
{
return "result";
}
}
But as for instantiating a class, the best approach is to use a composition root to build an object graph. In MVC, that is best handled by implementing IControllerFactory.
public class NinjectControllerFactory : DefaultControllerFactory
{
private readonly IKernel kernel;
public NinjectControllerFactory(IKernel kernel)
{
this.kernel = kernel;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null
? null
: (IController)this.kernel.Get(controllerType);
}
}
Usage
using System;
using Ninject;
using DI;
using DI.Ninject;
using DI.Ninject.Modules;
internal class CompositionRoot
{
public static void Compose()
{
// Create the DI container
var container = new StandardKernel();
// Setup configuration of DI
container.Load(new MyNinjectModule());
// Register our ControllerFactory with MVC
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(container));
}
}
In Application_Start, add:
CompositionRoot.Compose();
You will also need to create an interface for your class A and register it. An integer cannot be resolved automatically, you have to do that explicitly.
Kernel.Bind<IClassA>().To<A>()
.WithConstructorArgument("i", 12345);
And then you would add your dependency to a controller. Dependencies of dependencies are resolved automatically.
public class HomeController : Controller
{
private readonly IClassA classA;
public HomeController(IClassA classA)
{
if (classA == null)
throw new ArgumentNullException("classA");
this.classA = classA;
}
public ActionResult Index()
{
// Use this.classA here...
// IService will be automatically injected to it.
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
}

Related

How can I use dependency injection in asp.net core action filter?

I have a FilterAttribute that has two parameters, one defined in dependency injection and one defined on method of controller as as string
public controller : ControllerBase
{
[MyFilter("Parameter1", FromDependency)]
public ActionResult MyMethod()
{
....
}
}
and the filter
public MyFilter : Attribute
{
MyFilter(string parameter1, context fromDependency)
{
}
}
How can I inject the parameter from dependency injection?
You can implement an IFilterFactory for this purpose. The runtime checks for this interface when creating filters and calls the CreateInstance method that gets an IServiceProvider as a parameter. You can use this provider to create services and inject them into the filter.
The following sample is taken from the docs:
public class ResponseHeaderFilterFactory : Attribute, IFilterFactory
{
public bool IsReusable => false;
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) =>
new InternalResponseHeaderFilter();
private class InternalResponseHeaderFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context) =>
context.HttpContext.Response.Headers.Add(
nameof(OnActionExecuting), nameof(InternalResponseHeaderFilter));
public void OnActionExecuted(ActionExecutedContext context) { }
}
}
If you need to both use services from DI and values defined on the attribute, you can use the following approach:
public class ResponseHeaderFilterFactory : Attribute, IFilterFactory
{
private readonly string _attrParam;
public ResponseHeaderFilterFactory(string attrParam)
{
_attrParam = attrParam;
}
public bool IsReusable => false;
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
var svc = serviceProvider.GetRequiredService<IMyService>();
return new InternalResponseHeaderFilter(_attrParam, svc);
}
private class InternalResponseHeaderFilter : IActionFilter
{
private readonly string _attrParam;
private readonly IMyService _service;
public InternalResponseHeaderFilter(string attrParam, IMyService service)
{
_attrParam = attrParam;
_service = service;
}
public void OnActionExecuting(ActionExecutingContext context) =>
context.HttpContext.Response.Headers.Add(
nameof(OnActionExecuting), nameof(InternalResponseHeaderFilter));
public void OnActionExecuted(ActionExecutedContext context) { }
}
}
You can then apply the filter like this:
public controller : ControllerBase
{
[ResponseHeaderFilterFactory("Parameter1")]
public ActionResult MyMethod()
{
....
}
}
You can implement ActionFilterAttribute to get DI dependencies from HttpContext.RequestServices:
public sealed class MyAttr : ActionFilterAttribute
{
MyAttr(string parameter1)
{
}
public override void OnActionExecuting(ActionExecutingContext context)
{
//Get dependency from HttpContext services
var myDependency = context.HttpContext.RequestServices.GetService<MyDependency>();
//Use it
myDependency.DoSomething();
//....
}
}
Injecting components into action filter attributes directly is not possible but there are various workarounds to allow us to effectively accomplish the same thing. Using ServiceFilter is a relatively clean way to allow dependency injection into individual action filters.
The ServiceFilter attribute can be used at the action or controller level. Usage is very straightforward:
[ServiceFilter(typeof(MyFilter))]
And our filter:
public class MyFilter: IActionFilter
{
MyFilter(string parameter1, context fromDependency)
{
}
}
Obviously, as we are resolving our filter from the IoC container, we need to register it:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<MyFilter>(x =>
new Service(x.GetRequiredService<IOtherService>(),
"parameter1"));
...
}
more details in Paul Hiles article: here

The current type is an interface and could not be constructed. Are you missing type mapping?

I am trying to start a new web project and I am new to asp.net mvc. Everytime I debug the following code, the error appears stating
the current type, BBNepal.Repository.Interface.IUnitOfWork, is an interface and cannot be constructed.
Are you missing a type mapping?'
Where did I go wrong?
This is my controller
public class RegisterController : Controller
{
private IRegisterService registerService;
public RegisterController(IRegisterService _registerService)
{
registerService = _registerService;
}
// GET: Register
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Create(RegisterDTO registerDTO)
{
try
{
bool isSaved;
if (registerDTO.Id == 0)
isSaved = registerService.Create(registerDTO);
else
return View();
}
This is my service layer
public interface IRegisterService
{
bool Create(RegisterDTO registerDTO);
}
#endregion
#region Implementation
public class RegisterService : IRegisterService
{
private IUnitOfWork unitOfWork;
public RegisterService(IUnitOfWork _unitOfWork)
{
unitOfWork = _unitOfWork ?? new UnitOfWork();
}
public bool Create(RegisterDTO registerDTO)
{
Register register = registerDTO.Convert();
unitOfWork.RegisterRepository.Insert(register);
unitOfWork.Save();
return true;
}
}
This is my interfaced repository
public interface IUnitOfWork
{
#region Core Method
int Save();
Task<int> SaveAsync();
#endregion
IRepository<Register> RegisterRepository { get; }
}
This is my repository
private IRepository<Register> _registerRepository;
public IRepository<Register> RegisterRepository
{
get
{
return _registerRepository ?? (_registerRepository = new RepositoryBase<Register>(_context));
}
}
My DI registrations are:
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IRegisterService, RegisterService>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
You need to update the registrations, and include one for IUnitOfWork as it is passed into the constructor of class RegisterService.
It should be something like:
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IRegisterService, RegisterService>();
container.RegisterType<IUnitOfWork, UnitOfWork>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

Use Inversion of Control to construct dependency with caller

Given the following ASP.NET Core controller :
public class MyController : Controller {
public MyController(IDependency dependency) { this.dependency = dependency; }
}
public interface IDependency;
public class DependencyImplementation : IDependency {
public DependencyImplementation(Controller controller) { ... }
}
I want MyController to have a new instance of DependencyImplementation injected, constructed with the controller it's being passed to. Ideally using Ninject.
The non-IoC version would be:
public class MyController : Controller {
public MyController() { this.dependency = new DependencyImplementation(this); }
}
This would cause circular dependency. Only idea I have is to introduce factory:
public interface IDependencyFactory
{
IDependency Create(Controller controller);
}
public class MyController : Controller
{
private IDependency dependency;
public MyController(IDependencyFactory dependencyFactory)
{
this.dependency = dependencyFactory.Create(this);
}
}
var kernel = new StandardKernel();
kernel.Bind<Controller>().To<MyController>();
kernel.Bind<IDependency>().To<DependencyImplementation>();
kernel.Bind<IDependencyFactory>().ToFactory();
var controller = kernel.Get<Controller>();
Or maybe rather reconsider whole design.

Ninject .Net 3.5 SOAP Webservice issues

this is my first time posting up here and trust me I have searched high and low for an answer to my question but have had very little success.
Background: I have currently started trying to re-factor our existing SOAP web service (.Net 3.5) in order to do some IOC and DI using Ninject. I have a CacheManager which I am trying to initalize in the web method, however the injection does not seem to kick in.
I have an console application that calls the webservice with the below:
static void Main(string[] args)
{
TestService service = new CachingService.TestService();
DataResult result = service.GetSomething(1);
}
The webservice is below: TestService.asmx.cs
[WebMethod(Description = "Get something")]
public DataResult GetSomething(int param)
{
try
{
return this.CacheManager.Get();
}
catch (Exception ex)
{
throw;
}
}
Base.cs (TestService.asmx.cs inherits Base to initialize CacheManager)
public class Base
{
[Inject]
public ICacheManager CacheManager
{
get
{
if (cacheProxy == null)
{
cacheProxy = new CacheProxy();
}
return cacheProxy.CacheManager;
}
}
}
CacheProxy.cs
public class CacheProxy
{
[Inject]
public ICacheManager CacheManager { get; set; }
}
CacheManager.cs
public class CacheManager : ICacheManager
{
//implements iCacheManager methods
}
App_Start/NinjectWebCommon.cs
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ICacheManager>()
.ToMethod(x => x.Kernel.Get<ICacheManagerFactoryBuilder>().GetCacheManagerFactory().CreateCacheManager())
.InRequestScope();
}
CacheManagerFactoryBuilder.cs
public class CacheManagerFactoryBuilder : ICacheManagerFactoryBuilder
{
private ICacheManagerFactory _Factory;
public CacheManagerFactoryBuilder(ICacheManagerFactory factory)
{
_Factory = factory;
}
public ICacheManagerFactory GetCacheManagerFactory()
{
return _Factory;
}
}
CacheManagerFactory.cs
public class CacheManagerFactory : ICacheManagerFactory
{
private readonly ICacheManager Manager;
public CacheManagerFactory(ICacheManager manager)
{
if (this.Manager == null)
{
this.Manager = manager;
}
}
public ICacheManager CreateCacheManager()
{
return this.Manager;
}
}
Everytime I run the console application and it hits GetSomething, CacheManager is null. Why is it that the injection is not happening when I do a call to the web method?
Another member of the team eventually stumbled across this in another thread here:
How can I implement Ninject or DI on asp.net Web Forms?
All I was missing was inheriting WebServiceBase on my TestService web service class!

Registering Class to Ninject and injecting that class in Constructor

I am using NHibernate, and I have a BootStrapper class that has a method that returns an ISession. In my Repository class I am passing it an ISession Object. In my Controller, I am passing its constructor an IRepository object.
I have been successful in binding my IRepository to Repository class, but I can't figure out how to bind/register my Repository class so that it receives a ISession object from the BootStrapper class when it is instantiated and bind my controller to recieve a IRepository object when it is created.
My Code:
public interface IProductsRepository
{
IQueryable<Product> Products { get; }
void SaveProduct(Product product);
}
public class MySqlProductsRepository : IProductsRepository
{
private readonly ISession _session;
public MySqlProductsRepository(ISession session)
{
_session = session;
}
public IQueryable<Product> Products
{
get
{
return _session.CreateCriteria(typeof (Product)).List<Product>().AsQueryable();
}
}
public class BootStrapper
{
public static ISessionFactory SessionFactory = CreateSessionFactory();
private static ISessionFactory CreateSessionFactory()
{
var cfg = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nhibernate.config"));
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, System.Environment.MachineName);
NHibernateProfiler.Initialize();
return cfg.BuildSessionFactory();
}
public static ISession GetSession()
{
return SessionFactory.GetCurrentSession();
}
}
public class AdminController : Controller
{
private readonly IProductsRepository _productsRepository;
public AdminController(IProductsRepository productsRepository)
{
_productsRepository = productsRepository;
}
}
public class NinjectControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel = new StandardKernel(new DaisyBlossomsServices());
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return null;
return (IController) _kernel.Get(controllerType);
}
public class DaisyBlossomsServices : NinjectModule
{
public override void Load()
{
Bind<IProductsRepository>().To<MySqlProductsRepository>();
}
}
}
How do I bind a ISession to MySqlProductsRepository so it receives an ISession object for its constructor, and how do I bind it so my controller receives an IProductsRepository in its constructor?
you can bind ISession to a Provider. In this case you can change the BootStrapper class to inherit from Provider and implement the CreateInstance method.
That would look like that:
public class BootStrapper : Provider<ISession>
{
.....
protected override ISession CreateInstance(IContext context)
{
return GetSession();
}
....
}
After that just add the binding to your modul:
public class DaisyBlossomsServices : NinjectModule
{
public override void Load()
{
Bind<IProductsRepository>().To<MySqlProductsRepository>();
Bind<ISession>().ToProvider<BootStrapper>();
}
}
That should do it.

Categories

Resources