Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

EF Code First - Using DbContext in Service Layer - Help Needed

$
0
0

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:

 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;

        #region Contructor
        public UnitOfWork()
        {
            context = new EFDbContext();
        }
        #endregion

        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..
    }

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
        }
    }

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
}

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.




 

 


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>