0

I am super new to Dependency Injeection and using "Unity Mvc" as my DI container. I have a ProductRepository Class which is using my dbContext class to get data, however I would love to register my dbContext. here is what I have...

Partial Public Class websolutionsEntities
    Inherits DbContext
' Code for dbContext Class

' This is my Repo, which I want to fix
Public Class productsRepository
    Implements IProductRepository

    ' This line below I want to avoid, I dont want to rely on my dbContext for all repo's

    Private _db As websolutionsEntities = New websolutionsEntities()

and here is my Unity Bootstrap:

Private Shared Function BuildUnityContainer() As IUnityContainer
    Dim container = New UnityContainer()

    ' register all your components with the container here
    ' e.g. container.RegisterType<ITestService, TestService>();       
    container.RegisterType(Of IProductRepository, productsRepository)()


    Return container

End Function

What do i need to do, so in my Repo i can avoid creating a new instance of dbContect class?

Jasen
  • 13,170
  • 3
  • 43
  • 64
highwingers
  • 1,629
  • 4
  • 19
  • 37
  • Ultimately, you will need your `websolutionsEntities` to implment an interface (i.e. `IWebsolutionsEntities`). With that done you can register `IWebsolutionsEntities` with your DI container and you're done. Now the more interesting question is what exactly your interface will look like. Unfortunately, there are various opinions and you will need to decide for yourself. I think the place to start would be to look up "Unit of Work" in relation to DbContext. You will also need to consider the lifetime of your `websolutionsEntities` instance and dispose of it properly. – Jasen Dec 07 '13 at 22:04
  • This question should point you to the right direction. http://stackoverflow.com/questions/5187562/mvc-ef-datacontext-singleton-instance-per-web-request-in-unity – Spock Dec 07 '13 at 22:30
  • my dbContext will end up having over 100 entities, and maybe more as time goes by, is the best solution is to have an interface for my dbcontext, I have no idea what "Unit of Work" is. – highwingers Dec 07 '13 at 23:25

1 Answers1

2

Will answer with C# syntax, ok?

Make an interface for your context:

public interface IWebSolutionEntities
{
}

public class WebSolutionEntities : IWebSolutionEntities
{
   ...
}

And add a constructor to the repository, that depends on this interface:

public class ProductsRepository : IProductsRepository
{
    private IWebSolutionEntities _db;

    public ProductsRepository(IWebSolutionEntities db)
    {
        _db = db;
    }
}

A controller depends on the repository interface:

public class ProductsController : Controller
{
    private IProductsRepository _productsRepository;

    public ProductsController(IProductsRepository _productsRepository)
    {
        productsRepository = _productsRepository;
    }
}

Then you need to register a dependency resolver in global.asax:

protected void Application_Start()
{
    DependencyResolver.SetResolver(new UnityDependencyResolver(UnityBootstrap.BuildUnityContainer()));
}

After that controllers will be created by the UnityDependencyResolver instance.

nativehr
  • 222
  • 2
  • 8