Question

I have an ASP.NET MVC3 in C# and Razor. The architecture of the application is divided in Data Access Layer (EF classes + Repository), Service Layer, Controller, ViewModels and View.

From my Service Layer ProductServices I call the method GetAllProducts exposed by my Repository ProductRepository , which has the following signature:

IQueryable<Products> GetAllProducts()

Therefore within ProductServices I call (productRepository is an instance of ProductRepository):

var products = productRepository.GetAllProducts(); 

that populates the variable products. Now I would like to access the name of the product ProductName from productServices. If I use this instruction:

var productNames = products.Select(m => m.ProductName).ToList();

I am creating coupling between ServiceLayer and the EF (bypassing the repository). That means I must add to ProductRepository a method with signature:

IQueryable<string> GetAllProductsName()

However, since I need other product information in my application, shall I create one method in productRepository for each field of the Product class? Is my reasoning correct? Thanks

Was it helpful?

Solution

Theres two schools of thought around this,

  1. A repository explicitly defines the way you interact with the database and should be tightly controlled. Therefore methods on the repository should provide enumerated data
  2. The repository breaks the strong coupling to a specific datasource type but does not need to provide detailed and enumerated datasets.

Personally i subscribe to the second, heres why:

my feeling is that when you get overly explicit within the repository it turns into business logic rather than a decoupling mechanism. I don't really like this as it means that you become more tightly linked to the repository implementation.

I also think that in some cases the repository isnt the right place to enumerate the data, for example I believe paging and sorting is a UI concern, however for performance you want queries to only relate to the current page/sort. this means you either need to let the UI contribute to the query compilation or the repository needs to understand paging and sorting.

Having said that providing un-enumerated datasources does open you up for issues later down the track, and even if you do provide them its very important to enumerate the set as soon as possible.

If you're interested heres my take on the repositories: http://blog.staticvoid.co.nz/2011/10/staticvoid-repository-pattern-nuget.html , all the code is also on github

OTHER TIPS

You have all information in your productServices because you load all information from your Repository with the method productRepository.GetAllProducts(). If you need more information from an other entity then you need to extend your productServices with a new Service.

However, since I need other product information in my application, shall I create one method in productRepository for each field of the Product class? Is my reasoning correct? Thanks

In this situation normally I create Extensions method's for the service and not in the repository. The Repository have normally a CRUD Setup and nothing more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top