Resolving some parameters of an object using factory

Dec 12, 2013 at 4:19 AM
Hi All,
I am trying to resolve an object using some automatic constructor injected parameters and some parameters which I will supply at runtime, is this possible with Unity?
public class Service : IService
{
    ....
}

public class DatabaseConnector : IDatabaseConnector
{
    // like how the below magically works in AutoFac
    //public delegate DatabaseConnector Factory(string connectionString);

    public DatabaseConnector(IService service, string connectionString)
    {
    }
}
So in the above ctor of DatabaseConnector, I want IService to be auto resolved and connectionString to be passed in everytime.

Thanks,
Ruskin
Editor
Dec 14, 2013 at 6:03 AM
If you know the connection string at registration time you can use an injection constructor to register the specific connection string:
container
    .RegisterType<IService, Service>()
    .RegisterType<IDatabaseConnector, DatabaseConnector>(
        new InjectionConstructor(typeof(IService), 
            ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString));
If you don't know the connection string until resolve then you can use a ParameterOverride:
IDatabaseConnector dbConnector = container.Resolve<IDatabaseConnector>(
    new ParameterOverride("connectionString", "Database=MyDb;Server=.;Integrated Security=SSPI;")
    .OnType<DatabaseConnector>());
        }

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
[Support How-to](http://entlib.codeplex.com/wikipage?title=Support%20How-t
Dec 14, 2013 at 7:03 AM
Hi Randy,
Thanks for that option, I don't know the connection string at registration time and would like to create one as per users input for e.g.

With your second solution every place I need a IDatabaseConnector I'll have to inject the "container", I want to avoid that by all means possible. Can I not use the factory register patterns in Unity or some equivalent concept?

Regards,
Ruskin
Editor
Dec 14, 2013 at 7:36 AM
Edited Dec 14, 2013 at 7:37 AM
If you are using a composition root then all IDatabaseConnector classes should be injected into the object graph during resolve.

I'm not familiar with Autofac so maybe I'm not understanding how you want it to work.

One way would be to Inject a Func<string> (or create a factory class) that knows how to get the connection string (as in this contrived example):
    public class DatabaseConnector : IDatabaseConnector
    {
        public DatabaseConnector(IService service, Func<string> connectionString)
        {
            Console.WriteLine("Injected " + connectionString());
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();

            container
                .RegisterType<IService, Service>()
                .RegisterType<IDatabaseConnector, DatabaseConnector>(
                    new InjectionConstructor(typeof(IService), new Func<string>(GetConnectionString)));

            // Set connection string the first time
            IDatabaseConnector dbConnector = container.Resolve<IDatabaseConnector>();
            dbConnector = container.Resolve<IDatabaseConnector>();
        }
        public static string GetConnectionString()
        {
            Console.WriteLine("Enter Connection String: ");
            return Console.ReadLine();
        }
    }

If you want a more seamless integration then perhaps something similar to what is described here: http://stackoverflow.com/questions/13652359/unitys-automatic-abstract-factory.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to