使用 Unity 注入 DAAB 数据库对象

发布于 2024-10-19 17:00:46 字数 137 浏览 7 评论 0原文

我正在尝试创建一个 DAO 对象,该对象采用对数据访问应用程序块的数据库对象的构造函数依赖项。如何设置 .config 文件以使 Unity 解决依赖关系?我尝试映射数据库类型来创建数据库对象,但无法使其工作。

还有其他人尝试过做这样的事情吗?

I'm trying to create a DAO object that takes a constructor dependency to the Data Access Application Block's Database object. How do I setup my .config file to have Unity resolve the dependency? I've tried mapping the Database type to create a Database object, but couldn't get it to work.

Has anyone else tried doing anything like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

莳間冲淡了誓言ζ 2024-10-26 17:00:46

只是一个开始提示,添加您对问题所做的代码示例。它告诉我们您已经走了多远,以及至少您正在使用什么语言。我将假设使用 c#、EL V5 并从头开始:

首先让我们回顾一下依赖于数据库应用程序块的类的标准构造函数:

private Microsoft.Practices.EnterpriseLibrary.Data.Database database;

public TestSQLConnection(Microsoft.Practices.EnterpriseLibrary.Data.Database injectedDatabase)
{
    database = injectedDatabase;
}

以下是调用创建者的方法:

TestSQLConnection testSQLConnection =
    Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.Current.GetInstance<TestSQLConnection>(); 

简而言之,您可以使用依赖注入,忘记如何实现要创建它,只需请求它的一个实例,库就会完成其余的工作。

创建配置的最简单方法是使用 Enterprise Library V5 配置工具,因为它创建了必要的部分,我建议您“阅读精细手册”。

如果您手动执行此操作,则以下是您需要的部分:

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <!-- Tell EL what sections to use-->
        <configSections>
           <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        </configSections>
        <!-- And use them -->
        <dataConfiguration defaultDatabase="MyDatabase" />
        <!-- This is just a plain connection string-->
        <connectionStrings>
            <add name="MyDatabase" connectionString="Data Source=DUMMY;Initial Catalog=default;Integrated Security=True" providerName="System.Data.SqlClient" />
        </connectionStrings>
    </configuration>

Just a starting hint, add a sample of the code you've done to your question. It tells us how far you've got, and at least what language you are using. I'll assume c#, EL V5 and starting from scratch:

Firstly let's review a standard constructor for a class that depends on the database application block:

private Microsoft.Practices.EnterpriseLibrary.Data.Database database;

public TestSQLConnection(Microsoft.Practices.EnterpriseLibrary.Data.Database injectedDatabase)
{
    database = injectedDatabase;
}

and here's how to call the creator:

TestSQLConnection testSQLConnection =
    Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.Current.GetInstance<TestSQLConnection>(); 

So in short there's dependency injection for you, forget about how to create it, just ask for an instance of it and the library does the rest.

The easiest way to create the config is to use the Enterprise Library V5 Configuration Tool, as it creates the necessary parts, I advise you to 'Read The Fine Manual'.

If you are doing it manually here are the parts you need:

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <!-- Tell EL what sections to use-->
        <configSections>
           <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        </configSections>
        <!-- And use them -->
        <dataConfiguration defaultDatabase="MyDatabase" />
        <!-- This is just a plain connection string-->
        <connectionStrings>
            <add name="MyDatabase" connectionString="Data Source=DUMMY;Initial Catalog=default;Integrated Security=True" providerName="System.Data.SqlClient" />
        </connectionStrings>
    </configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文