Unity 框架可以在与给定接口不同的命名空间中实例化类吗?

发布于 2024-10-29 10:56:10 字数 1845 浏览 2 评论 0原文

我有一个接口:

namespace IF.Model
{
    public interface IAllocationGroupRepository
    {
    }
}

和一个实现该接口的类:

using IF.Model;
namespace IF.Repository
{
    public class AllocationGroupRepository : IAllocationGroupRepository
    {
    }
}

在 Unity Framework 调用中,我可以在它们的代码中使用 .RegisterType() :

 IUnityContainer container = new UnityContainer();
 container.RegisterType<IAllocationItemRepository, AllocationItemRepository>();
 IAllocationItemRepository _allocationItemRepository = container.Resolve<IAllocationItemRepository>();

并且 .Resolve() 可以工作并为我提供一个新的 AllocationItemRepository 对象。

但是,当我尝试调用 Resolve 并且映射存在于 app.config 文件中时,我收到此错误:

“当前类型 IF.Model.IAllocationItemRepository 是一个接口,无法构造。您是否缺少类型映射? ”

我的 app.config 文件如下所示:

    <unity>
        <containers>
            <container>
                <types>
                    <type
                        type="IF.Model.IAllocationGroupRepository, IF.Model"
                        mapTo="IF.Repository.AllocationGroupRepository, IF.Repository" />
                </types>
            </container>
        </containers>
    </unity>

下面是尝试使用 App.config 文件中的内容调用 .Resolve() 的代码:

IUnityContainer container = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);
IAllocationItemRepository _allocationItemRepository = container.Resolve<IAllocationItemRepository>();

如您所见,这是非常基本的东西。给定一个接口,将其解析为类。它在内联执行此操作时有效,但在尝试从 app.config 文件执行此操作时失败。

我在这里缺少什么?

谢谢, 麦克风

I have an interface:

namespace IF.Model
{
    public interface IAllocationGroupRepository
    {
    }
}

and a class the implements that interface:

using IF.Model;
namespace IF.Repository
{
    public class AllocationGroupRepository : IAllocationGroupRepository
    {
    }
}

In a Unity Framework call, I can .RegisterType() in the code for both of them:

 IUnityContainer container = new UnityContainer();
 container.RegisterType<IAllocationItemRepository, AllocationItemRepository>();
 IAllocationItemRepository _allocationItemRepository = container.Resolve<IAllocationItemRepository>();

and .Resolve() works and gives me a new AllocationItemRepository object.

BUT, when I try to call Resolve and the mapping lives in the app.config file, I get this error:

"The current type, IF.Model.IAllocationItemRepository, is an interface and cannot be constructed. Are you missing a type mapping?"

Here is what my app.config file looks like:

    <unity>
        <containers>
            <container>
                <types>
                    <type
                        type="IF.Model.IAllocationGroupRepository, IF.Model"
                        mapTo="IF.Repository.AllocationGroupRepository, IF.Repository" />
                </types>
            </container>
        </containers>
    </unity>

here is what the code looks like trying to call .Resolve() using what's in the App.config file:

IUnityContainer container = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);
IAllocationItemRepository _allocationItemRepository = container.Resolve<IAllocationItemRepository>();

as you can see, this is pretty basic stuff. Given an interface, resolve it to the class. It works when doing it inline, but fails when trying to do it from the app.config file.

What am I missing here?

Thanks,
Mike

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

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

发布评论

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

评论(2

む无字情书 2024-11-05 10:56:10

尝试使用别名来调用命名空间作为参数之一。

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="IAllocationGroupRepository" type="IF.Model.IAllocationGroupRepository, IF.Model, Version=1.0.0.0, Culture=neutral"/>
    <alias alias="AllocationGroupRepository"  type="IF.Repository.AllocationGroupRepository, IF.Repository, Version=1.0.0.0, Culture=neutral"/>

    <container>
        <register type="IAllocationGroupRepository" mapTo="AllocationGroupRepository"/>
    </container>
</unity>

但是,为什么要将存储库接口放在模型名称空间中?作为第一次查看该代码的开发人员,您是否期望在 Model 命名空间或 Repository 命名空间中找到 IRepository 接口?

Try using aliases which call for the namespace as one of the parameters.

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="IAllocationGroupRepository" type="IF.Model.IAllocationGroupRepository, IF.Model, Version=1.0.0.0, Culture=neutral"/>
    <alias alias="AllocationGroupRepository"  type="IF.Repository.AllocationGroupRepository, IF.Repository, Version=1.0.0.0, Culture=neutral"/>

    <container>
        <register type="IAllocationGroupRepository" mapTo="AllocationGroupRepository"/>
    </container>
</unity>

However, why are you putting your repository interfaces in your model namespace? Would you, as a developer looking at that code for the first time, expect to find an IRepository interface in the Model namespace or the Repository namespace?

少女情怀诗 2024-11-05 10:56:10

我假设您有统一参考,那么您需要定义您的部分名称及其库。像这样:

<?xml version="1.0"?>
<configuration>
  <configSections>
 <section name="mySectionName" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <mySectionName>
    <typeAliases>
     ...

I'm assuming you a have unity reference, then you need to define your section name and its library. like so:

<?xml version="1.0"?>
<configuration>
  <configSections>
 <section name="mySectionName" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <mySectionName>
    <typeAliases>
     ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文