How to inject different implementations to same interface without conditional/context DI? - c#

I have the following problem and I currently have a solution using conditional dependency injection. I have read that this is a bad idea, such as here in the SimpleInjector docs. I have read a large number of posts now and have seen various things suggesting using Strategy, Factory patterns, etc. What I am really looking for is some specifics - i.e. an example of some code - about how to solve without conditional injection. I need more that "use a factory". Here's a simplified version of my code. This is in an MVC web app, thus the controllers.
public abstract class QAControllerBase : Controller
{
protected readonly QABusinessLayer _blQA;
public QAControllerBase(QABusinessLayer bl)
{
_blQA = bl;
}
[HttpGet]
public ActionResult PPR(string accession, string site)
{
var m = _blQA.popPPRViewModel(accession);
return View(m);
}
}
public class QASouthController : QAControllerBase
{
public QASouthController([QASouthBinding] QABusinessLayer bl) : base(bl)
{
}
// some actions that are specific to South
}
public class QANorthController : QAControllerBase
{
public QANorthController([QANorthBinding] QABusinessLayer bl) : base(bl)
{
}
// some actions that are specific to North
}
public abstract class QABusinessLayer
{
protected readonly IFullBaseRepo _repo;
public QABusinessLayer(IFullBaseRepo repo)
{
_repo = repo;
}
public abstract PPRViewModel popPPRViewModel(string accession);
protected PPRViewModel DoSomeCommonStuff(PPRViewModel model)
{
...
return model;
}
}
public class SouthBusinessLayer: QABusinessLayer
{
public SouthBusinessLayer([QASouthBinding] IFullBaseRepo repo) : base(repo)
{
}
public override PPRViewModel popPPRViewModel(string accession)
{
var m = new PPRViewModel();
// do some stuff that is specific to South
DoSomeCommonStuff(m);
return m;
}
}
public class NorthBusinessLayer : QABusinessLayer
{
public NorthBusinessLayer([QANorthBinding] IFullBaseRepo repo) : base(repo)
{
}
public override PPRViewModel popPPRViewModel(string accession)
{
var m = new PPRViewModel();
// do some stuff that is specific to North
DoSomeCommonStuff(m);
return m;
}
}
and here is the Ninject binding code that is pertinent:
kernel.Bind<QABusinessLayer>()
.To<SouthBusinessLayer>()
.WhenTargetHas<QASouthBinding>()
.InRequestScope();
kernel.Bind<QABusinessLayer>()
.To<NorthBusinessLayer>()
.WhenTargetHas<QANorthBinding>()
.InRequestScope();
The QASouthBinding and QANorthBinding are just simple attributes. I am not asking for Ninject specific example. Any code sample as to how this might be handled without using conditional or context based injection as I am now.

Make your QABusinessLayer class abstract.
Change your startup configuration to:
kernel
.Bind<SouthBusinessLayer>()
.To<SouthBusinessLayer>()
.InRequestScope();
kernel
.Bind<NorthBusinessLayer>()
.To<NorthBusinessLayer>()
.InRequestScope();
Change your controller constructors to accept a concrete business layer type:
public class QANorthController : QAControllerBase
{
public QANorthController(NorthBusinessLayer businessLayer) : base(businessLayer)
{
}
}
public class QASouthController : QAControllerBase
{
public QASouthController(SouthBusinessLayer businessLayer) : base(businessLayer)
{
}
}
Few things:
If Ninject auto-binds concrete types to the same type, then you don't need to manually configure the dependencies during startup.
You may want to use an interface rather than just passing your concrete BusinessLayer type.

Related

How to define a public const string in a repository that implements an interface

I have a Controllerclass which makes use of a TodoRepositoryclass via DI that implements an interface ITodoRepository.
The interface:
public interface ITodoRepository
{
public bool ValidateTodo(Todo todo);
}
The repository class:
public class TodoRepository : ITodoRepository
{
public bool ValidateTodo(Todo todo)
{
//some validation
return false;
}
}
The Controller:
public TodoController : BaseController
{
private readonly ITodoRepository _todoRepository;
private const string INVALID_TODO_MESSAGE = "Invalid todo.";
public TodoController(ITodoRepository todoRepository)
{
_todoRepository = todoRepository;
}
public IActionResult Post(Todo todo)
{
if(!_todoRepository.ValidateTodo(todo))
{
return new JsonResult(INVALID_TODO_MESSAGE);
}
}
}
To be able to use INVALID_TODO_MESSAGE in every Controller that makes use of TodoRepository I want to move it to TodoRepository, but that doesn't seem to be possible. Some other solution is to create a readonly property in ITodoRepository, and implement it in TodoRepository, but I am not sure if that is the best way to go.
You could create a helper class, next to the interface:
public static class TodoConstants
{
public const string INVALID_TODO_MESSAGE = "Invalid todo.";
}
You can use it everywhere as TodoConstants.INVALID_TODO_MESSAGE.
DISCLAIMER: This is not a direct answer to the question, but rather a design suggestion for error handling in web applications.
I would implement such a validation using Exceptions. Typically you could throw business exceptions from your application layer (possibly including repository too). Then at the web layer (aka. controllers, filters, etc.) you could implement an ExceptionFilter to handle business exceptions, for example turning them into a special JsonResult.
This way you'll have a good separation of concerns and centralized error handling through the application.
See this documentation for ASP.NET WebApi (it's almost the same for MVC too) https://learn.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling
Some untested pseudo code would look like this.
public class TodoRepository : ITodoRepository
{
public bool ValidateTodo(Todo todo)
{
//some validation
throw new ValidationException("Invalid todo");
}
}
public class MyBusinessExceptionFilterAttribute : ExceptionFilterAttribute
{
public void OnException(ExceptionContext filterContext)
{
...
filterContext.Result = new JsonResult(filterContext.Exception.Message);
...
}
}
public static void ApplicationStartup() {
...
GlobalFilters.Filters.Add(new MyBusinessExceptionFilterAttribute());
...
}
public IActionResult Post(Todo todo)
{
// Just let the exception to be thrown in case of business errors
_todoRepository.ValidateTodo(todo);
}

.Net Core Dependency Injection inject out of constructor

I need to inject out of constructor, everything I declared in Setup.
Ho can I do it ? How can I inject services out of constructor ?
Something like Injector service in Angular 2.
INJECT SERVICES WITHOUT CONSTRUCTOR IN CONTROLLERS
something like this
public class ControllerBase : Controller
{
protected IRepository<Test> _test;
protected IRepository<Test1> _test1;
protected IRepository<Test2> _test2;
public ControllerBase(INJECTOR injector)
{
_test = injector.inject(IRepository<Test>);
_test1 = injector.inject(IRepository<Test1>);
_test2 = injector.inject(IRepository<Test2>);
}
}
public class SomeController : ControllerBase
{
public SomeController(INJECTOR injector)
: base(injector)
{
}
}
THANKS FOR ANSWER #Rick van den Bosch
FOR THOSE WHO STILL CAN'T GET WHAT I WANTED:
public class ControllerBase : Controller
{
protected IRepository<Test> _test;
protected IRepository<Test1> _test1;
protected IRepository<Test2> _test2;
public ControllerBase(IServiceProvider injector)
{
_test = injector.GetService<IRepository<Test>>();
_test1 = injector.GetService<IRepository<Test1>>();
_test2 = injector.GetService<IRepository<Test2>>();
}
}
public class SomeController : ControllerBase
{
public SomeController(IServiceProvider injector)
: base(injector)
{
//HERE I HAVE ALL 3 REPO NOW WITHOUT EXTRA LINES
}
}
public class SomeController1 : ControllerBase
{
public SomeController1(IServiceProvider injector)
: base(injector)
{
//HERE I HAVE ALL 3 REPO NOW WITHOUT EXTRA LINES
}
}
You can inject the service as a parameter to the action method. This is done by marking the parameter with the attribute [FromServices].
This looks something like this:
public IActionResult About([FromServices] IDateProvider dateProvider)
{
ViewData["Message"] = $"Current date is {dateProvider.CurrentDate}";
return View();
}
If you're looking for default services in a BaseController: you could go about that several ways:
1. Still use a constructor
This would look something like this:
public class HomeController : BaseController
{
public HomeController(IDateProvider dateProvider) : base(dateProvider)
{
}
}
and
public class BaseController
{
protected IDateProvider _dateProvider;
protected BaseController(IDateProvider dateProvider)
{
_dateProvider = dateProvider;
}
}
This way the IDateProvider is available to both the BaseController and all inheriting Controllers.
2. Resolve services manually
This way you resolve the service manually. This could be in the BaseController, and only when you need them (lazy). For more info, see this post.
For simplicity and readability I would probably choose the constructor one.

Advanced dependency injection in ASP.NET Core

I have following interfaces, abstract classes etc.
public interface IAggregateRootMapping<T> : IAggregateDefinition where T : AggregateRoot
{
IEnumerable<Expression<Func<T, object>>> IncludeDefinitions { get; }
}
public abstract class AggregateRootMapping<T> : IAggregateRootMapping<T> where T : AggregateRoot
{
public abstract IEnumerable<Expression<Func<T, object>>> IncludeDefinitions { get; }
}
public class OrderAggregateRootMapping : AggregateRootMapping<Order>
{
public override IEnumerable<Expression<Func<Order, object>>> IncludeDefinitions
{
get
{
return new Expression<Func<Order, object>>[] {
order => order.Supplier
};
}
}
}
I use those in another class like this:
public class Repository<TAggregateRoot> : IRepository<TAggregateRoot> where TAggregateRoot : AggregateRoot
{
private readonly AggregateRootMapping<TAggregateRoot> _aggregateRootMapping;
public Repository(AggregateRootMapping<TAggregateRoot> aggregateRootMapping)
{
_aggregateRootMapping = aggregateRootMapping;
}
Do something...
}
How do I use the dependency injection of ASP.NET Core so that on runtime the matching class is injected?
For example if the AggregateRoot type class is Order than for the Repository class the OrderAggregateRootMapping class should be injected.
How do I use the ServiceCollection in ConfigureServices of the Startup class in .NET Core to accomplish this?
The dependency injection that comes by default is very basic. If you want to start wiring up rules based on generics, you will need to use a different implementation.
But, what you're after is still possible if you're willing to code the dependencies one by one.
In your Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<AggregateRootMapping<Order>, OrderAggregateRootMapping>();
services.AddScoped<Repository<Order>>();
// Add framework services.
services.AddMvc();
}
And then you can use your Repository class by injecting it into a controller, for example.
In ValuesController.cs
[Route("api/[controller]")]
public class ValuesController : Controller
{
private Repository<Order> _repository;
public ValuesController(Repository<Order> repository)
{
_repository = repository;
}
}
ValuesController will then receive an instance of Repository<Order> which will have been created with a OrderAggregateRootMapping.

Get different interface in two controllers with Autofac

I want to use different interface in different controller.
public interface IMessenger {
Id {get; set;}
void Send();
}
I have two class implement two same interface.
public class SmsSender : IMessenger {
public Id {get; set;}
public void Send() {
//logic here
}
}
public class MailSender : IMessenger {
public Id {get; set;}
public void Send() {
//logic here
}
}
Two Controllers:
public class HomeController : Controller {
private readonly IMessenger _messenger;
public HomeController(IMessenger messenger) {
_messenger = messenger;
}
}
public class Home2Controller : Controller {
private readonly IMessenger _messenger;
public HomeController(IMessenger messenger) {
_messenger = messenger;
}
}
Autofaq setup:
builder.RegisterType<MailSender>().As<IMessenger>().InstancePerLifetimeScope();
builder.RegisterType<SmsSender>().As<IMessenger>().InstancePerLifetimeScope();
How can I get SmsSender in HomeController and MailSender in Home2Controller?
When you register your component you can tell Autofac which dependency to choose using the WithParameter method.
builder.RegisterType<Service>()
.As<IService>()
.WithParameter((pi, c) => pi.ParameterType == typeof(IDependency),
(pi, c) => new XDependency());
In order to avoid the new XDependency code and let Autofac create the dependency, you can resolve a named registration.
builder.RegisterType<XDependency>().Named<IDependency>("X");
builder.RegisterType<YDependency>().Named<IDependency>("Y");
and resolve it using c.ResolveNamed<IDependency>("X")
Another solution would be to let the component choose which dependency it wants using IIndex<TKey, TService>. To use this, you have to register your IDependency as named registrations and inject IIndex<String, IDependency>.
public class Service
{
public Service(IIndex<String, IDependency> dependencies)
{
this._dependency = dependencies["X"];
}
private readonly IDependency _dependency;
}
For more information you can have a look at the FAQ on the autofac documentation : How do I pick a service implementation by context
You cannot. Autofac won't be able to figure out the differences. Better to separate them into two different interfaces, inject the one you need into controllers. From the design point of view the dependency is clearer as well. If there is a need, both interfaces can implement IMessenger.

Bestpractices: StructureMap and ASP.NET MVC 2 - Setter Injection/Contructur Injection in an abstract base Controller

public abstract class ConventionController : Controller
{
public const int PageSize = 5;
public IMappingService MappingService { get; set;}
}
How do I set up StructureMap to get the Instance of IMappingService?
Edit:
With the help of Joshua Flanagan I now have the following code:
EmployeeController
public class EmployeeController : ConventionController
{
private readonly ITeamEmployeeRepository _teamEmployeeRepository;
public EmployeeController(ITeamEmployeeRepository teamEmployeeRepository)
{
_teamEmployeeRepository = teamEmployeeRepository;
}
public ActionResult Index(int page = 1)
{
// The IMappingService dependency is hidden in the AutoMappedHybridView method that is a part of the ConventionController, easy use in the controller
return AutoMappedHybridView<TeamEmployee, TeamEmployeeForm>(_teamEmployeeRepository.GetPagedEmployees(page, PageSize));
// With constructor injection I had to write this ...
// return new HybridViewResult<TSourceElement, TDestinationElement>(_mappingService, _teamEmployeeRepository.GetPagedEmployees(page, PageSize));
}
}
ConventionController
public abstract class ConventionController : Controller
{
public const int PageSize = 5;
// This property is inject via StructureMap
public IMappingService MappingService { get; private set; }
public HybridViewResult<TSourceElement, TDestinationElement> AutoMappedHybridView<TSourceElement,TDestinationElement>(PagedList<TSourceElement> pagedList, string viewNameForAjaxRequest)
{
return new HybridViewResult<TSourceElement, TDestinationElement>(MappingService, pagedList, viewNameForAjaxRequest);
}
public HybridViewResult<TSourceElement, TDestinationElement> AutoMappedHybridView<TSourceElement,TDestinationElement>(PagedList<TSourceElement> pagedList)
{
return new HybridViewResult<TSourceElement, TDestinationElement>(MappingService, pagedList);
}
public HybridViewResult<TSourceElement, TDestinationElement> AutoMappedHybridView<TSourceElement, TDestinationElement>(TSourceElement sourceElement)
{
return new HybridViewResult<TSourceElement, TDestinationElement>(MappingService, sourceElement);
}
public HybridViewResult<TSourceElement, TDestinationElement> AutoMappedHybridView<TSourceElement, TDestinationElement>(TSourceElement sourceElement, string viewNameForAjaxRequest)
{
return new HybridViewResult<TSourceElement, TDestinationElement>(MappingService, sourceElement, viewNameForAjaxRequest);
}
}
HybridViewResult
public class HybridViewResult<TSourceElement, TDestinationElement> : BaseHybridViewResult
{
public HybridViewResult(IMappingService mappingService, PagedList<TSourceElement> pagedList)
{
ViewModel = mappingService.MapToViewModelPagedList<TSourceElement, TDestinationElement>(pagedList);
}
public HybridViewResult(IMappingService mappingService, PagedList<TSourceElement> pagedList, string viewNameForAjaxRequest)
{
ViewNameForAjaxRequest = viewNameForAjaxRequest;
ViewModel = mappingService.MapToViewModelPagedList<TSourceElement, TDestinationElement>(pagedList);
}
public HybridViewResult(IMappingService mappingService, TSourceElement model)
{
ViewModel = mappingService.Map<TSourceElement, TDestinationElement>(model);
}
public HybridViewResult(IMappingService mappingService, TSourceElement model, string viewNameForAjaxRequest)
{
ViewNameForAjaxRequest = viewNameForAjaxRequest;
ViewModel = mappingService.Map<TSourceElement, TDestinationElement>(model);
}
}
As you can see the HybridViewResult needs the IMappingService dependency.
If I would use a constructur in the ConventionController I would "pollute" my EmployeeController (imho).
If EmployeeController would directly need the IMapping dependency I would use the constructor for injecting. But this would not be necessary, because there is already the IMapping property of the ConventionController. So as Darin Dimitrov said, this would violate the DI principle.
How do I refactor my code? Do I really have to use constructor injection?
Edit 2
How could I order StructureMap to create an instance of the HybridViewResult? If this would be possible the controllers would not need to know about a IMapping dependency. Is it at all possible to get an generic object (not boxed) from StructureMap?
Actually, no, SetAllProperties() does not work for abstract classes. This is a weakness in the Structuremap implementation. I'm sorry for you, as I am for myself, that Property Injection does not work for base abstract classes. For the base abstract classes, you will need to use constructor injection (which, contrary to all the hype around here, is not always the best way to go when satisfying dependencies).
I assume you are already getting your controllers out of StructureMap. If thats the case, you just need to add the SetAllProperties() call to your container configuration. SetAllProperties allows you to define the criteria for the properties that should be injected.

Categories

Resources