在这种情况下如何使用Microsoft Unity?

发布于 2024-12-13 06:40:36 字数 1572 浏览 1 评论 0原文

假设我有一个 IBookRepository 接口并由 SybaseAsaBookRepository 和 XMLBookRepository 实现。

SybaseAsaBookRepository 构造函数需要 2 个参数,即数据库用户 ID 和密码,这两个值都可以由 IBookAppConfiguration 实例检索。 XMLBookRepository 构造函数不需要参数。

显然,IBookAppConfiguration 可以轻松配置为 Microsoft Unity 进行解析,我们甚至假设它是单例。

现在,我如何配置 Microsoft Unity,将 IBookRepository 接口解析为 SybaseAsaBookRepository,并正确提供所需的 2 个构造函数参数?

示例代码如下:

public interface IBookRepository
{
    /// <summary>
    /// Open data source from Sybase ASA .db file, or an .XML file
    /// </summary>
    /// <param name="fileName">Full path of Sybase ASA .db file or .xml file</param>
    void OpenDataSource(string fileName);

    string[] GetAllBookNames(); // just return all book names in an array
}

public interface IBookAppConfiguration
{
    string GetUserID();   // assuming these values can only be determined 
    string GetPassword(); // during runtime
}

public class SybaseAsaBookRepository : IBookRepository
{
    public DatabaseAccess(string userID, string userPassword)
    {
        //...
    }
}

<register type="IBookAppConfiguration" mapTo="BookAppConfigurationImplementation">
    <lifetime type="singleton"/>
</register>

<register name="SybaseAsa" type="IBookRepository" mapTo="SybaseAsaBookRepository"> 
    <constructor> 
        <param name="userID" value="??? what shall i put it here???"/> 
        <param name="userPassword" value="??? what shall i put it here???"/> 
    </constructor> 
</register>

Suppose I have an IBookRepository interface and implemented by SybaseAsaBookRepository and XMLBookRepository.

SybaseAsaBookRepository constructor requiers 2 parameters, database user id and password, both values could be retrieved by an IBookAppConfiguration instance.
XMLBookRepository constructor needs no parameter.

Apparently IBookAppConfiguration can be easily configured for Microsoft Unity to resole, let's even assume it's a Singleton.

Now, how could I configure the Microsoft Unity, to resolve IBookRepository interface to SybaseAsaBookRepository, with the required 2 constructor parameters properly served?

Sample codes are below:

public interface IBookRepository
{
    /// <summary>
    /// Open data source from Sybase ASA .db file, or an .XML file
    /// </summary>
    /// <param name="fileName">Full path of Sybase ASA .db file or .xml file</param>
    void OpenDataSource(string fileName);

    string[] GetAllBookNames(); // just return all book names in an array
}

public interface IBookAppConfiguration
{
    string GetUserID();   // assuming these values can only be determined 
    string GetPassword(); // during runtime
}

public class SybaseAsaBookRepository : IBookRepository
{
    public DatabaseAccess(string userID, string userPassword)
    {
        //...
    }
}

<register type="IBookAppConfiguration" mapTo="BookAppConfigurationImplementation">
    <lifetime type="singleton"/>
</register>

<register name="SybaseAsa" type="IBookRepository" mapTo="SybaseAsaBookRepository"> 
    <constructor> 
        <param name="userID" value="??? what shall i put it here???"/> 
        <param name="userPassword" value="??? what shall i put it here???"/> 
    </constructor> 
</register>

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

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

发布评论

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

评论(1

不交电费瞎发啥光 2024-12-20 06:40:36

您可以更改 SybaseAsaBookRepository 的参数列表以接受 IBookAppConfiguration 的实例:

public class SybaseAsaBookRepository : IBookRepository
{
    public SybaseAsaBookRepository(IBookAppConfiguration configuration)
    {
        string userID = configuration.GetUserID();
        string userPassword = configuration.GetPassword();
        ...
    }
}

您的注册必须是:

<register name="SybaseAsa" type="IBookRepository" mapTo="SybaseAsaBookRepository" /> 

这可以使用,因为 Unity 知道

You can change the parameter list of SybaseAsaBookRepository to accept an instance of IBookAppConfiguration:

public class SybaseAsaBookRepository : IBookRepository
{
    public SybaseAsaBookRepository(IBookAppConfiguration configuration)
    {
        string userID = configuration.GetUserID();
        string userPassword = configuration.GetPassword();
        ...
    }
}

Your registration has to be:

<register name="SybaseAsa" type="IBookRepository" mapTo="SybaseAsaBookRepository" /> 

This can be used because Unity knows

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