Create Service Layer from MVC
In MVC + Entity framework has certain change is that how can we introduce service layer or any code level business layer which responsible to data management with executes store procedure.
So I have introduce a service layer with is write business logic based on model.
Model Structure
In general I have added all model in one separate project and there we have add one service class for each model (some time more then one model belongs to one single service class.
Entity framework
For handle entity framework we have created one common “serviceBase” class that will be inherited by all services class, in this service class we have maintained all related common items like connection string binding and remove conversation. This class is basically belongs to “DBContext” as part of entity frame work.
Model:
public class Country { public int ID { get; set; } [StringLength (100)] public string CountryName { get; set; } [StringLength(500)] public string Remarks { get; set; } public int LanguageID { get; set; } public bool IsActive { get; set; } public virtual ICollection<Users> Users { get; set; } } }
Service class:
namespace Business.Model.Services { public class CountryService : ServiceBase { public DbSet<Country> Country { get; set; } public void InActiveContry(int id) { // write any of the logic } //public DbSet<Player> Players { get; set; } } }
Controller: