I am currently working on a IoC container abstraction that sits on top of StructureMap. The idea is that it could work with other IoC containers as well.
public interface IRegister
{
IRegister RegisterType(Type serviceType, Type implementationType, params Argument[] arguments);
}
public abstract class ContainerBase : IRegister
{
public abstract IRegister RegisterType(Type serviceType, Type implementationType, params Argument[] arguments);
}
public class StructureMapContainer : ContainerBase
{
public StructureMapContainer(IContainer container)
{
Container = container;
}
public IContainer Container { get; private set; }
public override IRegister RegisterType(Type serviceType, Type implementationType, params Argument[] arguments)
{
// StructureMap specific code
Container.Configure(x =>
{
var instance = x.For(serviceType).Use(implementationType);
arguments.ForEach(a => instance.CtorDependency<string>(a.Name).Is(a.Value));
});
return this;
}
}
public class Argument
{
public Argument(string name, string value)
{
Name = name;
Value = value;
}
public string Name { get; private set; }
public string Value { get; private set; }
}
I can execute this code by doing the following:
//IContainer is a StructureMap type
IContainer container = new Container();
StructureMapContainer sm = new StructureMapContainer(container);
sm.RegisterType(typeof(IRepository), typeof(Repository));
The problem arises when I try to pass in constructor arguments. StructureMap allows you to fluently chain however many CtorDependency calls as you want but requires the constructor parameter name, value and type. So I could do something like this:
sm.RegisterType(typeof(IRepository), typeof(Repository), new Argument("connectionString", "myConnection"), new Argument("timeOut", "360"));
This code works as CtorDependency is currently of type string and both arguments are strings as well.
instance.CtorDependency<string>(a.Name).Is(a.Value)
How can I pass in multiple constructor arguments BUT of any type to this method? Is it possible? Any help would be much appreciated.