s_h
s_h

Reputation: 1496

unity mvc3 - configuring using database first approach

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.

Upvotes: 0

Views: 921

Answers (1)

madcapnmckay
madcapnmckay

Reputation: 15984

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.

Upvotes: 3

Related Questions