C# 策略模式和数据库访问
我是设计模式的新手,现在我想实现策略模式。这是我的代码:
namespace StrategyPattern
{
public interface ISendBehavior
{
void Send();
}
public class SendAppointment : ISendBehavior
{
public void Send()
{
// send item
}
}
public class SendTask : ISendBehavior
{
public void Send()
{
// send item
}
}
public class SendItem
{
ISendBehavior _sendBehavior;
public SendItem(ISendBehavior sendbehavior)
{
_sendBehavior = sendbehavior;
}
public void Send()
{
_sendBehavior.Send();
}
}
/* CALL */
public class Aanroep
{
public void Verzenden()
{
SendItem app = new SendItem(new SendAppointment());
app.Send();
}
}
}
在 SendAppointment 类的 Send 方法中,将发送该项目。我的问题是,我必须在这个类中连接到数据库吗?如果是这样,那么我还必须在 SendTask 中连接到数据库。但此时我正在重复自己,对吗?因此,如果连接字符串发生变化,我必须在每个类中修改它。我该如何解决这个问题?
I am new to design patterns and now I want to implement the Strategy patern. Here's my code:
namespace StrategyPattern
{
public interface ISendBehavior
{
void Send();
}
public class SendAppointment : ISendBehavior
{
public void Send()
{
// send item
}
}
public class SendTask : ISendBehavior
{
public void Send()
{
// send item
}
}
public class SendItem
{
ISendBehavior _sendBehavior;
public SendItem(ISendBehavior sendbehavior)
{
_sendBehavior = sendbehavior;
}
public void Send()
{
_sendBehavior.Send();
}
}
/* CALL */
public class Aanroep
{
public void Verzenden()
{
SendItem app = new SendItem(new SendAppointment());
app.Send();
}
}
}
In the method Send in the class SendAppointment the item will be send. My question is, do I have to connect to the database in this class? If so, then I also have to connect to the database in SendTask. But at this point I am repeating myself right? So if a connection string changes, i have to modify this in every class. How can I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为数据库操作提供另一层抽象。该层应负责集中访问所有数据库请求。连接字符串应该在外部配置,数据映射层可以直接访问它们。
存储库模式 是您可以应用的该层的良好模式。它可以位于域对象和数据映射层之间。
You could have another layer of abstraction for the database operations. This layer should be responsible for taking all the database requests centralizing the access. Connections strings should be configured externally, and the data mapping layer could access them directly.
The Repository Pattern is a good pattern for this layer that you can apply. It can sit between your domain objects and the data mapping layers.
使用另一个负责数据库连接的对象来初始化 ISendBehavior 的每个实现者怎么样?
您的 Verzenden() 实现类似于
您的 ISendBehavior.Send() 实现,
这样,您可以为任何其他类重用该 IDatabaseConnection。
How about initializing each implementor of ISendBehavior with yet another object that's responsible for the database connection?
Your Verzenden()-implementation would be something like
and your ISendBehavior.Send() would be implemented like this
This way, you can reuse that IDatabaseConnection for any other classes.
既然您不喜欢 Lennaert 将连接传递给您的类的答案,为什么不反转它并创建一个使用简单命令模式的连接类并将您的类作为参数传递给它呢?
Since you don't like Lennaert answer of passing the connection to your class why not reverse it and create a connection class that uses a simple command pattern and pass your class to it as a the parameter?