EF代码优先-在服务层中使用DbContext-需要帮助 [英] EF Code First - Using DbContext in Service Layer - Help Needed

查看:96
本文介绍了EF代码优先-在服务层中使用DbContext-需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于我所有的应用程序(ASP.NET MVC3,EF代码优先),我都具有以下通用模式:

实体:

Hi,

For all my applications (ASP.NET MVC3, EF Code First), I have this general pattern that i''m using:

Entity:

public class Product 
    { 
        public int ProductID { get; set; } 
 
        public string Name { get; set; } 
 
        public string Category { get; set; } 
 
        public decimal Price { get; set; } 
    }



EFDbContext:



EFDbContext:

public class EFDbContext : DbContext 
    { 
        public DbSet<Product> Products { get; set; } 
 
        protected override void OnModelCreating(DbModelBuilder modelBuilder) 
        { 
            // Product 
            modelBuilder.Entity<Product>() 
                .HasKey(p => p.ProductID) 
                .Property(p => p.ProductID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
 
      
        } 
    }



我有一个通用存储库,将由我的工作单元类使用,如下所示:



I have a Generic Repository, which will be used by my Unit Of Work class, as below:

public class UnitOfWork : IUnitOfWork, IDisposable 
    { 
        private readonly EFDbContext context; 
        private IGenericRepository<Product> _productRepository; 
 
        
        public UnitOfWork() 
        { 
            context = new EFDbContext(); 
        } 
        
        public EFDbContext Context 
        { 
            get 
            { 
                return context; 
            } 
        } 
 
        public IGenericRepository<Product> ProductRepository 
        { 
            get 
            { 
                if (_productRepository == null) 
                { 
                    _productRepository = new 
                                         GenericRepository<Product>(context); 
                } 
                return _productRepository; 
            } 
        } 
 
       // save() 
       // Dispose() & so on.. left out



我有一个服务层,它应该对业务逻辑进行建模.该层使用工作单元,如下所示:



I have a Service Layer, which as it should, models business logic. This layer, uses Unit Of Work, as below:

public class ProductService : IProductService 
    { 
        private IUnitOfWork unitOfWork; 
 
        public ProductService(IUnitOfWork unitOfWorkParam) 
        { 
            unitOfWork = unitOfWorkParam; 
        } 
 
        public void AddProduct(Product product) 
        { 
            unitOfWork.ProductRepository.Insert(product); 
            unitOfWork.Save(); 
        } 
        // other services for business logic.. 
    }



现在,我的UI项目(ASP.NET MVC3)使用如下服务层



Now, my UI project (ASP.NET MVC3), uses the service layer as below

public class HomeController : Controller 
    { 
        private IProductService productService; 
 
        public HomeController(IProductService productServiceParam) 
        { 
            productService = productServiceParam; 
        } 
 
        public ActionResult Products() 
        { 
            // User productService.GetProducts and so on 
        } 
    }



因此,要进行汇总的项目的整个设置将是:

控制器->服务层-> UnitOfWork(包含上下文,存储库)
-> DB

现在,我的问题是,EFDbContext包含在UnitOfWork中,因此,在此类中,我们可以使用以下查询:



Thus the whole setup of the Projects to summazie would be:

Controller --> Service Layer --> UnitOfWork (which contains context, repositories)
--> DB

Now, my question is, the EFDbContext is contained in UnitOfWork, and thus, in this class, we can use queries such as:

using (var context = new EFDbContext()) 
{ 
    // use context.Entry<> 
    // use context.Find<> 
    // use context.Attach<> 
    // and other useful functions 
}



但是,如何才能将此功能迁移到使用UnitOfWork.Repositories的服务层?

我错过了设计方面的东西吗?

使服务层能够以某种方式使用裸上下文对象,这将是很棒的,其中我们可以使用有用的扩展方法,例如context. ..

谁能帮我这个忙吗?



but, how can i get this functionality migrated to my Service Layer, which uses UnitOfWork.Repositories ?

Have i missed something in design?

It would be great to have the Service Layer capable somehow of using the bare context object, where in we can use useful extension methods such as context.Entry<> ..

Can anyone please help me with this?

thanks.

推荐答案

这看起来很复杂.为什么不在类中使用注释来定义键等?例如,类上方的[Table("Product")]和ProductId上方的[Key].

在我的代码中,DbContext具有私有构造函数和公共静态实例属性,该属性在针对同一会话的请求中返回相同的实例(它使用会话密钥来执行此操作).我可以从项目的任何位置访问DBContext,但不会将其分解为dll,所以也许您的问题是在哪里使用了dll的其中之一?
This looks complex. Why don''t you use annotations on your class to define keys, etc ? For example, [Table("Product")] above the class and [Key] above the ProductId.

In my code, the DbContext has a private constructor, and a public static instance property, which returns the same instance across requests for the same session ( it uses the session key to do this ). I can access my DBContext from anywhere in my project, but I don''t break it up in to dlls, so perhaps your issue is one of what dlls are consumed where ?


这篇关于EF代码优先-在服务层中使用DbContext-需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆