Mist
Mist

Reputation: 684

ASP.Net MVC: working with multiple repositories & injection by Unity DI

I am working with asp.net mvc 5 project. suppose i am showing Customer data where i am showing customer detail and customer favorite products.

so i fetch data from customer repository, country repository and favorite repository.

many time people write article on injection repository by unity DI. when i am working with single repository then this concept make sense but when i have to fetch data from multiple repositories then how could i inject multiple repositories in mvc controller ctor by unity di?

see a small code for injecting repository by unity DI.

public class FooController : Controller  
{  
     readonly IFooRepository _repository;  

     // Inject here  
     public ProductController(IFooRepository repository)  
     {  
           _repository = repository;   
     }  

     // Use it here  
     public ActionResult Bar()  
     {  
          var bar = _repository.DoSomething();  
     }  
}  

above code refer from https://forums.asp.net/t/2105895.aspx?Repository+inject+into+controller+by+Unity+DI

now tell me how could i refactor my code or what approach i should follow as a result i can work with multiple repositories and also can inject by Unity DI.

please give me best guidance. thanks

Upvotes: 1

Views: 1061

Answers (2)

thmshd
thmshd

Reputation: 5847

Please note that while L-Four's response is a often a good way to go, you might run into difficulties later when you did some modification to loaded entities and want to save them, as you probably ended up having separate DBContext instances inside of your repositories. But it depends on your Repository and DI implementation and configuration...

Example:

// Assume you want to create a new User with associated Account

var user = _usersRepository.AddUser(new User(....));
var account = _accountRepository.AddAccount(new Account{ ... User = user });

// Now you want to save them both in one transaction... how?
_usersRepository.Commit();
_accountRepository.Commit(); // What if this fails? you have an orphaned user?

To solve this problem, I'd recommend implementing the so called Unit Of Work pattern. There are some good examples also on Stack Overflow and elsewhere.

Might save you some headache for later.

Your updated code will be:

public class FooController : Controller  
{  
     readonly IUsersAndAccountUnitOfWork _uow;  

     // Inject here  
     public ProductController(IUsersAndAccountUnitOfWork uow)  
     {  
           _uow = uow;
     }  

     // Use it here  
     public ActionResult Bar()  
     {  
           var user = _uow.Users.AddUser(new User(....));
           var account = _uow.Accounts.AddAccount(new Account{ ... User = user });

           _uow.Commit();
     }  
} 

Upvotes: 0

L-Four
L-Four

Reputation: 13531

Just add any dependency you require into the controller's constructor.

public class FooController : Controller  
{  
    readonly IFooRepository _repository;  
    readonly IOtherRepository _otherRepository;  

    public ProductController(IFooRepository repository, IOtherRepository otherRepository)  
    {  
        _repository = repository;   
        _otherRepository = otherRepository;
    }  

Upvotes: 1

Related Questions