Command Builder Design Pattern in an ASP.NET Core Web API using a Three-Tier Architecture for CRUD operations. For the sake of this example, let's assume you have a model named CSharpCornerArticle.
Three-Tier Architecture Overview
- Presentation Layer (Controller): Handles incoming HTTP requests, processes input data, and interacts with the service layer.
- Business Logic Layer (Service): Contains business logic, validates data, and coordinates the interaction between the presentation and data access layers.
- Data Access Layer (Repository): Manages data storage, retrieval, and manipulation.
Model - CSharpCornerArticle
// Sardar Mudassar Ali Khan
public class CSharpCornerArticle
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
// Add other properties as needed
}
Data Access Layer (Repository)
//Sardar Mudassar Ali Khan
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
public interface ICSharpCornerArticleRepository
{
CSharpCornerArticle GetById(int id);
IEnumerable<CSharpCornerArticle> GetAll();
void Add(CSharpCornerArticle article);
void Update(CSharpCornerArticle article);
void Delete(int id);
}
public class CSharpCornerArticleRepository : ICSharpCornerArticleRepository
{
private readonly YourDbContext _dbContext; // Replace YourDbContext with your actual DbContext class
public CSharpCornerArticleRepository(YourDbContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
public CSharpCornerArticle GetById(int id)
{
return _dbContext.CSharpCornerArticles.Find(id);
}
public IEnumerable<CSharpCornerArticle> GetAll()
{
return _dbContext.CSharpCornerArticles.ToList();
}
public void Add(CSharpCornerArticle article)
{
_dbContext.CSharpCornerArticles.Add(article);
_dbContext.SaveChanges();
}
public void Update(CSharpCornerArticle article)
{
_dbContext.Entry(article).State = EntityState.Modified;
_dbContext.SaveChanges();
}
public void Delete(int id)
{
var article = _dbContext.CSharpCornerArticles.Find(id);
if (article != null)
{
_dbContext.CSharpCornerArticles.Remove(article);
_dbContext.SaveChanges();
}
}
}
Business Logic Layer (Service)
// Sardar Mudassar Ali Khan
public interface ICSharpCornerArticleService
{
CSharpCornerArticle GetArticleById(int id);
IEnumerable<CSharpCornerArticle> GetAllArticles();
void AddArticle(CSharpCornerArticle article);
void UpdateArticle(CSharpCornerArticle article);
void DeleteArticle(int id);
}
public class CSharpCornerArticleService : ICSharpCornerArticleService
{
private readonly ICSharpCornerArticleRepository _repository;
public CSharpCornerArticleService(ICSharpCornerArticleRepository repository)
{
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
}
public CSharpCornerArticle GetArticleById(int id)
{
return _repository.GetById(id);
}
public IEnumerable<CSharpCornerArticle> GetAllArticles()
{
return _repository.GetAll();
}
public void AddArticle(CSharpCornerArticle article)
{
if (article == null)
{
throw new ArgumentNullException(nameof(article));
}
_repository.Add(article);
}
public void UpdateArticle(CSharpCornerArticle article)
{
if (article == null)
{
throw new ArgumentNullException(nameof(article));
}
_repository.Update(article);
}
public void DeleteArticle(int id)
{
_repository.Delete(id);
}
}
Join the conversation! Your thoughts help the community grow.