I have 2 storage (Azure) accounts lets call them src and dest and I have controllers that require access to both and I'm trying to work out how to register these 2 singletons conditionally.
This answer gave me some hope but I can't quite work it out, what I want to do is something like (appreciate RegisterSingletonConditional isn't a valid fn):
IBlobAccessClient src = new BlobAccessClient(srcConnectionString);
IBlobAccessClient dest = new BlobAccessClient(destConnectionString);
container.RegisterSingletonConditional<IBlobAccessClient>(
src,
c => c.Consumer.Target.Parameter.Name.Contains("src"));
container.RegisterSingletonConditional<IBlobAccessClient>(
dest,
c => c.Consumer.Target.Parameter.Name.Contains("dest"));
Any guidance appreciated.
There is a non-generic
RegisterConditional
overload that accepts aRegistration
object. You can wrap yourBlobAccessClient
instance in aRegistration
and pass it on toRegisterConditional
as shown here:If this is a common pattern, you can simplify your code a bit by defining a simple extension method as follows:
This allows you to reduce the previous configuration to the following:
Optionally, you can simplify the predicate by extracting this to a simple method as well:
This reduces the registration to the following:
I would suggest defining two new interfaces:
And then registering them both individually (in whatever way that SimpleInject supports).
Then in your consumers, inject in
IBlobAccessClientSource
and / orIBlobAccessClientDestination
as needed. This will ensure that there are two singleton registrations - one for each of the two interfaces.