Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC repository architecture and accessing different tables

Thank you for helping me understand some of this stuff:

Say I have 2 controllers in an MVC application - 1 controls viewModels related to salespeople 1 controls viewModels related to sales

Each have a their own repository, which access data using Entity framework (code first)

both repositories are set up to handle dependency injection, but also have 0 argument constructors with defaults to use the appropriate EF dataAccess.

the salespeople controller uses a _salesPeopleRepository.getAllSalesPeople() function which returns a List of salespeople to populate an index view.

the sales controller needs to access the same list to populate a dropdownlist.

there are several ways to get the information to the sales controller, and I was wondering which options would be considered best practice:

a) In the controller

db = new DataContext();
_saleRepos = new SalesRepository(db);
_salesPeople = new SalesPeopleRepository(db);
 .....
modelA.SalePeopleSelectList = SelectList(_salesPeople.getAllSalesPeople(),"id","name")

b) in the SalesRepository - either using EF itself:

public IEnumerable<salesPerson> getAllSalesPeople()
{ 
    return _db.SalesPeople.ToList();
}

c) or instantiating and injecting the same data access object before calling the function

public IEnumerable<salesPerson> getAllSalesPeople()
{ 
    return (new SalesPersonRepository(_db)).getAllSalesPeople();
}

Edit

If the answer is a), how should custom buisiness logic be called from 1 repository - say for instance sales has a storeId and the repository checks that the storeId entered for the sale matches the storeId for the salesPerson. Should the salesPerson object used for buisness logic purposes (within the salesRepository) be accessed via the salesPerson repository, or directly from the dataContext object?

Thank you for your thoughts and experience

like image 559
Brent Avatar asked Nov 17 '25 20:11

Brent


1 Answers

It doesn't make sense to have your SalesRepository retrieving data from the SalesPerson table. That data access logic ought to be centralized in the SalesPeopleRepository. Duplicating the method across repositories will just muddy the water, in my opinion.

So why not have both the SalesRepository and SalesPeopleRepository used in your Sales Controller? I would just instantiate an instance of the SalesPeopleRepository and use the method already defined in there.

Also, if your Controllers are using dependency injection, you could just have the repositories passed in to the constructor:

public SalesController (ISalesRepository salesRepository, ISalesPeopleRepository salesPeopleRepository)
{
   this._salesRepository = salesRepository;
   this._salesPeopleRepository = salesPeopleRepository;
}
like image 154
Garrett Vlieger Avatar answered Nov 20 '25 08:11

Garrett Vlieger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!