I am designing a new asp.net MVC4 application. I know that context class generated by EF is Unit of work and Dbset is the repository class. I am just avoiding the extra abstraction. I want to know whether I am following the correct pattern or not.
Entity
public partial class ProductMaster
{
public long ID { get; set; }
public string ProductCode { get; set; }
public string ProductName { get; set; }
}
public partial class CompanyMaster
{
public long ID{ get; set; }
public string CompanyName { get; set; }
public string CompanyCode { get; set; }
}
Context class(Generated By EF)
public partial class SampleEntities : DbContext,IUnitOfWork
{
public SampleEntities()
: base("name=SampleEntities")
{
}
public void Commit()
{
SaveChanges();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<ProductMaster> ProductMaster { get; set; }
public DbSet<CompanyMaster> CompanyMaster { get; set; }
public DbSet<UserMaster> UserMaster{ get; set; }
public virtual ObjectResult<SP_UserMaster_Result> SP_UserMaster(Nullable<long> tM_UM_ID, string userName, string userPassword, Nullable<byte> userType)
{
var tM_UM_IDParameter = tM_UM_ID.HasValue ?
new ObjectParameter("TM_UM_ID", tM_UM_ID) :
new ObjectParameter("TM_UM_ID", typeof(long));
var userNameParameter = userName != null ?
new ObjectParameter("UserName", userName) :
new ObjectParameter("UserName", typeof(string));
var userPasswordParameter = userPassword != null ?
new ObjectParameter("UserPassword", userPassword) :
new ObjectParameter("UserPassword", typeof(string));
var userTypeParameter = userType.HasValue ?
new ObjectParameter("UserType", userType) :
new ObjectParameter("UserType", typeof(byte));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<SP_UserMaster_Result>("SP_UserMaster", tM_UM_IDParameter, userNameParameter, userPasswordParameter, userTypeParameter);
}
}
Unit of Work Interface
public Interface IUnitOfWork
{
public DbSet<ProductMaster> ProductMaster;
public DbSet<CompanyMaster> CompanyMaster;
public Dbset<UserMaster> UserMaster;
public virtual ObjectResult SP_UserMaster(Nullable<long> tM_UM_ID, string userName, string userPassword, Nullable<byte> userType){};
public void Commit();
}
Business Logic(Implementation)
public class ProductMaster
{
IUnitOfWork _unitOfWork;
public ProductMaster(IunitOfWork unitOfWork)//Injected using Ninject
{
this._unitOfWork = unitOfWork;
}
public void AddProduct(Product product)
{
_unitofWork.ProductMaster.Add(product);
_unitOfWork.Commit();
}
}
Interface of Product Logic
public class IProductMaster
{
public ProductMaster(IunitOfWork unitOfWork);
}
Product Controller
public class ProductController : Controller
{
IProductMaster _productMaster;
public ProductController(IproductMaster productMaster)
{
this._ProductMaster=productMaster;//Injecting using Ninject
}
public void SaveProduct(ProductMaster product)
{
this._productMaster.AddProduct(product.ToEntity());
}
}