Step 1. Define the Base Entity
First, you need to define Base Entity in your Domain Layer,
Code for Base Entity
public class BaseEntity {
public int Id {
get;
set;
}
public int UsesId {
get;
set;
}
public int CreatedBy {
get;
set;
}
public int CompanyId {
get;
set;
}
public DateTime CreatedDate {
get;
set;
} = DateTime.Now;
public DateTime ModifiedDate {
get;
set;
}
public bool IsDeleted {
get;
set;
}
}
Step 2. Define the Generic IRepository
Define the Generic Interface in your Repository Layer as shown in the picture,
Code for IRepository
using AC_Contact_API_Service_Layer.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
namespace AC_Contact_API_Repository_Layer.IRepository {
public interface IRepository < T > where T: BaseEntity {
IEnumerable < T > GetAll();
T Get(int Id);
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
void Remove(T entity);
void SaveChanges();
}
}
Step 3. Define the Generic Repository
Define the Generic Repository In your Repository Layer Using the Onion Architecture,
Code For Generic Repository
using AC_Contact_API_Repository_Layer.IRepository;
using AC_Contact_API_Service_Layer.App_Data;
using AC_Contact_API_Service_Layer.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AC_Contact_API_Repository_Layer.Repository {
public class Repository < T > : IRepository < T > where T: BaseEntity {
#region property
private readonly ApplicationDbContext _applicationDbContext;
private DbSet < T > entities;
#endregion
#region Constructor
public Repository(ApplicationDbContext applicationDbContext) {
_applicationDbContext = applicationDbContext;
entities = _applicationDbContext.Set < T > ();
}
#endregion
public void Delete(T entity) {
if (entity == null) {
throw new ArgumentNullException();
} else {
entities.Remove(entity);
_applicationDbContext.SaveChanges();
}
}
public T Get(int Id) {
try {
if (Id == null) {
throw new ArgumentNullException();
} else {
return entities.SingleOrDefault(c => c.Id == Id);
}
} catch (Exception ex) {
throw new Exception(ex.Message);
}
}
public IEnumerable < T > GetAll() {
return entities.AsEnumerable();
}
public void Insert(T entity) {
if (entity == null) {
throw new ArgumentNullException("entity");
}
entities.Add(entity);
_applicationDbContext.SaveChanges();
}
public void Remove(T entity) {
if (entity == null) {
throw new ArgumentNullException("entity");
}
entities.Remove(entity);
}
public void SaveChanges() {
_applicationDbContext.SaveChanges();
}
public void Update(T entity) {
if (entity == null) {
throw new ArgumentNullException("entity");
}
entities.Update(entity);
_applicationDbContext.SaveChanges();
}
}
}
Conclusion
In API (Application Programming Interface) development, the term "generic" refers to a type or method that can work with values of any data type rather than being restricted to a specific data type. Generics allow you to write flexible, reusable code that can be applied to different data types, making your API more versatile and easier to maintain. For example, a generic list in a programming language such as Java or C# can be defined to hold elements of any type, making it useful for various tasks rather than creating separate lists for different data types.
If you want to study Onion Architecture, follow the link below,