0

I´ve been checking Microsot Unity IOC and found some examples using Code First approach. On the other hand I cannot find any tutorial or configuration in order to include Unity IoC with edmx files using a database first approach. I will be glad in anyone could shed some light on it.

I tried using http://unitymvc3.codeplex.com/ and using unity 2.1 directly = http://unity.codeplex.com/

sorry that I cannot provide code but truly I´m very confused about IOC patterns and I was not able to generate a demo solution. brgds.

1 Answer 1

3

IoC is simply the turning of an object inside out so that instead of containing internal hard references to objects (dependencies), instead those same objects are passed into it from the outside. The turning inside out is the inversion of control, and the injecting of the objects it needs is the dependency injection and is often done your container (unity).

All IoC containers are the same, they have some way to register or discover dependencies and then a way to resolve reference. Mostly the resolution involves asking for a reference of FooClass and getting an object in return. Often you don't actually ask for a concrete type like FooClass and instead ask for an IFooClass so as to decouple your usage from the actual type that gets passed in.

So in your case you need to register your EF data context as a dependency in unity. I have not used unity before so please forgive any minor errors.

container.RegisterType<YourContext, YourContext>();

Add a dependency to your class. Say you have a FooRepository that implements IFooRepository.

public FooRepository : IFooRepository 
{
    private YourContext context;

    public FooRepository(YourDataContext context) {
         this.context = context;
    }
}

In MVC 3 you register unity as your default dependency resolver which means all requests for controllers are fed through it;

protected void Application_Start()
{
    ...
    var container = new UnityContainer();
    container.RegisterType<YourContext, YourContext>();
    container.RegisterType<IFooRepository, FooRepository>();

    container.RegisterControllers();

    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

Now your controller can add arguments to it's constructor and have them populated.

public class MyController : Controller 
{
    private IFooRepository repository;

    public MyController(IFooRepository repository)  {
        this.repository = repository;
    }

}

When this controller get instantiated it will receive an instance of IFooRepository, which will receive a reference to YourContext. This continues all the way down the chain.

EDIT

The edmx file is simply a designer that creates a C# context class under the covers. Click on the surface and view the properties to see the name of the class.

enter image description here

So you can register it in exactly the same way as any other class. In this case.

container.RegisterType<Model1Container, Model1Container>();

Hope this helps.

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

3 Comments

Hi @Madcapnmckay thank you for your answer. I have one problem, I dont know how to register my edmx file as container.registertype<yourcontext, yourcontext>(). I have created those container.registertype for all my IFooclass and Fooclass but I dont know what to do with the edmx representation. thanks for your help in advance.
thank you for the edit. I built again the solution and include the name that figure in entity container name but is not recognised by intellisense... I will check if I´m doiing something else wrong because I believe that your approach is the correct path but I dont know why for my solution is not working.. I will try again and write.. thank you so much!
Perfect, I found what happened. I changed edmx file properties to the following details and it was recongnized in container.registertype: 1- Code generation move to: Default 2- lazyloading : false and it worked. Thank you @Madcapnmckay!

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.