1
我在我的應用程序中有以下的邏輯分離,你可以看到我有一個名爲SimpleController的類,它用於導入和查找SimpleEntities。我的商業邏輯應該去哪裏?
我將所有SimpleEntities讀入內存列表因爲我經常搜索這些實體,並且每當我想要搜索一個實體時從數據庫中讀取的速度會更快。
將SimpleEntities讀入並存儲到SimpleLogic類而不是SimpleController類中的邏輯會更好嗎?
public class SimpleEntity
{
public int SimpleId { get; set; }
public string SimpleName { get; set; }
}
public class SimpleDAL
{
public ICollection<SimpleEntity> GetAllSimpleEntities()
{
//Retrieve SimpleEntities from Database
}
public void InsertSimpleEntity(SimpleEntity simpleEntity)
{
//Insert simple Entity into Database
}
}
public class SimpleLogic
{
private readonly SimpleDAL simpleDAL = new SimpleDAL();
public ICollection<SimpleEntity> GetAllSimpleEntities()
{
return simpleDAL.GetAllSimpleEntities();
}
public void InsertSimpleEntity(SimpleEntity simpleEntity)
{
//Validate simpleEntity before adding to database
if (simpleEntity.SimpleId <= 0)
throw new Exception("Invalid SimpleEntity Id: " + simpleEntity.SimpleId);
if (String.IsNullOrEmpty(simpleEntity.SimpleName))
throw new Exception("SimpleEntity Name cannot be empty or null");
simpleDAL.InsertSimpleEntity(simpleEntity);
}
}
public class SimpleController
{
private readonly SimpleLogic simpleLogic = new SimpleLogic();
private List<SimpleEntity> simpleEntities;
public SimpleController()
{
simpleEntities = simpleLogic.GetAllSimpleEntities().ToList();
}
public int FindSimpleIndex(int simpleId)
{
return simpleEntities.FindIndex(p=> p.SimpleId == simpleId);
}
public void ImportOtherSimpleEntity(OtherSimpleEntity otherSimpleEntity)
{
if (otherSimpleEntity.Operation == "Update")
{
int index = FindSimpleIndex(otherSimpleEntity.OtherSimpleId);
//If entity was found update SimpleEntity
if (index > -1)
{
//Call SimpleLogic.UpdateSimpleEntity(Pass in SimpleEntity);
}
}
}
}
好吧,我同意SimpleEntityManager方面。我也關於如何將實體驗證邏輯轉移到它自己的實體中,但是然後邏輯類似乎沒有真正的用途,它只是調用DAL並且似乎有點浪費。 – Jethro 2011-02-17 10:45:30