How can you leverage the connection string property when initializing a registered type with the Funq.Container? ServiceStack shows how to include a connection string while registering a type with the IoC container (Funq.Container) but I cannot find any examples actually using that connection string. I'd like to use this on the client end to specify the base URI for my internal JsonClient.
The examples show this:
container.Register(new MyType(c.Resolve<IDependency>(), connectionString));
But none of the examples show anything ever happening with the connectionString
variable.
What that example is saying is the container will create an instance of MyType
for each request. When it creates an instance of MyType
, the dependency IDependency
will be passed as a parameter to the constructor of MyType
and so will the connectionString
.
Your example code needs an input parameter c
though:
container.Register(c => new MyType(c.Resolve<IDependency>(), connectionString));
The usage of the connectionString
is up to you. You can use it how you want in your object of MyType
. You may wish to expose it as a public property, so it can be accessed. Or use it to alongside you IDependency
. By way of an expanded example:
public class MyType
{
private IDependency _someDependency;
public string ConnectionString { get; private set }
public MyType(IDependency dependency, string connectionString)
{
_someDependency = dependency.SetConnectionString(connectionString); // Assumes this method exists.
ConnectionString = connectionString;
}
}
In your service when MyType
is autowired you now have access to the object.
public class MyService : Service
{
public MyType MyType { get; set; }
public void Get(SomeRequest request)
{
string connectionString = MyType.ConnectionString;
}
}
If you wish to resolve MyType
somewhere else you can use
MyType myType = HostContext.Container.Resolve<MyType>();
string connectionString = myType.ConnectionString;
I hope this clears up that all that is happening is the connectionString
is passed into the object constructor and you are free to use it as you require.