Generic Type in Repository – .NET MVC Part
Last article that we talk about the Generic type in the repository of the .NET Core part (“Generic Type in Repository – .NET Core Part“) and demo this concept in the .NET Core web application solution. We change the framework from .NET Core to .NET MVC, which see the same ideas in different framework solution projects that will happen? How to do the repository’s generic type way in .NET MVC solution?
Suppose this source code seems like the .NET Core cases. We create the new class files under the Infra project and the Core project. These files, respectively, are the new interface class files and the new repository class files. Remember a thing that the .NET MVC case web application solution project doesn’t use Microsoft.EntityFrameworkCore. A part of the method will be a few differences in the new repository class files.
Generic type in Repository – Tutorial
Step 1. Create the new interface class file under the Core project. The new interface class file name sets “IGenericTypeRepository.” Add the synchronous CRUD methods and the asynchronous CRUD methods. It also does the same as things that mark note above the method name position.


public interface IGenericTypeRepository<T> where T: class
{
/// <summary>
/// GetAll
/// </summary>
/// <returns>IEnumerable<T></returns>
Task<IEnumerable<T>> GetAll();
/// <summary>
/// Async GetAll
/// </summary>
/// <returns>IEnumerable<T></returns>
Task<IEnumerable<T>> AsyncGetAll();
/// <summary>
/// Find
/// </summary>
/// <param name="predicate">Where conditions</param>
/// <returns></returns>
Task<T> Find(Expression<Func<T, bool>> predicate);
/// <summary>
/// Async Find
/// </summary>
/// <param name="predicate">Where conditions</param>
/// <returns></returns>
Task<T> AsyncFind(Expression<Func<T, bool>> predicate);
/// <summary>
/// Get information by ID
/// </summary>
/// <param name="id">int32</param>
/// <returns></returns>
Task<T> GetById(int id);
/// <summary>
/// Async Get information by ID
/// </summary>
/// <param name="id">int32</param>
/// <returns></returns>
Task<T> AsyncGetById(int id);
/// <summary>
/// Create new data
/// </summary>
/// <param name="entity">entity model class</param>
/// <returns></returns>
Task Create(T entity);
/// <summary>
/// Async create new data
/// </summary>
/// <param name="entity">entity model class</param>
/// <returns></returns>
Task AsyncCreate(T entity);
/// <summary>
/// Update data
/// </summary>
/// <param name="entity">entity model class</param>
/// <returns></returns>
Task Update(T entity);
/// <summary>
/// Async update data
/// </summary>
/// <param name="entity">entity model class</param>
/// <returns></returns>
Task AsyncUpdate(T entity);
/// <summary>
/// Delete data
/// </summary>
/// <param name="entity">entity model class</param>
/// <returns></returns>
Task Delete(T entity);
/// <summary>
/// Async delete data
/// </summary>
/// <param name="entity">entity model class</param>
/// <returns></returns>
Task AsyncDelete(T entity);
/// <summary>
/// Row count
/// </summary>
/// <param name="predicate">Where conditions</param>
/// <returns>Row count number.</returns>
Task<int> RowCount(Func<T, bool> predicate);
/// <summary>
/// Row count
/// </summary>
/// <param name="predicate">Where conditions</param>
/// <returns>Row count number.</returns>
Task<int> AsyncRowCount(Func<T, bool> predicate);
/// <summary>
/// Database save change.
/// </summary>
/// <returns></returns>
Task SaveChange();
/// <summary>
/// Database async save change.
/// </summary>
/// <returns></returns>
Task SaveChangeAsync();
}
Note: If you want to see the complete source code about the new interface class file, please, click my GitHub repository.