我正在規劃一個程序的結構並希望使用多個層。 您認爲我的方法很好嗎?或者您有其他建議嗎?多層程序體系結構,反饋和/或建議
// The Form is the View + Controller (Windows Forms standard behaviour, don't want to change it)
class FormCustomer
{
CustomerModel _customerModel;
void LoadCustomer()
{
Customer c = _customerModel.ReadCustomer(tbCustomer.Text);
ShowCustomer(c);
}
}
// The Model-Layer is for business Logic
class CustomerModel
{
StorageLayer _StorageLayer;
public Customer ReadCustomer(int id)
{
if (id < 0) throw new Exception("Invalid id");
Customer c = _StorageLayer.ReadCustomer(id);
if (c == null) throw new Exception("Customer not found");
return c;
}
}
// The StorageLayer ist a facade to all storage Methods
// See http://en.wikipedia.org/wiki/Facade_pattern for more details
class StorageLayer
{
SqlMethods _sqlMethods;
public Customer ReadCustomer(int id)
{
return _sqlMethods.ReadCustomer(id)
}
}
// The SqlMethods is one class (or maybe several classes) which contain
// all the sql operations.
class SqlMethods
{
public Customer ReadCustomer(int id)
{
string sql = "Select c.*, a.* From customers c left join addresses a where c.id = " + id; // Not optimized, just an example
IDataReader dr = ExecuteStatement(sql);
return FetchCustomer(dr);
}
}
您對您的架構有任何具體問題嗎?你使用多層的原因是什麼?你是否理解[層和層之間的區別](http://stackoverflow.com/q/120438/310112)? –