CSharp: Unit of Work Pattern in donet core 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuUnitOfWork.Domain { /// <summary> /// /// </summary> public abstract class Entity { /// <summary> /// /// </summary> public int Id { get ; init; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuUnitOfWork.Domain { /// <summary> /// 工作单元模式 Unit of Work Pattern /// </summary> public class Order : Entity { /// <summary> /// /// </summary> public string Description { get ; set ; } = string .Empty; /// <summary> /// /// </summary> public string DeliveryAddress { get ; set ; } = string .Empty; /// <summary> /// /// </summary> public decimal Price { get ; set ; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuUnitOfWork.Domain { /// <summary> /// 工作单元模式 Unit of Work Pattern /// </summary> public class Customer : Entity { /// <summary> /// /// </summary> public string FirstName { get ; set ; } = string .Empty; /// <summary> /// /// </summary> public string LastName { get ; set ; } = string .Empty; } } using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Geovin.Du.DuUnitOfWork.Domain; namespace Geovin.Du.DuUnitOfWork.DuExample.Infrastructure.Repositories.Contracts { public interface IRepositoryDu<T> where T : Entity { T Add(T entity); IEnumerable<T> GetAll(); IEnumerable<T> Get(Expression<Func<T, bool >> predicate); T GetById( int id); void Delete(T entity); } } using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Geovin.Du.DuUnitOfWork.DuExample.Infrastructure.Repositories.Contracts; using Geovin.Du.DuUnitOfWork.Domain; namespace Geovin.Du.DuUnitOfWork.DuExample.Infrastructure.Repositories { /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> public class RepositoryDu<T> : IRepositoryDu<T> where T : Entity { protected OrderManagementContextDu _context; public RepositoryDu(OrderManagementContextDu context) { _context = context; } public virtual T Add(T entity) => _context .Add(entity) .Entity; public virtual IEnumerable<T> GetAll() => _context.Set<T>().ToList(); public virtual IEnumerable<T> Get(Expression<Func<T, bool >> predicate) => _context.Set<T>() .AsQueryable() .Where(predicate) .ToList(); public virtual T GetById( int id) { var entity = _context.Find<T>(id); return entity ?? throw new ArgumentException($ "Entity with {id} doesn't exists" ); } public virtual void Delete(T entity) => _context.Remove(entity); } } using Geovin.Du.DuUnitOfWork.DuExample.Infrastructure.Repositories.Contracts; using Geovin.Du.DuUnitOfWork.Domain; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuUnitOfWork.DuExample.Infrastructure { /// <summary> /// /// </summary> public interface IUnitOfWorkDu { IRepositoryDu<Customer> CustomerRepository { get ; } IRepositoryDu<Order> OrderRepository { get ; } void SaveChanges(); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Geovin.Du.DuUnitOfWork.Domain; namespace Geovin.Du.DuUnitOfWork.DuExample.Infrastructure { public class OrderManagementContextDu : DbContext { public DbSet<Customer> Customers => Set<Customer>(); public DbSet<Order> Orders => Set<Order>(); protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlite( "Data Source=storage1.db" ); } } using Geovin.Du.DuUnitOfWork.DuExample.Infrastructure.Repositories; using Geovin.Du.DuUnitOfWork.DuExample.Infrastructure.Repositories.Contracts; using Geovin.Du.DuUnitOfWork.Domain; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuUnitOfWork.DuExample.Infrastructure { /// <summary> /// /// </summary> public class UnitOfWorkDu : IUnitOfWorkDu { private readonly OrderManagementContextDu _context; private IRepositoryDu<Customer>? _customerRepository; private IRepositoryDu<Order>? _orderRepository; public UnitOfWorkDu(OrderManagementContextDu context) { _context = context; } public IRepositoryDu<Customer> CustomerRepository { get { if (_customerRepository == null ) { _customerRepository = new RepositoryDu<Customer>(_context); } return _customerRepository; } } public IRepositoryDu<Order> OrderRepository { get { if (_orderRepository == null ) { _orderRepository = new RepositoryDu<Order>(_context); } return _orderRepository; } } public void SaveChanges() => _context.SaveChanges(); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Geovin.Du.DuUnitOfWork.Domain; using Geovin.Du.DuUnitOfWork.DuExample.Infrastructure; namespace Geovin.Du.DuUnitOfWork.DuExample.Controllers { /// <summary> /// /// </summary> public class CustomerControllerDu { private readonly IUnitOfWorkDu _unitOfWork; public CustomerControllerDu(IUnitOfWorkDu unitOfWork) { _unitOfWork = unitOfWork; } public IEnumerable<Customer> GetAll() => _unitOfWork.CustomerRepository.GetAll(); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Geovin.Du.DuUnitOfWork.Domain; using Geovin.Du.DuUnitOfWork.DuExample.Infrastructure; namespace Geovin.Du.DuUnitOfWork.DuExample.Controllers { /// <summary> /// /// </summary> public class OrderControllerDu { private readonly IUnitOfWorkDu _unitOfWork; public OrderControllerDu(IUnitOfWorkDu unitOfWork) { _unitOfWork = unitOfWork; } public Order Create( int id, string description, string deliveryAddress, decimal price) { // ID is usually auto-generated by database. var newOrder = new Order { Id = id, Description = description, DeliveryAddress = deliveryAddress, Price = price, }; newOrder = _unitOfWork.OrderRepository.Add(newOrder); // _unitOfWork.CustomerRepository.Add(new Customer() { Id = 33 }); // In this example we work only with order repository, // but it would be possible to create/update different entities by using // different repositories and save all changes via unit of work instance. _unitOfWork.SaveChanges(); return newOrder; } public IEnumerable<Order> GetAll() => _unitOfWork.OrderRepository.GetAll(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | /// <summary> /// /// </summary> public static class DuExampleExecutor { /// <summary> /// /// </summary> public static void Execute() { ConsoleExtension.WriteSeparator( "工作单元模式 Unit of Work Pattern- demo" ); InitializeDatabase(); using var context = new OrderManagementContextDu(); var unitOfWork = new UnitOfWorkDu(context); var customerController = new CustomerControllerDu(unitOfWork); var orderController = new OrderControllerDu(unitOfWork); ShowAllCustomers(); ShowAllOrders(); CreateNewOrder(); ShowAllOrders(); void ShowAllCustomers() { Console.WriteLine( "显示所有客户..." ); foreach ( var customer in customerController.GetAll()) { Console.WriteLine($ "{customer.FirstName} {customer.LastName}" ); } } void ShowAllOrders() { Console.WriteLine( "\n显示所有订单..." ); foreach ( var order in orderController.GetAll()) { Console.WriteLine($ "{order.Description} 价格为: {order.Price:C}" ); } } void CreateNewOrder() { Console.WriteLine( "\n创建新订单..." ); orderController.Create(3, "游船 5" , "北京 3" , 9800); } } /// <summary> /// /// </summary> private static void InitializeDatabase() { using var context = new OrderManagementContextDu(); context.Database.EnsureDeleted(); context.Database.EnsureCreated(); var du = new Customer { Id = 1, FirstName = "Du" , LastName = "Geovin" }; var tu = new Customer { Id = 2, FirstName = "Tu" , LastName = "Juwen" }; var camera = new Order { Id = 1, Description = "摄像机" , DeliveryAddress = "深圳 1" , Price = 880 }; var phone = new Order { Id = 2, Description = "手机" , DeliveryAddress = "上海 2" , Price = 210 }; var unitOfWork = new UnitOfWorkDu(context); unitOfWork.CustomerRepository.Add(du); unitOfWork.CustomerRepository.Add(tu); unitOfWork.OrderRepository.Add(camera); unitOfWork.OrderRepository.Add(phone); unitOfWork.SaveChanges(); } } } |
调用:
1 | Geovin.Du.DuUnitOfWork.DuExample.DuExampleExecutor.Execute(); |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 工作单元模式 Unit of Work Pattern- demo -------------------------------------------------- 显示所有客户... Du Geovin Tu Juwen 显示所有订单... 摄像机 价格为: ¥880.00 手机 价格为: ¥210.00 创建新订单... 显示所有订单... 摄像机 价格为: ¥880.00 手机 价格为: ¥210.00 游船 5 价格为: ¥9800.00 |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
数值算法
, CSharp code
标签:
desgin patterns
, 设计模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2016-01-08 How to get the query string by javascript?