要在C#中搭建基於ISqlSugarClient的三層架構框架,你需要定義資料訪問層(DAL)、業務邏輯層(BLL)和表現層(UI)。下面是一個完整的例子,涉及資料庫倉儲、業務邏輯層,以及依賴注入。這個例子基於ASP.NET Core MVC構建,使用ISqlSugarClient來處理資料訪問。這個例子中,我們將使用User作為一個簡單的實體來演示三層架構。
資料庫實體類
public class User { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } }
資料訪問層介面與實現
// 資料訪問層介面 public interface IUserRepository { User GetById(int id); List<User> GetAll(); void Add(User user); void Update(User user); void Delete(int id); } // 基礎倉儲實現 public abstract class BaseRepository<T> where T : class, new() { protected readonly ISqlSugarClient _sqlSugarClient; protected BaseRepository(ISqlSugarClient sqlSugarClient) { _sqlSugarClient = sqlSugarClient; } public virtual T GetById(int id) { return _sqlSugarClient.Queryable<T>().InSingle(id); } public virtual List<T> GetAll() { return _sqlSugarClient.Queryable<T>().ToList(); } public virtual void Add(T entity) { _sqlSugarClient.Insertable(entity).ExecuteCommand(); } public virtual void Update(T entity) { _sqlSugarClient.Updateable(entity).ExecuteCommand(); } public virtual void Delete(int id) { _sqlSugarClient.Deleteable<T>().In(id).ExecuteCommand(); } } // User資料訪問層實現 public class UserRepository : BaseRepository<User>, IUserRepository { public UserRepository(ISqlSugarClient sqlSugarClient) : base(sqlSugarClient) { } }
業務邏輯層
public class UserService { private readonly IUserRepository _userRepository; public UserService(IUserRepository userRepository) { _userRepository = userRepository; } public User GetUserById(int id) { return _userRepository.GetById(id); } public List<User> GetAllUsers() { return _userRepository.GetAll(); } public void AddUser(User user) { _userRepository.Add(user); } public void UpdateUser(User user) { _userRepository.Update(user); } public void DeleteUser(int id) { _userRepository.Delete(id); } }
依賴注入配置
public class DependencyInjectionConfig { public static void Configure(IServiceCollection services, string connectionString) { // 註冊ISqlSugarClient例項 services.AddScoped<ISqlSugarClient>(_ => new SqlSugarClient(new ConnectionConfig() { ConnectionString = connectionString, DbType = DbType.SqlServer, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true })); // 註冊資料訪問層介面和實現類 services.AddScoped<IUserRepository, UserRepository>(); // 註冊業務邏輯層 services.AddScoped<UserService>(); } }
表現層示例
public class UserController : Controller { private readonly UserService _userService; public UserController(UserService userService) { _userService = userService; } public IActionResult Index() { var users = _userService.GetAllUsers(); return View(users); } public IActionResult Details(int id) { var user = _userService.GetUserById(id); if (user == null) { return NotFound(); } return View(user); } public IActionResult Create() { return View(); } [HttpPost] public IActionResult Create(User user) { if (ModelState.IsValid) { _userService.AddUser(user); return RedirectToAction("Index"); } return View(user); } public IActionResult Edit(int id) { var user = _userService.GetUserById(id); if (user == null) { return NotFound(); } return View(user); } [HttpPost] public IActionResult Edit(User user) { if (ModelState.IsValid) { _userService.UpdateUser(user); return RedirectToAction("Index"); } return View(user); } public IActionResult Delete(int id) { var user = _userService.GetUserById(id); if (user == null) { return NotFound(); } return View(user); } [HttpPost, ActionName("Delete")] public IActionResult DeleteConfirmed(int id) { _userService.DeleteUser(id); return RedirectToAction("Index"); } }
Startup配置
public class Startup { public void ConfigureServices(IServiceCollection services) { // 配置依賴注入 var connectionString = "YourConnectionStringHere"; DependencyInjectionConfig.Configure(services, connectionString); // 註冊MVC服務 services.AddControllersWithViews(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } }
這個完整示例展示瞭如何在C#中構建三層架構,包括資料訪問層、業務邏輯層和表現層。資料訪問層使用ISqlSugarClient與資料庫進行互動,業務邏輯層包含應用程式的核心業務邏輯,表現層負責與使用者互動。透過依賴注入,確保每一層之間的松耦合。