Generic Type in Repository – .NET Core Part
We have talked about the CRUD in the .NET Core cases and the .NET MVC cases based on the Onion architecture. Our methods create the interface class file and the repository class file, then let the repository class inherits the interface class’s methods. In these cases, we only design an entity model to use an interface class and a repository class that means this idea is one-on-one concepts. The developer shouldn’t write the same source code too many times as the web application projects, so we should design the repository class files and the interface class file, which can inherit any one entity model class file. The interface class file’s methods must have the primary way, for example, CRUD. In the interface class file, we can add the calling stored procedures and the disposing methods.
We start to design the new interface class’s methods and the repository class file’s method. We must understand what kind of process should be created by developers in the interface class file. The interface class files and the repository class file must use the generic type methods and object methods. Generics let you tailor a method, class, structure, or interface to the precise data type it acts upon. Object class support all classes in the .NET class hierarchy and provides low-level services to derived classes.
Generic type in Repository – Tutorial
Step 1. Create the new interface class file under the Core project. The new interface class file’s name sets “IGenericTypeRepository.”

Step 2. Add the synchronous CRUD methods and the asynchronous CRUD method in the IGenericTypeRepository file. It will have more source code and methods, so it must add the mark notes.

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>T</returns>
Task<T> Find(Expression<Func<T, bool>> predicate);
/// <summary>
/// Async Find
/// </summary>
/// <param name="predicate">Where conditions</param>
/// <returns>T</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();
}
3 Comments