使用StructureMap对具有多个数据库字符串的DAL进行依赖注入 [英] Dependency Injection for DAL with multiple database strings using StructureMap

查看:84
本文介绍了使用StructureMap对具有多个数据库字符串的DAL进行依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,将我的业务和DAL层都使用Structuremap for DI。到目前为止,我一直在针对每个环境使用一个DAL。因此,我将从配置中获取它,并将该值用于所有连接。

I have an application that uses Structuremap for DI for both my business and DAL layer. Up to this point, I have had a single DAL per environment that I have been working on. So I would grab it from the config and use that value for all my connections. An example of this is.

 using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ToString()))
{
    //Do a call to db here.
}

我使用以下结构图调用此方法。

I am calling this method using structure map as follows.

 ObjectFactory.GetInstance<IDALManager>().MethodName();

现在我有一个新功能,我想允许用户在开发环境中进行更改,然后按一个按钮将其提升到测试或生产环境。因此,我对DAL管理器的连接字符串将需要进行更改。我还想将所有连接字符串访问权限保留在DAL中,而不是在其他层中。我正在寻找有关如何执行此操作的建议,或者正在寻找哪种设计模式的建议。

Now I have a new feature where I want to allow the users to make change in a dev environment and then push a button to elevate it to test or prod environment. Therefore my connectionstring for the DAL manager will need to be able to change. I also would like to keep all the connection string access in the DAL and not in the other layers. I am looking for advice on how to do this or what design patterns to look into for this.

更新的信息
用户将确定需要使用哪个连接字符串使用。例如,他们将数据从开发人员转移到测试中,他们将选择源和目标。

UPDATED INFORMATION The user will determine which connection string needs to be used. For example, they will be moving data from dev to test, they will select a source and a destination.

string source = \\user selection from combobox.
if (source == "DEV")
{
     //Instantiate dev instance of manager
}
if (source == "TEST")
{ 
     //Instantiate Test Instance of manager.
}


推荐答案

您需要抽象工厂。请查看此问题中的答案,以了解一些示例。

You need an abstract factory. Take a look at the answer in this question for some examples.

在您的特定情况下,抽象工厂界面应如下所示:

In your particular case, your abstract factory interface should look like this:

public interface IDALManagerFactory
{
    IDALManager Create(string environment);
}

您需要创建此接口的实现,以创建 DAL管理器

You need to create an implementation of this interface that creates a "DAL Manager" with the appropriate connection string.

要执行此操作,需要将连接字符串注入到类的构造函数中,如下所示:

To be able to do this, you need the connection string to be injected into the constructor of your class like this:

public class MyDalManager: IDALManager
{
    private readonly string connectionString;

    public MyDalManager(string connectionString)
    {
        this.connectionString = connectionString;
    }

    public MyMethod()
    {
        //..

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            //Do a call to db here.
        }
    }
}

现在执行工厂会看起来像这样:

Now the implementation of the factory would look something like this:

public class DALManagerFactory : IDALManagerFactory
{
    public IDALManager Create(string environment)
    {
         if(environment == "DEV")
             return new MyDalManager(
                 ConfigurationManager.ConnectionStrings["Database"].ToString());
         //...
    }
}

此工厂该类应位于组合根中。您还可以访问此工厂类中的容器以创建 DAL管理器。

This factory class should live in the Composition Root. You can also access the container inside this factory class to create the "DAL Manager".

现在,需要访问相应 DAL管理器的类应具有一个 IDALManagerFactory 注入到其构造函数中,它将使用该工厂通过调用 IDALManager > Create 方法传递环境名称。

Now, the class that needs access to the appropriate "DAL Manager" should have a IDALManagerFactory injected into its constructor, and it would use such factory to create a IDALManager by invoking the Create method passing the environment name.

请注意,在您的代码中,您正在访问DAL层中的连接字符串。您确实应该只在组合根目录中访问此类信息。

Please note that in your code, you are accessing the connection string in the DAL layer. You really should access such information in the composition root only.

这篇关于使用StructureMap对具有多个数据库字符串的DAL进行依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆