Implement passive attributes with dependencies that must be resolved by the DI container

I am trying to implement passive attributes in an ASP.NET web interface. The filter that I implement depends on the repository, which itself has a dependency on the custom DbContext. The message says that you can enable a component with a DI container, but also that the code should be called from Application_Start. I'm not sure how to implement this, taking advantage of the DI container lifecycle management capabilities (so that a new DbContext request is used for each request). Would an abstract factory injection be a good solution for this? or is there something simpler that I am missing.

+7
dependency-injection asp.net-web-api entity-framework
source share
1 answer

You can solve this problem by moving Decoraptor between the filter and the repository.

Without knowing much about your code, you should be able to define a Decoraptorepository using Abstract Factory:

public class Decoraptorepository : IRepository { private readonly IFactory<IRepository> factory; public Decoraptorepository(IFactory<IRepository> factory) { this.factory = factory; } // Just guessing IRepository member(s) here... public void Save(Foo foo) { this.factory.Create().Save(foo); } // other members... } 

This allows your filter to remain Singleton, while the actual repository is created in Transient mode.

If you also need to delete objects, see the next article on how to decompose Transient objects from within Decoraptor .

+7
source share

All Articles