Currently we use a static factory class to create our DAOs. This isn't a bad solution but we'd like to be able to use the built-in dependency injection features within AspNetCore. However, we have a few DAOs that utilize our OSS fault tolerance library: Mjolnir. These DAOs require that we createa a proxy class to wrap the method calls so that they use Mjolnirs internals. This is currently handled seemlessly by the DAOFactory, it can create simple concretes or these Mjolnir proxies for us by detecting if the dao interface is decorated with a specific attribute.
This code lets us change what gets injected in place of IDao
by changing the [InjectInstead]
attribute. This is just a simple POC, our actual implementation of this would differ slightly as we'd likely inject a Func
that would return the proxy class so we could defer creation until the instance is needed.
So while this solution requires us to implement a custom ServiceCollection
it shouldn't be messy at all to wire something like this up at startup.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var hudlServices = new HudlServiceCollection(); // potentially MjolnirAutoProxyServiceCollection, or something like that
services.forEach(hudlServices.Add);
// add other framework services
return hudlServices.BuildServiceProvider();
}