The way to use the MVC pattern at the moment in my ASP.NET application (using Entity Framework) is as follows:
1) My Models folder contains all EF objects, as well as my ViewModels
2) I have Helpers folders where classes created for the purposes of a specific application are stored.
3) In my Helpers folder, I have a static class called MyHelper , which contains methods that access the database using EF.
namespace myApp.Helpers { public static class MyHelper { public static async Task<ProductVM> GetProductAsync(int productId) { using (var context = new myEntities()) { return await context.vwxProducts.Where(x => x.ProductId == productId).Select(x => new ProductVM { A = xA, B = xB }).FirstOrDefaultAsync(); } } } }
4). Then my controllers call these functions:
namespace myApp.Controllers { public class ProductController : Controller { [HttpGet] public async Task<ActionResult> Index(int productId) { var productVM = await MyHelper.GetProductAsync(productId); return View(productVM); } } }
Usually I come across comments in SO like "don't use a static class, static classes are evil, etc.". Will this apply in such a scenario? If so, why? Is there a better βstructureβ that my application should follow best practices and avoid such errors?
source share