Unity.Mvc3 解析 Interface 类型

发布于 2024-12-04 00:29:22 字数 1544 浏览 3 评论 0 原文

我在我的项目中使用 Unity.Mvc3,它嵌入了 UnityDependencyResolver

var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));

BuildUnityContainer 方法看起来像:

private static IUnityContainer BuildUnityContainer()
{
    var container = new UnityContainer()
        .RegisterType<IControllerActivator, HControllerActivator>()
        .RegisterType<IDatabaseFactory, DatabaseFactory>(
            new HierarchicalLifetimeManager())
        .RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager())
        .RegisterType<IRepository<Article>, HRepository<Article>>()
        .RegisterType<IRepository<ContentBase>, HRepository<ContentBase>>()
        .RegisterType<IRepository<User>, HRepository<User>>()
        .RegisterType<IRepository<Profile>, HRepository<Profile>>();

    container.RegisterControllers();

    return container;
}

但是当我尝试像这样解析我的类型时:

var users = DependencyResolver.Current.GetService<IRepository<User>>();

我收到以下异常:

依赖关系解析失败,类型 = “Data.IRepository1[Data.Models.User]”,名称 =“(无)”。例外 发生时:解决时。例外是: InvalidOperationException - 类型 HRepository1 不能是 建造的。您必须配置容器来提供此值。

我做错了什么?实际上可以使用 .RegisterType, Foo>() 吗? 也许我需要某种自定义的 DependencyResolver?

提前致谢 :)

I'm using Unity.Mvc3 in my project and it has embedded UnityDependencyResolver.

var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));

and the BuildUnityContainer method looks like:

private static IUnityContainer BuildUnityContainer()
{
    var container = new UnityContainer()
        .RegisterType<IControllerActivator, HControllerActivator>()
        .RegisterType<IDatabaseFactory, DatabaseFactory>(
            new HierarchicalLifetimeManager())
        .RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager())
        .RegisterType<IRepository<Article>, HRepository<Article>>()
        .RegisterType<IRepository<ContentBase>, HRepository<ContentBase>>()
        .RegisterType<IRepository<User>, HRepository<User>>()
        .RegisterType<IRepository<Profile>, HRepository<Profile>>();

    container.RegisterControllers();

    return container;
}

but when I try to resolve my type like this:

var users = DependencyResolver.Current.GetService<IRepository<User>>();

I get the following exception:

Resolution of the dependency failed, type =
"Data.IRepository1[Data.Models.User]", name = "(none)". Exception
occurred while: while resolving. Exception is:
InvalidOperationException - The type HRepository
1 cannot be
constructed. You must configure the container to supply this value.

What am I doing wrong? And is it actually possible to use .RegisterType<IFoo<T>, Foo<T>>()?
Maybe I need some kind of custom DependencyResolver?

Thanks in advance :)

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

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

发布评论

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

评论(1

青瓷清茶倾城歌 2024-12-11 00:29:22

你想做的事情应该是可能的。 Unity 可能不知道如何构建 HRepository

HRepository 类是什么样的?具体来说,构造函数参数是什么?它们的类型是否需要向 Unity 注册?

如果 Unity 不知道如何构造其参数,它将无法解析 HRepository

What you are trying to do do should be possible. Unity may not know how to build HRepository<T>.

What does the HRepository<T> class look like? Specifically, what are the constructor parameters and do any of their types need to be registered with Unity?

Unity will not be able to resolve HRepository<T> if it does not know how to construct its parameters.

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