01/10/2018, 15:47
Cần giúp đỡ về tổ chức model trong asp.net mvc với entity frame work
Em xây dựng model như thế này thì có ổn không ạ. Mọi người có project asp.net mvc cho em xin để xem cách tổ chức như nào ạ. Em có lên github tìm mà sao thấy mấy project đơn giản không à
Đây là Model chung
public abstract class BaseModel
{
public DateTime CreateDate { get; set; }
public int CreateBy { get; set; }
public DateTime ModifiedDate { get; set; }
public int ModifiedBy { get; set; }
public BaseModel()
{
CreateDate = DateTime.Today;
CreateBy = 1;
ModifiedDate = DateTime.Today;
ModifiedBy = 1;
}
}
Interface các method cơ bản
interface ICRUD
{
object GetRecord(int id);
object GetAllRecord();
object Insert(object obj);
object Update(int id);
bool Delete(int id);
}
public class ProductCategory : BaseModel, ICRUD
{
public int ID { get; set; }
public string Name { get; set; }
public ICollection<Product> Products { get; set; }
public bool Delete(int id)
{
throw new NotImplementedException();
}
public object GetAllRecord()
{
throw new NotImplementedException();
}
public object GetRecord(int id)
{
throw new NotImplementedException();
}
public object Insert(object obj)
{
throw new NotImplementedException();
}
public object Update(int id)
{
throw new NotImplementedException();
}
}
public class Product : BaseModel, ICRUD
{
public int ID { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public int ProductCategoryID { get; set; }
public ProductCategory ProductCategory { get; set; }
public bool Delete(int id)
{
throw new NotImplementedException();
}
public object GetAllRecord()
{
throw new NotImplementedException();
}
public object GetRecord(int id)
{
throw new NotImplementedException();
}
public object Insert(object obj)
{
throw new NotImplementedException();
}
public object Update(int id)
{
throw new NotImplementedException();
}
}
Bài liên quan
Theo mình thấy thì chưa có vấn đề gì. vì còn tùy thuộc vào từng bài toán, từng vấn đề cụ thể thì mới có cách xây dựng các model phù hợp. Tuy nhiên thì các model cũng có cấu trúc dựa trên các bảng data trong database, nên bạn hãy tìm cách xây dựng database một cách tối ưu và phù hợp nhất. dựa vào đó thì các model trong project sẽ đc tổ chức theo đơn giản hơn.