简体   繁体   中英

Is there a code smell in this Generic Repository using Ado.Net?

I have been reading this article for refreshing my understanding on the repository pattern and figured out that my repository can be a combination of multiple methods that work for the business layer that uses that repository. So with that understanding I came up with this class and interface for the UserRepository

public class UserRepo : IUserRepo 
{

    public TheUser RegisterUser(UserRegistrationDetails details)
    {
        //The details here are what is required for inserting to the user table plus a
        //few other related tables which are decided and populated at the Business layer.
    }

    //example method returning data from a combination of multiple tables
    //and a dto created specially for that purpose
    public BaggageDetails GetBaggageDetailsForUser(string username)
    {
        //implementation here
    }

    //example method returning data from a combination of multiple 
    //different tables
    public TravelDetails GetTravelDetailsForUser(string username)
    {
        //implementation here
    }


 }

Here the methods basically return a custom dto which in each case has either all or some of the fields of the [Users] table, plus a bunch of other fields that are not part of the [Users] table.

Then I came across this article which spoke about a generic repository, so now my class and interfaces look like this

public class UserRepo : IDisposable, IUserRepo, IGenericRepo<TheUser> //,IGenericRepo<Users>
{

    public TheUser RegisterUser(UserRegistrationDetails details)
    {
        //
    }

    //example method....
    public BaggageDetails GetBaggageDetailsForUser(string username)
    {
        //implementation here
    }

    //example method....
    public TravelDetails GetTravelDetailsForUser(string username)
    {
        //implementation here
    }



    //New methods added for the generic repository
    public IList<TheUser> GetAll()
    {
        //
    }

    public TheUser GetById(int id)
    {
        //
    }

    public void Save(TheUser saveThis)
    {
        //
    }

    public void Delete(TheUser deleteThis)
    {
        //
    }

    //Should I implement this here?
    public void Dispose()
    {
        //TODO
    }
}

Am I right in having the UserRepo be implemented of many interfaces like this? The generic repository has methods that pertain to the aggregate root of the repository (if I am using the term right).

Questions

  1. Should I put the dto pertaining to the aggregate root in IGenericRepo<Users> or the bigger dto that has more information than is required for updating the [Users] table as I have done above IGenericRepo<TheUser>
  2. Now if I have mock the UserRepo for testing purposes I am not sure which interface to use

Edit Other than answers to the questions above, I am looking for thoughts on this subject of creating repositories. I know this falls under the "it depends" category, but would love to hear what you all think about this way of building a repository. What have those who have created and used repositories done in their live projects?

Generic repository makes only sense if you treat you data in common way. Classic repository pattern build up Get(GetAll/GetById etc.),Create and Delete, but could of cause skip some of them. It is very depends on you application. Important to consider that Repository creates level of abstraction to you data storage, in order to be able replace existing data storage with a different one and you have better possibility to test using some mock data.

In your example you implement very specific user repository and I can not see how much data you need in your application additionally. So it looks like you try to force your User repository to be able do things which are not needed at all by implementing the generic repository interface.

I recommend to use generic approach if you really have common base on data treatment.

DTO(Data Transfer Object) should provide so much data as you need on your consumer side(eg UI) for one specific use case. It is really depends on how much data you want to visualize/consume at once and it should be in balance with the costs for transfer of the data and number of service calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM