Q:

ASP.NET MVC 5 solution architecture improvement - multiple services with the same methods and code

I'm currently building an ASP.NET MVC 5 application using Unity and Entity Framework.  
Here is my architecture (the solution contains multiple projects) :  

Bootstrapper : project that contains the link between my interfaces and class for the repositories and the services
Data : mapping between my models and the database objects. This project contains the repositories as well.
Domain : this one contains the application constants + the interfaces of my repositories and services
Models : contains the models used in the application
Services : contains all the class Services
Web : the actual application with the views, controllers, view models, ...

Here is my issue: in multiple services I have the same methods (Get an item, check if it exists, ...) and these methods are exactly the same in every services except that they don't use the same repository and the same model.  
Example : 
public IQueryable<Car> Search(string search)
{
   #region Sanitize parameters + Contracts
   Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(search), Resources.Resources.SearchRequired);

   search = StringHelper.SafePlainText(search);
   Contract.Assume(search.Length <= 100 && search.Length > 1);
   #endregion

   return _carRepository.Search(StringHelper.SafePlainText(search));
}

I want to "extract" these methods so I don't have to recreate the same code over and over.  
First, I thought about create an abstract class that my service inherit from but I couldn't figure out how to pass the right repository to the mother class.  
This is what I tried :  
public abstract class Service<T>
{
   public object Repository { get; set; }

   protected Service(object repository) 
   {
       Repository = repository;
   }

   public IQueryable<T> Search(string search) 
   {
      #region Sanitize parameters + Contracts
      Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(search), Resources.Resources.SearchRequired);

      search = StringHelper.SafePlainText(search);
      Contract.Assume(search.Length <= 100 && search.Length > 1);
      #endregion

      return Repository.Search(StringHelper.SafePlainText(search));
   }
}

But this, of course, doesn't work.  
So I'm asking you guys if you have an idea to how I can manage to make this work, if this is possible at least.
Thank you in advance and excuse me for the poor language, I'm not an English native speaker.
Thibault.ce

A:

The best solution that I found is to create a generic class which depends of 2  types: The repository and the entity.
A BaseService< T1, T2 > which takes in parameter a "BaseRepository" and a "IEntity" and which will contain all the generic methods. This class must contain a property containing the instance of the repository. Thus, you can use the repository as shown in your example.
Then you can create specific Service classes, like a CarService which will inherit from BaseService< CarRepository, Car >.
A simple example, based on the example of my previous answer :
public class BaseService<TRepo, TEntity> where TRepo : BaseRepository<TEntity> where TEntity: IEntity
{
    public TRepo Repository { get; set; }

    public BaseService(TRepo repository)
    {
        this.Repository = repository;
    }

    public List<TEntity> GetAll()
    {
        return this.Repository.GetAll().ToList();
    }
}

public class UserService : BaseService<UserRepository, User>
{
    public UserService(UserRepository repository)
        : base(repository)
    {
    }

    public List<User> GetAllUserSortByName()
    {
        return this.Repository.GetAllUserSortByName();
    }
}

You can use it like this :
YourEntities entities = new YourEntities();
UserRepository repo = new UserRepository(entities);
UserService service = new UserService(repo);

List<User> users = service.GetAllUserSortByName();

Let me know if my answer is not clear enough.
Edit :
Final example: https://dotnetfiddle.net/3s5BKZ

