C# 策略模式和数据库访问

发布于 2024-08-06 00:40:16 字数 1039 浏览 2 评论 0原文

我是设计模式的新手,现在我想实现策略模式。这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遮了一弯 2024-08-13 00:40:16

您可以为数据库操作提供另一层抽象。该层应负责集中访问所有数据库请求。连接字符串应该在外部配置,数据映射层可以直接访问它们。

存储库模式 是您可以应用的该层的良好模式。它可以位于域对象和数据映射层之间。

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.

浮光之海 2024-08-13 00:40:16

使用另一个负责数据库连接的对象来初始化 ISendBehavior 的每个实现者怎么样?

您的 Verzenden() 实现类似于

IDatabaseConnection connection = new DatabaseConnection();

SendItem app = new SendItem( new SendAppointment( connection ) );

您的 ISendBehavior.Send() 实现,

_databaseConnection.Send( ... ); // fill behavior-specific information here (perhaps with properties)

这样,您可以为任何其他类重用该 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

IDatabaseConnection connection = new DatabaseConnection();

SendItem app = new SendItem( new SendAppointment( connection ) );

and your ISendBehavior.Send() would be implemented like this

_databaseConnection.Send( ... ); // fill behavior-specific information here (perhaps with properties)

This way, you can reuse that IDatabaseConnection for any other classes.

方觉久 2024-08-13 00:40:16

既然您不喜欢 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文