nickbw
nickbw

Reputation: 463

Repository and Service Tier Interaction Question

I have a generic repository interface, that has the usual methods for saving, reading and querying from the service tier like so:

public interface IRepository<T>
{
    T GetById(int id);
    void Save(T entity);
    void Update(T entity);
    void Delete(T entity);
    IEnumerable<T> GetAll();
}

If I have a service, for example a User service that uses a concrete implementation of the IRepository with User as its type (IRepository<User>), if the service itself might need something from another IRepository say IRepository<Admin> should the service call IRepository<Admin> directly, or should it call an associated service (i.e., the service that primarily deals with the IRepository<Admin> repository)?

I personally can see an issue if you are pulling items directly from a repository, if say you wanted to apply certain business rules before the results are returned to a client, but on the other-hand a service might want to work on the raw result set and to apply its own rules to the results, so I am a bit confused about which direction to take, any suggestions would be greatly appreciated.

Upvotes: 4

Views: 303

Answers (1)

Raghu
Raghu

Reputation: 2768

If the implementation detail of all the repositories is same, you could probably create an abstract BaseRepository, for example:

protected abstract class BaseRepo<T> : IRepository<T>
     where T : class
{
    #region IRepository Members

    // Implement all IRepository members here and make them public

    #endregion
}

then you can create a dedicated AdminRepository, for example:

public class AdminRepo : BaseRepo<Admin> {}

and to call from another repository you can do the following:

public class UserRepo : BaseRepo<User>
{
    private AdminRepo adminRepo = new AdminRepo();

    public UserRepo()
    {
        Admin person = adminRepo.GetById(1);
    }
}

hope that helps.

Upvotes: 1

Related Questions