程序设计之术
工作单元模式
01实体
02数据库上下文
03仓储
04工作单元
05服务
06调用
本文档使用 MrDoc 发布
-
+
首页
06调用
```java static void Main(string[] args) { // 设置依赖注入 var serviceProvider = new ServiceCollection() .AddDbContext<AppDbContext>() .AddScoped<IProductRepository, ProductRepository>() .AddScoped<IUnitOfWork, UnitOfWork>() .AddScoped<ProductService>() .BuildServiceProvider(); using (var scope = serviceProvider.CreateScope()) { var productService = scope.ServiceProvider.GetRequiredService<ProductService>(); productService.PerformProductOperations(); DisplayAllProducts(scope.ServiceProvider.GetRequiredService<IUnitOfWork>()); } static void DisplayAllProducts(IUnitOfWork unitOfWork) { var products = unitOfWork.Products.GetAll(); foreach (var product in products) { Console.WriteLine($"Product Id: {product.Id}, Name: {product.Name}, Price: {product.Price}"); } } } ```
孙端己
2024年7月30日 10:57
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码