DbContextScope icon indicating copy to clipboard operation
DbContextScope copied to clipboard

IoC containers support

Open musukvl opened this issue 11 years ago • 8 comments

Simple Injector fails with exception: For the container to be able to create DbContextReadOnlyScope, it should contain exactly one public constructor, but it has 3.

musukvl avatar Jan 20 '15 13:01 musukvl

Thanks @musukvl.

The library was implemented with both DI and non-DI-based code bases in mind. In our case, our entire code base follows the Dependency Injection principle and our DbContextScopes are instantiated by StructureMap without any issue.

I have to say that I'm not familiar with Simple Injector. If, like the exception message suggests, it can only cope with a single public constructor, I would suggest switching to a more mature DI container as this sounds like a very serious limitation that will cause you a lot of problems.

That being said, I'd be really quite baffled if Simple Injector really didn't allow you to have more than one public constructor on your components. It's definitely not the job of a DI container to dictate how your components should be constructed. It's the other way around: you tell your DI container how to construct your components and the DI container does it for you.

So I would first check out whether you can configure Simple Injector to use a specific constructor. There's bound to be a way to do this.

mehdime avatar Jan 20 '15 13:01 mehdime

Re-opening in case other people here are familiar with Simple Injector and can provide the magic config code for this.

mehdime avatar Jan 20 '15 13:01 mehdime

The Simple Injector design philosophy views multiple constructors as an anti pattern and therefore they do not work out of the box. I believe there are some hooks to enable this though (see the docs).

I've just started using DbContextScope in a project with Simple Injector and haven't noticed this issue though. It could be because I am not explicitly registering the DbContextReadOnlyScope. I'm only registering IDbContextScopeFactory and IAmbientDbContextLocator.

container.Register<IAmbientDbContextLocator, AmbientDbContextLocator>(); container.Register<IDbContextScopeFactory, DbContextScopeFactory>();

You could also try:

container.Register<IDbReadOnlyContextScope>(() => new DbReadOnlyDbContextScope(container.GetIntance<type>, ...));

BTW - Thanks for sharing the DbContextScope solution. So far it seems to be solving the problems I was hoping it would (as described in your blog post).

andrewthauer avatar Jan 20 '15 14:01 andrewthauer

Thanks @andrewthauer for jumping in. Actually, your answer made me realize that the core issue here is probably not with Simple Injector.

@musukvl: If you use DbContextScope in a code base that follows the dependency injection principle, you shouldn't depend directly on the DbContextScope or DbContextReadOnlyScope classes.

You should instead take a dependency on IDbContextScopeFactory as @andrewthauer mentioned. You can then use that factory to instantiate your DbContextScope or DbContextReadOnlyScope as and when needed.

Give it a try, this should solve your issue.

If you're interested, this is the standard Abstract Factory pattern that you can use to manually control the creation of stateful components while still maintaining all the benefits of Dependency Injection. Mark Seeman has a collection of posts on the subject here: http://stackoverflow.com/questions/2280170/why-do-we-need-abstract-factory-design-pattern/2280289#2280289

mehdime avatar Jan 20 '15 14:01 mehdime

Looks like there is right way to use DbContextScope with SimpleInjector:

container.Register<IAmbientDbContextLocator, AmbientDbContextLocator>(); 
 container.Register<IDbContextScopeFactory>(() => new DbContextScopeFactory());

musukvl avatar Jan 22 '15 15:01 musukvl

@musukvl That looks about right to me. Thanks for giving us the right configuration for SimpleInjector.

mehdime avatar Jan 24 '15 16:01 mehdime

Hi mehdime & msukvl,

First, Thank you for providing this solution in github. I was trying to utilize this solution, but I'm running into a roadblock. I've added this in stackoverflow since this is technically not a bug in your code. http://stackoverflow.com/questions/31930179/using-dbcontextscopefactory-with-unity

For some reason, I'm not able to register the DbContextScopeFactory properly. It tried the suggestion above and still got errors. I also tried to instantiate a new DbContextScopeFactory when I register my IVendorRepository: container.RegisterType<IVendorRepository, VendorRepository>(new PerResolveLifetimeManager(), new InjectionConstructor(new DbContextScopeFactory()));

Thanks in advance for your help.

dasis avatar Aug 10 '15 22:08 dasis

was able to get this resolved. Thanks!

dasis avatar Aug 12 '15 00:08 dasis