linq to sql - Should repositories expose IQueryable to service layer or perform filtering in the implementation? -
I am trying to decide on the best pattern for data access in my MVC application. Currently, after following the MVC storefront series, I am using the repository, I am exposing a service layer to the IQueryable, which then applies the filter. Initially I am using LINQtoSQL. E.g.
Public Interface IMyRepository {IQueryable & lt; MyClass & gt; get all(); }
implemented into:
public class LINQtoSQLRepository: IMyRepository {public IQueryable & lt; MyClass & gt; GetAll () {Return to the table in dbContext.table New MyClass {Field1 = table.field1, ... etc}}}
Filters for ID:
Public Static Class TableFilters {Public static MyClass WithID (This IQueryable Curie, String ID} {Returns (from T to qry where t.ID == ID is selected). SingleOrDefault ( ); }}
Service called from:
public class table service {public MyClass RecordsByID (string id) {returns _repository.GetAll () .WithID Id) ; }}
When I used LINQ with the Entity Framework to apply the repository with entities, in the filter class in my project "WHERE ... == ..." In the example above, there are some more complex operations, which I believe require different implementations based on the LINQ provider. Specifically I need to break a SQL "WHERE ... IN ..." I am able to implement in the filter class using this:
string [] aParams = // qry = qry.Where of id (t = & gt; a.Param.Contains (t.ID)); However, to do this against the unit framework, I have to provide the solution as if the unit is connected to the framework, that means I have 2 different implementations of this special filter. Based on the base provider. I appreciate any advice about how to proceed from here. I felt like exposing an IQueryable from my repository, allowing me to filter for it regardless of the underlying provider, and enables me to switch between providers when needed, though With the stated problem, I think that I should return all my filtering to the repository within the IEnumerable, ILIT or single classes.
Many thanks, Matt
This is a very popular question that is one Frequently Ask Yourself I've always felt best to return to IyEnableable instead of a repository instead of IQueryable.
The purpose of a repository is to explain the database infrastructure so that the customer does not worry about the data source. However, if you return the IQueryable, then you depend on the consumer's compassion, which type of query runs against your DB, and will they do something that does not support the LINQ provider.
Take paging for example, say you have a client unit and your database can have hundreds of thousands of customers, which code will your client have?
var customer = repos.GetCustomers (). Skip (skipCount) Move (page size) .OolIS ();
or
var customer = repos.GetCustomers (Page Index, Page Size);
In the first approach, you make the repository impossible to limit the number of records obtained from the data source. In addition, the calculation of quitting your consumer is calculated.
In another approach, you provide a more coarse grain interface to your customer. Now, your repository can apply some constraints on page sections to customize the query. You also close the calculation of skipCount.
However, it is being said that, if your client is your service in your situation, then I think that the question really comes down to the separation of concerns, how is it better to have such validation logic? Well the answer can be very "in service" but "Where is better to include curie logic?" What about the answer? I have the answer clearly "repository". This is the area of expertise you want
Comments
Post a Comment