I want to inject the specific dependencies using Unity OR any other DI container, based on the controller types.
Below is an example:
The 2 dependencies are of type interface IAppLogger
:
1. NetworkLogger : IAppLogger
2. FileLogger : IAppLogger
Now below 2 controllers consume the dependency with abstraction IAppLogger.
1. NetworkController depends on IAppLogger
2. FileController depends on IAppLogger
In my MVC project I have installed Unity and Unity.mvc3 packages. After that I got a bootstrap.cs file where I can register the types. Now how should I set the DependencyResolver to resolve
IAppLogger to NetworkLogger for controller NetworkController
AND
IAppLogger to FileLogger for controller FileController.
Edit:
If things are dynamic with App then chaning the dependency types inside each controller would be hard as against doing it in service locator or resolver and have less footprint. In standalone app I can do it using alias while registering types.
Container.RegisterType<IAppLogger, NetworkLogger>("Network");
Container.RegisterType<IAppLogger, FileLogger >("File");
IAppLogger flogger = Container.Resolve<IAppLogger>("File");
IAppLogger nlogger = Container.Resolve<IAppLogger>("Network");
If NetworkController
should always operate with a NetworkLogger
then it should depend on NetworkLogger
, not on IAppLogger
. Or maybe on INetworkLogger
if you want.
The same goes for FileController
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.