I'm using MVC 5 which talks to BusinessServices (a CLASS library). I'm using Unity4 (basically Unity.Mvc5) to auto-create the BusinessLayer objects. I do not want to use Unity to manage the lifetime of DbContext, but do want to send the connectionString to the BusinessServices class so that the proper connection string is being passed from MVC to Class library.
I do not know how to pass the connectionString and other required variables (e.g logPath etc.) to my ClassLibrary using Unity. Any suggestions?
There are many possibilities to achieve this. You could just use an injection constructor like this:
class Service
{
Service(string connectionString, string logPath)
{
}
}
myContainer.RegisterType<Service>(
new InjectionConstructor("Data Source=ServerName;..", "/logs"));
Or you register an instance of your service:
myContainer.RegisterInstance<Service>(
new Service("Data Source=ServerName;..", "/logs"));