I just want to know how to correctly separate code in MVC3 using EF
In accordance with my project structure.
Presentation → View and Controller
Domain → Model (business object)
Data -> RepositoryBase, IRepository, ApplicationDbContext
Services → third-party service (PayPal, SMS) or application service
ApplicationDbContext requires the model to be a reference.
public sealed class ApplicationDbContext : DbContext { public DbSet<CountryModel> Country { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } }
1. Is it good to place DbContext in Data? Or do I need to move it to a domain?
Now in the controller I need to write code
using (ApplicationDbContext db = new ApplicationDbContext()) { var countryRepository = new Repository<Country>(db); if (ModelState.IsValid) { countryRepository.insert(country); db.SaveChanges(); } }
Is there a way to split this code at any business / service level?
So, we just call this layer from the controller and just transfer the specific business object to complete the rest of the operation.
I want to do PL -> BLL -> DLL approach Using MVC 3 and EF?
Please suggest me the right way.