Unity 框架可以在与给定接口不同的命名空间中实例化类吗?
我有一个接口:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用别名来调用命名空间作为参数之一。
但是,为什么要将存储库接口放在模型名称空间中?作为第一次查看该代码的开发人员,您是否期望在 Model 命名空间或 Repository 命名空间中找到 IRepository 接口?
Try using aliases which call for the namespace as one of the parameters.
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?
我假设您有统一参考,那么您需要定义您的部分名称及其库。像这样:
I'm assuming you a have unity reference, then you need to define your section name and its library. like so: