1

What is the best way to register an EF ObjectContext for injection, using Unity?

I tried this:

var environment = new EnvironmentInformationProvider();
container.RegisterType<MyContext>(new MyContext(environment.ConnectionString));

But that results in this error:

Error   CS1503  Argument 2: cannot convert from 'MyContext' to 
'Microsoft.Practices.Unity.InjectionMember' 

And I have no idea how to solve that ... Plus I don't think that sets up instance lifetime.

So, with some digging I found this:

builder.RegisterType<MyContext>()
                  .As<IMyContext>()
                  .InstancePerLifetimeScope();

That is Autofac code. Switching to it isn't an option, because I'm working on an existing application (and Unity should be able to do the same).

Assuming the above is what I need, what is the Unity equivalent?

1 Answer 1

3

When registering a Type, you're not supposed to pass an instance of that type but (optionally) some data that would help Unity to instantiate that Type upon request (the connection-string in your case):

container.RegisterType<IMyContext, MyContext>(new InjectionConstructor(environment.ConnectionString));

Your attempt to instantiate the context yourself and register it, could have been compiled using:

container.RegisterInstance<IMyContext>(new MyContext(environment.ConnectionString));

But that would cause your context to behave effectively like a Singleton and in the case of an Entity Framework context, it's a known anti-pattern that will put you in lots of trouble.

Sign up to request clarification or add additional context in comments.

11 Comments

How can I extract an interface from MyContext? (MyContext is my EF ObjectContext)
If by "extract" you mean "resolve", you can do container.Resolve<IMyContext>() and that will build and return the registered type. But you would much better be with Dependency Injection instead. Do you know why are you using an IoC in the first place?
No I mean literally extract :) What I mean is, my MyContext inherits from ObjectContext, but no interface (which is standard EF implementation as generated from DB). So, I don't know what interface I would have that can be mapped to MyContext.
The reason why most people bother to create an interface through which they will access their data layer is to abstract away the hard dependency on EF (usually, to ease testing of dependent types), if you don't know what interface to define, perhaps you don't need to abstract EF at all and you can end-up with container.RegisterType<MyContext>, or avoid IoC at all. However, the path most people tend to choose is abstraction using the Repository pattern. See codeproject.com/Articles/207820/…
I think in older version of the Unity.Mvc NuGet package it was missing for some reason. Try to install Unity.Mvc5 first, if it's still missing, add it manually using the code in the second link.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.