Categories: ASP.NETASP.NET Core

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.”

Fig 1. Solution explorer’s core project

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.

Fig 2. IGenericTypeRepository source code
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();
 
}

Page: 1 2 3

davidsky69

View Comments

Recent Posts

API Gateway in .NET 5 with Ocelot

What is the API gateway? An API gateway is an API management tool that sits…

4 years ago

.NET 5 application with Onion architecture

The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…

4 years ago

SOLID Principles – Dependency inversion principle

In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…

4 years ago

SOLID Principles – Interface segregation principle

In the field of software engineering, the interface segregation principle (ISP) states that no code…

4 years ago

SOLID Principles – Liskov substitution principle

Subtype Requirement: Let  be a property provable about objects  of type T. Then  should be true for objects  of type S where S is…

4 years ago

SOLID Principles – Open-closed principle

In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…

4 years ago