Resolve dependency based on configuration - dependency-injection

In my windows service application I need to resolve components using configuration when service is starting. I use Castle Windsor as my IoC container.
Application looks like:
public class RootComponent : IRootComponent {
public RootComponent (IDataProvider1 provider1, IDataProvider2 provider2)
{
this.provider1 = provider1;
this.provider2 = provider2;
}
...
}
public class DataProvider1 : IDataProvider1
{
...
public DataProvider1 (IDbHelper dbHelper)
{
this.dbHelper = dbHelper;
}
...
}
public class DataProvider2 : IDataProvider2
{
...
public DataProvider1 (IDbHelper dbHelper)
{
this.dbHelper = dbHelper;
}
...
}
public interface IDbHelper
{
IDbConnection GetNewConnection();
DbParameter CreateDbParameter(string paramName, object paramValue);
string GetCommandString(string commandName);
}
public class MsSqlDbHelper : IDbHelper
{
...
public MsSqlDbHelper(string connectionString)
{
this.connectionString = connectionString;
}
...
}
public class PostgreDbHelper : IDbHelper
{
...
public PostgreDbHelper(string connectionString)
{
this.connectionString = connectionString;
}
...
}
I would like to register dependencies in service constructor and in OnStart method read the configuration and resolve correct IDbHelper based on it:
public partial class MyWindowsService : ServiceBase
{
public MyWindowsService()
{
InitializeComponent();
this.container = new WindsorContainer();
RegisterDependencies();
}
private void RegisterDependencies()
{
container.Register(
Classes.FromAssemblyInThisApplication().BasedOn<IRootComponent>().WithServiceFromInterface(),
Classes.FromAssemblyInThisApplication().BasedOn<IDataProvider1>().WithServiceFromInterface(),
Classes.FromAssemblyInThisApplication().BasedOn<IDataProvider2>().WithServiceFromInterface(),
Classes.FromAssemblyInThisApplication().BasedOn<IDbHelper>().WithServiceFromInterface()
.ConfigureFor<MsSqlDbHelper>(
registration => {
registration.DependsOn(Dependency.OnValue<string>(ConnectStringProvider.GetConnectionString("connectString1")));
registration.Named("msSql");
})
.ConfigureFor<PostgreDbHelper>(
registration => {
registration.DependsOn(Dependency.OnValue<string>(ConnectStringProvider.GetConnectionString("connectString2")));
registration.Named("postgreSql");
}));
}
protected override void OnStart(string[] args)
{
ResolveDependencies();
}
private void ResolveDependencies()
{
// Helper properties contain "msSql" or "postgreSql" value
root = container.Resolve<IRootComponent>(Config.Provider1Helper, Config.Provider2Helper);
}
...
}
I see three options how to resolve configured IDbHepler:
Typed factory facility
Child containers
Implement IHandlerSelector
What is the best way and why?

Solution is simpler than I expected. DynamicParameters is what I neeed:
private void RegisterDependencies()
{
container.Register(
Classes.FromAssemblyInThisApplication().BasedOn<IRootComponent>().WithServiceFromInterface(),
Classes.FromAssemblyInThisApplication().BasedOn<IDataProvider1>().WithServiceFromInterface()
.Configure(registration => registration.DynamicParameters((kernel, parameters) =>
{
IDbHelper dbHelper = kernel.Resolve<IDbHelper>(Config.Provider1Helper);
parameters.Add("dbHelper", dbHelper);
return k => k.ReleaseComponent(dbHelper);
})),
Classes.FromAssemblyInThisApplication().BasedOn<IDataProvider2>().WithServiceFromInterface(),
.Configure(registration => registration.DynamicParameters((kernel, parameters) =>
{
IDbHelper dbHelper = kernel.Resolve<IDbHelper>(Config.Provider2Helper);
parameters.Add("dbHelper", dbHelper);
return k => k.ReleaseComponent(dbHelper);
})),
Classes.FromAssemblyInThisApplication().BasedOn<IDbHelper>().WithServiceFromInterface()
.ConfigureFor<MsSqlDbHelper>(
registration => {
registration.DependsOn(Dependency.OnValue<string>(ConnectStringProvider.GetConnectionString("connectString1")));
registration.Named("msSql");
})
.ConfigureFor<PostgreDbHelper>(
registration => {
registration.DependsOn(Dependency.OnValue<string>(ConnectStringProvider.GetConnectionString("connectString2")));
registration.Named("postgreSql");
}));
}
protected override void OnStart(string[] args)
{
// Helper properties should contain "msSql" or "postgreSql" value
// take configuration from arguments
Config.Provider1Helper = UseMsSql(args, 0) ? "msSql" : "postgreSql";
Config.Provider2Helper = UseMsSql(args, 1) ? "msSql" : "postgreSql";
ResolveDependencies();
}
private void ResolveDependencies()
{
root = container.Resolve<IRootComponent>();
}
private bool UseMsSql(string[] args, int argNumber)
{
...
}

Related

Interface with more implementation and Dependency Injection

I created a project with .net Core 2.
Now I have a List of classes from the same interface which I needed at runtime.
My problem is, I can't add this classes to the servicecollection (only one interface). So I don't have access to the other services in those classes. Also I think it wouldn't solve it.
I could create a singleton/static class with my servicecollection and use the IServiceProvider to get those other services from there, but I think that isn't the best practice.
Here is an example of my problem:
public class Manager : IManager
{
private IList<IMyService> _myService;
public Manager()
{
IList<Type> types = GetIMyServiceTypes();
foreach (Type type in types)
{
var instance = (IMyService)Activator.CreateInstance(type);
_myService.Add(instance)
}
}
public IList<bool> IsTrue()
{
return _myService
.Select(se => se.IsTrue())
.ToList();
}
public IList<Type> GetIMyServiceTypes()
{
var type = typeof(IMyService);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p))
.ToList();
return types;
}
}
public class ServiceType1: IMyService
{
public bool IsTrue()
{
//Need Access to IServiceCollection Services
}
}
public interface IMyService
{
bool IsTrue();
}
public class MyController : Controller
{
private IManager _amanager;
public MyController(IManager manager)
{
_manager = manager
}
public IActionResult IsTrue()
{
IList<bool> isTrue =_manager.IsTrue();
return new ObjectResult(isTrue);
}
}
Is there a pattern, which I could use to solve my problem? Is there a best practice to have access to the services without using them in the constructor?
I found the solution on another post in stackoverflow https://stackoverflow.com/a/44177920/5835745
But I will post my changes for other people with the same problem. I loaded the list of classes from the configuration, but it's also possible to add all classes.
public class Manager : IManager
{
private IList<IMyService> _myService;
private readonly Func<string, IService> _serviceAccessor;
public Manager (Func<string, IService> serviceAccessor)
{
IList<string> authentications = new List<string> {"value1", "value2"}
foreach (string authentication in authentications)
{
AddAuthentication(_serviceAccessor(authentication));
}
}
public IList<bool> IsTrue()
{
return _myService
.Select(se => se.IsTrue())
.ToList();
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<Value1>();
services.AddTransient<Value2>();
services.AddTransient(factory =>
{
Func<string, IService> accesor = key =>
{
switch (key)
{
case "value1":
return factory.GetService<Value1>();
case "value2":
return factory.GetService<Value2>();
default:
throw new KeyNotFoundException();
}
};
return accesor;
});
}
}

StructureMap - DI - Multiple Concrete Implementation

I have referred multiple threads for solution but those did not help :( Any help to provide solution in terms of code on below problem is appreciated
class Program
{
static void Main(string[] args)
{
//StructureMapConfiguration();
var registry = new Registry();
registry.IncludeRegistry<DependencyRegistry>();
var container = new Container(registry);
var depend = container.GetInstance<ITest>();
var controller1 = new Controller1(depend);
controller1.M1();
var controller2 = new Controller2(depend);
controller1.M1();
Console.Read();
}
}
public interface ITest
{
void Method();
}
public class A : ITest
{
public void Method()
{
Console.WriteLine("A");
}
}
public class B : ITest
{
public void Method()
{
Console.WriteLine("B");
}
}
public class C : ITest
{
public void Method()
{
Console.WriteLine("C");
}
}
public interface IController
{
void M1();
}
public class Controller1 : IController
{
private ITest _test;
public Controller1()
{
}
public Controller1(ITest test)
{
_test = test;
}
public void M1()
{
_test.Method();
}
}
public class Controller2 : IController
{
private ITest _test;
public Controller2(ITest test)
{
_test = test;
}
public void M1()
{
_test.Method();
}
}
public class DependencyRegistry : Registry
{
public DependencyRegistry()
{
For<ITest>().Use<A>().Named("A");
For<ITest>().Use<B>().Named("B");
For<ITest>().Use<C>().Named("C");
For<IController>().Add<Controller1>().Ctor<ITest>().Is(i => i.GetInstance<ITest>("A"));
For<IController>().Add<Controller2>().Ctor<ITest>().Is(i => i.GetInstance<ITest>("B"));
Scan(x =>
{
x.AssembliesFromApplicationBaseDirectory();
x.AddAllTypesOf<ITest>().NameBy(type => type.Name);
x.WithDefaultConventions();
});
}
}
}
Actual Result:
Everytime I am getting instance of class C for Controller1 and Controller2
Expected Result:
For Controller1, I need instance of class A and for Controller2, I need instance of class B
Thanks in advance

How do I use GetAll with Ninject so that one failure doesn't stop the other bindings from resolving?

Some of the provided bindings for the mutli injection may fail to resolve.
public List<IMyCommand> GetMyCommands()
{
//throws
return kernel.GetAll<IMyCommand>().ToList();
}
I want to still get all the successfully resolved objects, and ideally which ones failed. Is there a way to achieve this with Ninject?
Not out of the box.
But we can create some kind of hack/Workaround. Caution. I would rather implement some specific mechanism which handles my case explicitly than to involve Ninject in that.
But for the curiuous minded, here you go:
If you have a look at the implementation of IResolutionRoot.TryGet you'll see that all it does is catch ActivationException and return default(T) in that case.
We can create our own TryGetAll<T> which does the same, but not for the entire IRequest but rather for each binding separately. So here's how to do it:
public static class ResolutionRootExtensions
{
public static IEnumerable<T> TryGetAll<T>(this IResolutionRoot resolutionRoot)
{
var request = resolutionRoot.CreateRequest(
typeof(IFoo),
x => true,
Enumerable.Empty<IParameter>(),
true,
false);
IEnumerable results = resolutionRoot.Resolve(request);
IEnumerator enumerator = results.GetEnumerator();
while (MoveNextIgnoringActivationException(enumerator))
{
yield return (T)enumerator.Current;
}
}
private static bool MoveNextIgnoringActivationException(
IEnumerator enumerator)
{
while (true)
{
try
{
return enumerator.MoveNext();
}
catch (ActivationException)
{
}
}
}
}
I've tested it and it works:
public class Test
{
[Fact]
public void Foo()
{
var kernel = new StandardKernel();
kernel.Bind<IFoo>().To<FooA>();
kernel.Bind<IFoo>().To<FooWithDependencyD>();
kernel.Bind<IFoo>().To<FooB>();
kernel.Bind<IFoo>().To<FooC>();
kernel.Bind<IFoo>().To<FooWithDependencyE>();
kernel.TryGetAll<IFoo>().Should()
.HaveCount(3)
.And.Contain(x => x.GetType() == typeof(FooA))
.And.Contain(x => x.GetType() == typeof(FooB))
.And.Contain(x => x.GetType() == typeof(FooC));
}
}
public interface IFoo
{
}
class FooA : IFoo { }
class FooB : IFoo { }
class FooC : IFoo { }
class FooWithDependencyD : IFoo
{
private readonly IDependency _dependency;
public FooWithDependencyD(IDependency dependency)
{
_dependency = dependency;
}
}
class FooWithDependencyE : IFoo
{
private readonly IDependency _dependency;
public FooWithDependencyE(IDependency dependency)
{
_dependency = dependency;
}
}
internal interface IDependency
{
}

Unity.mvc3 - Dispose method is not being called for a disposable class being managed by HierarchicalLifetimeManager

Below is my code, and the issue is that the dispose method of my UnitOfWork class does not get called. For DI, I am using Unity v2.1.505 with Unity.Mvc3 v1.2 in Asp.net MVC3 Application
[assembly: PreApplicationStartMethod(typeof(Program), "Initialize")]
namespace Practice.DependencyResolution.Concrete
{
public class Program
{
private static IUnityContainer container;
public static void Initialize()
{
if (container == null) container = new UnityContainer();
string databaseSource = Settings.Default.DatabaseSource;
var dependencyMapperType = Type.GetType("Practice.DependencyResolution.Concrete." + databaseSource + "DependencyMapper", true);
var dependencyMapper = (IDependencyMapper)Activator.CreateInstance(dependencyMapperType);
var dependencyMapperContext = new DependencyMapperContext(dependencyMapper);
dependencyMapperContext.MapDependencies(container);
ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(container));
var locator = new UnityServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => locator);
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
}
internal class DependencyMapperContext
{
private IDependencyMapper dependencyMapper;
public DependencyMapperContext(IDependencyMapper dependencyMapper)
{
this.dependencyMapper = dependencyMapper;
}
public void MapDependencies(IUnityContainer container)
{
dependencyMapper.MapDependencies(container);
}
}
internal class AnyDependencyMapper : IDependencyMapper
{
public void MapDependencies(IUnityContainer container)
{
container.RegisterType<ISupplierRepository, SupplierRepository>();
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
}
}
public class UnitOfWork : IUnitOfWork
{
private readonly TransactionScope transactionScope;
private readonly ModelDataContext context;
private bool disposed = false;
public UnitOfWork()
{
transactionScope = new TransactionScope();
this.context = new ModelDataContext();
}
ModelDataContext IUnitOfWork.Context
{
get
{
Debug.WriteLine("context get called");
return context;
}
}
public void Commit()
{
if (disposed) throw new ObjectDisposedException("transactionScope");
transactionScope.Complete();
}
protected virtual void Dispose(bool disposing)
{
if (disposed == false)
{
if (disposing)
{
if (context != null)
{
context.Dispose();
}
if (transactionScope != null)
{
transactionScope.Dispose();
}
disposed = true;
}
}
}
public void Dispose()
{
Debug.WriteLine("Access dispose called");
if (HttpContext.Current != null && HttpContext.Current.Error != null)
{
//transaction transactionScope will be disposed automatically, do nothing
}
else
{
Commit();
}
Dispose(true);
GC.SuppressFinalize(this);
}
}
public class SupplierRepository : ISupplierRepository
{
private readonly IUnitOfWork unitOfWork;
private bool disposed = false;
public SupplierRepository(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public IList<SupplierItem> GetAll()
{
return unitOfWork.Context.SupplierItems.ToList();
}
public SupplierItem GetById(object id)
{
return unitOfWork.Context.SupplierItems.SingleOrDefault(a => a.SupplierID == (int)id);
}
public void Insert(SupplierItem entity)
{
unitOfWork.Context.SupplierItems.InsertOnSubmit(entity);
unitOfWork.Context.SubmitChanges();
}
public void Delete(object id)
{
var supplier = unitOfWork.Context.SupplierItems.SingleOrDefault(a => a.SupplierID == (int)id);
unitOfWork.Context.SupplierItems.DeleteOnSubmit(supplier);
unitOfWork.Context.SubmitChanges();
}
public void Delete(SupplierItem entityToDelete)
{
Delete(entityToDelete.SupplierID);
}
public void Update(SupplierItem entityToUpdate)
{
var supplier = unitOfWork.Context.SupplierItems.SingleOrDefault(a => a.SupplierID == entityToUpdate.SupplierID);
supplier.Address = entityToUpdate.Address;
supplier.City = entityToUpdate.City;
supplier.CompanyName = entityToUpdate.CompanyName;
supplier.ContactName = entityToUpdate.ContactName;
supplier.ContactTitle = entityToUpdate.ContactTitle;
supplier.Country = entityToUpdate.Country;
supplier.Fax = entityToUpdate.Fax;
supplier.HomePage = entityToUpdate.HomePage;
supplier.Phone = entityToUpdate.Phone;
supplier.PostalCode = entityToUpdate.PostalCode;
supplier.Region = entityToUpdate.Region;
unitOfWork.Context.SubmitChanges();
}
public SupplierItem GetDefault()
{
return new SupplierItem();
}
}
I am new to DI and Unity, thanks in advance.
I do read that you are using MVC 3. Nevertheless, if there is a possibility for you to update to MVC 4, then the new Unity 3 has support for MVC out of the box, and works with the HierarchicalLifetimeManager.
I am not familiar with the Unity.Mvc3 NuGet package (which is not supported by Microsoft) though.

Structuremap No parameterless constructor defined for this object

I'm a newbie to StructureMap, and I've been trying to fix that error for some time now.
Just can't figure out how to fix it and where am I doing wrong.
I even set up a template MVC4 site, with nothing in it and still getting that error.
Can someone please help me out ?
public static class IoC {
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IDbSession>().Use(() => MvcApplication.DbSession);
x.For<IDbService>().Use<DbService>();
});
return ObjectFactory.Container;
}
}
public class HomeController : Controller
{
protected readonly IDbService _dbService;
public HomeController(IDbService dbService)
{
_dbService = dbService;
}
...
}
public interface IDbSession : IDisposable
{
void Commit();
void Rollback();
}
public interface IDbService
{
StudentsService Students { get; }
CoursesService Courses { get; }
...
}
public class DbService : IDbService
{
private readonly IDbSession _dbSession;
public StudentsService Students { get; }
public CoursesService Courses { get; }
...
public DbService(IDbSession dbSession)
{
_dbSession = dbSession;
}
}
public class MvcApplication : System.Web.HttpApplication
{
private static readonly string _connectionString;
private static readonly IDbSessionFactory _dbSessionFactory;
public static IDbSession DbSession
{
get { return (IDbSession)HttpContext.Current.Items["Current.DbSession"]; }
private set { HttpContext.Current.Items["Current.DbSession"] = value; }
}
static MvcApplication()
{
_connectionString= ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
_dbSessionFactory = new DbSessionFactory(_connectionString);
}
protected MvcApplication()
{
BeginRequest += delegate
{
DbSession = _dbSessionFactory.Create();
};
EndRequest += delegate
{
if (DbSession != null)
DbSession.Dispose();
};
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
You must configure the dependency resolver to handle parameters in a Controller's constructor. You can find out here how: http://ardalis.com/How-Do-I-Use-StructureMap-with-ASP.NET-MVC-3

Resources