How to separate code (pure code separation) in ASP.NET MVC 3?

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.

+4
source share
2 answers

How to put DbContext in Data well?

Yes, where it belongs.

Now in the controller I need to write code

No, you absolutely should not write such code in the controller, because now you make your controller strongly connected with the data access technology you use (EF in your case). Even worse, you cannot unit test disable your controllers.

I would recommend you to abstract operations on objects in the interface (you already mentioned this, by the way, in your question - IRepository ). And now your controller can use the repository as a dependency:

 public class CountriesController: Controller { private readonly IRepository repository; public CountriesController(IRepository repository) { this.repository = repository; } public ActionResult Index(int id) { Country country = this.repository.Get<Country>(id); return View(country); } [HttpPost] public ActionResult Index(Country country) { if (ModelState.IsValid) { this.repository.Insert(country); return RedirectToAction("Success"); } return View(country); } } 

Now all you have to do is set up your favorite dependency injection infrastructure to inject a specific implementation of this IRepository into the controller constructor. In your case, this particular implementation may be some class that implements the IRepository interface and which uses ApplicationDbContext internally.

This way you abstract the data access logic from your controllers.

+1
source

u can do a separate project for each other in BLL to create a class for each form of the business object call repository and create some basic function that you need, for example, insert delete find select with parameter, etc.

 var country =new Country();//it class of BLL if (ModelState.IsValid) { country.insert(country); } 

something like that

+2
source

All Articles