c# – 用于数据库访问的静态类的ASP.NET MVC准则

前端之家收集整理的这篇文章主要介绍了c# – 用于数据库访问的静态类的ASP.NET MVC准则前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在ASP.NET应用程序(使用Entity Framework)中使用MVC模式的方式如下:

1)My Models文件夹包含所有EF实体以及我的viewmodel

2)我有一个Helpers文件夹,我存储为特定应用程序创建的类.

3)在我的Helpers文件夹中,我有一个名为MyHelper的静态类,它包含使用EF访问数据库方法.

  1. namespace myApp.Helpers
  2. {
  3. public static class MyHelper
  4. {
  5. public static async Task<ProductVM> GetProductAsync(int productId)
  6. {
  7. using (var context = new myEntities())
  8. {
  9. return await context.vwxProducts.Where(x => x.ProductId == productId).Select(x => new ProductVM { A = x.A,B = x.B }).FirstOrDefaultAsync();
  10. }
  11. }
  12. }
  13. }

4)我的控制器然后在必要时调用这些函数

  1. namespace myApp.Controllers
  2. {
  3. public class ProductController : Controller
  4. {
  5.  
  6. [HttpGet]
  7. public async Task<ActionResult> Index(int productId)
  8. {
  9. var productVM = await MyHelper.GetProductAsync(productId);
  10. return View(productVM);
  11. }
  12. }
  13. }

我经常在SO中遇到“不使用静态类,静态类是邪恶等”类型的注释.这适用于这种情况吗?如果是,为什么?是否有更好的“结构”我的应用程序应遵循最佳实践并避免此类陷阱?

解决方法

你不能真正使用静态类.您的实体框架上下文应该每个请求只有一个实例.您的方法在这里为每个方法实例化一个新的上下文,这将导致Entity Framework出现大量问题.

一般概念很好,但你的MyHelper类应该是普通的类.添加一个构造函数,该构造函数接受上下文的实例,然后使用DI容器将上下文注入到辅助类中,将辅助类注入控制器中.

UPDATE

帮手

  1. namespace myApp.Helpers
  2. {
  3. public class MyHelper
  4. {
  5. private readonly DbContext context;
  6.  
  7. public MyHelper(DbContext context)
  8. {
  9. this.context = context;
  10. }
  11.  
  12. public async Task<ProductVM> GetProductAsync(int productId)
  13. {
  14. return await context.vwxProducts.Where(x => x.ProductId == productId).Select(x => new ProductVM { A = x.A,B = x.B }).FirstOrDefaultAsync();
  15. }
  16. }
  17. }

调节器

  1. namespace myApp.Controllers
  2. {
  3. public class ProductController : Controller
  4. {
  5. private readonly MyHelper myHelper;
  6.  
  7. public ProductController(MyHelper myHelper)
  8. {
  9. this.myHelper = myHelper;
  10. }
  11.  
  12. [HttpGet]
  13. public async Task<ActionResult> Index(int productId)
  14. {
  15. var productVM = await myHelper.GetProductAsync(productId);
  16. return View(productVM);
  17. }
  18. }
  19. }

然后,您只需要设置一个DI容器来注入所有东西.该代码完全取决于您最终使用的容器,因此我无法真正帮助您.不过,这通常很简单.只需阅读容器的文档即可.您需要将对象的生命周期范围设置为请求.同样,它对于不同的容器是不同的,但它们都有某种请求范围.

猜你在找的C#相关文章