0

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?

1 Answer 1

0

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(e.g. 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.

4
  • Thanks for the comment. Why would there be a need to treat data in a common way. There is always going to be somethings in common and other things specific to a particular case. In this case since we are talking about the UserRepository, obviously there is going to be a table called Users which is the aggregate root and it will depend on related tables for getting other data. For creation of a User record I will need all the details pertaining to the User table. Then additional details or transaction records get created on other conditions in the system.When deleting I only need userid
    – user20358
    Jan 5, 2016 at 8:14
  • So basically what I was thinking behind this design was to create common set of functionality that is required for the root tables in each scenario. Each table in the system will have this in common with other tables.In the case of the User table,all the Create,Update, Delete, View functions querying the User table + other fucntions that get records which are joined from the User table to other tables.
    – user20358
    Jan 5, 2016 at 8:19
  • In this case I would inherit IUserRespository from generic IGenericRepository<T>. Than you have only one you need to consider working with users. Of cause if you have additional data (excepting user data) and for all of those data create , get and delete is required, than it makes sense to go for generic repository approach. Jan 5, 2016 at 9:34
  • Thank you for your contribution :)
    – user20358
    Jan 5, 2016 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.