多层计划架构、反馈和/或建议
我正在规划一个程序的结构,并希望使用多个层。 您认为我的方法好还是您有其他建议?
// 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);
}
}
I'm planning the structure of a program and want to use several tiers.
Do you think my approach is good or do you have some other suggestions?
// 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
切勿以任何理由这样做。这是完全不能接受的。对于测试来说这是不可接受的,对于原型来说也是不可接受的。作为开发人员,无论出于何种原因这样做,都会受到应有的惩罚。
这是最好的 sql 注入。
仅仅因为这是一个 int 现在并不意味着您不会将其更改为字符串,或者意识到您需要一些其他参数然后加入字符串。
您必须使用参数。
NEVER EVER DO THIS FOR ANY REASON. This is completely unacceptable. It is not acceptable for testing, it is not acceptable for a prototype. Doing this for any reason as a developer is chop your hand off punishment worthy.
This is sql injection at it's finest.
Just because this is an int now doesn't mean you won't change it to a string, or realize you need some other parameter then join in the string.
You MUST use parameters.
1)第一个问题 - 绑定耦合。
FormCustomer
到CustomerModel
StorageLayer
、Customer
Customer
、SqlMethods
2)第二 - 尝试使用泛型,因此每次您需要一个新实体以及 Customer 时,喜欢帐户等您至少可以重用基础设施的主要部分。
TODO:考虑像将查询与实体解耦这样的设计(也许有时它不会是 SQL 查询?)
1) First problem - tied coupling.
FormCustomer
toCustomerModel
StorageLayer
,Customer
Customer
,SqlMethods
2) Second - try to use generics, so each time you need a new entity along with Customer, like Account, etc you can reuse at least a major part of infrastructure.
TODO: Consider design like to decouple queries from entities (perhaps sometime it woudl not be an SQL queries?)