Unity 和 ASP.NET MVC 的依赖注入

发布于 2024-08-07 22:51:49 字数 3631 浏览 6 评论 0原文

我正在尝试使用 Unity 作为我的 IoC 容器,这“有点”有效。

来自 global.asax 的片段:

    protected void Application_Start()
    {
        //... left out for clarity

        //setup dependency injection with Microsoft Unity
        DependencyInjection();
    }

    private void DependencyInjection()
    {
        var container = new UnityContainer();

        container.RegisterType<ITimeregEntities, TimeregEntities>()
            .Configure<InjectedMembers>()
            .ConfigureInjectionFor<TimeregEntities>(new InjectionConstructor());

        container.RegisterType<TimeregistreringerController.IHelloWorld, TimeregistreringerController.MyHelloWorld>();

        //###PROBLEM HERE###
        container.RegisterType<ITimeregistreringerRepository, TimeregistreringerRepository>();
        //###END-PROBLEM###

        var factory = new UnityControllerFactory(container);

        ControllerBuilder.Current.SetControllerFactory(factory);
    }

此依赖项添加到我的“Timeregistreringer”控制器上:

public class TimeregistreringerController : BaseController
{
    //
    // GET: /Timeregistreringer/

    public ActionResult Index()
    {
        var approved = TimeregistreringerRepository.FindApproved();
        ViewData.Model = approved;
        ViewData["HelloText"] = Attribute.SayHello();

        return View();
    }

    //###THIS WORKS
    [Dependency]
    public IHelloWorld Attribute
    {
        get;set;
    }
    //###END-THIS WORKS###


    //###PROBLEM ATTRIBUTE, Does not resolve###
    [Dependency]
    public ITimeregistreringerRepository TimeregistreringerRepository
    {
        get;
        set;
    }
    //###END-PROBLEM###

    public interface IHelloWorld
    {
        string SayHello();
    }

    public class MyHelloWorld : IHelloWorld
    {
        public string SayHello()
        {
            return "Hello, World!";
        }
    }
}

TimeregistreringerRepository 的实现如下:

public class TimeregistreringerRepository : RepositoryBase, ITimeregistreringerRepository
{
    public TimeregistreringerRepository(ITimeregEntities entities) : base(entities)
    {
    }

    public IQueryable<Timeregistreringer> FindApproved()
    {
        return _entities
            .FindAll<Timeregistreringer>()
            .Where(t => t.Godkendt == true);
    }
}

public interface ITimeregistreringerRepository : IRepository
{
    IQueryable<Timeregistreringer> FindApproved();
}

public interface IRepository
{
    void Add<T>(T t) where T : IEntityWithKey;
}

public class RepositoryBase : IRepository
{
    protected ITimeregEntities _entities;
    public RepositoryBase(ITimeregEntities entities)
    {
        _entities = entities;
    }

    #region IRepository Members

    public IQueryable<T> FindAll<T>()
    {
        return _entities.FindAll<T>();
    }

    public void Add<T>(T t) where T : IEntityWithKey
    {
       _entities.Add<T>(t);
    }

    #endregion
}

现在针对我得到的错误:

依赖关系解析失败,类型=“[被我隐藏].MVCWeb.Controllers.TimeregistreringerController”,名称=“”。异常消息是:当前构建操作(构建密钥 Build Key[[被我隐藏].MVCWeb.Controllers.TimeregistreringerController,null])失败:无法解析属性“TimeregistreringerRepository”的值。 (策略类型 BuildPlanStrategy,索引 3)

现在请注意,在方法 DependencyInjection() 中,我将 ITimeregistreringerRepository 注册为类型 TimeregistreringerRepository,并且该类有一个 [Dependency] 属性- 属性,所以我根本看不到我错过了什么?另一件需要注意的事情是,我添加了一个名为“Attribute”的附加属性,我已将其注册到 IHelloWorld 和 MyHelloWorld,并且效果非常好。

感谢您花时间阅读所有这些:-)期待您的想法和建议!

I'm attempting to use Unity as my IoC container, which "kinda" works.

Snip from global.asax:

    protected void Application_Start()
    {
        //... left out for clarity

        //setup dependency injection with Microsoft Unity
        DependencyInjection();
    }

    private void DependencyInjection()
    {
        var container = new UnityContainer();

        container.RegisterType<ITimeregEntities, TimeregEntities>()
            .Configure<InjectedMembers>()
            .ConfigureInjectionFor<TimeregEntities>(new InjectionConstructor());

        container.RegisterType<TimeregistreringerController.IHelloWorld, TimeregistreringerController.MyHelloWorld>();

        //###PROBLEM HERE###
        container.RegisterType<ITimeregistreringerRepository, TimeregistreringerRepository>();
        //###END-PROBLEM###

        var factory = new UnityControllerFactory(container);

        ControllerBuilder.Current.SetControllerFactory(factory);
    }

This dependency is added on my 'Timeregistreringer'-Controller:

public class TimeregistreringerController : BaseController
{
    //
    // GET: /Timeregistreringer/

    public ActionResult Index()
    {
        var approved = TimeregistreringerRepository.FindApproved();
        ViewData.Model = approved;
        ViewData["HelloText"] = Attribute.SayHello();

        return View();
    }

    //###THIS WORKS
    [Dependency]
    public IHelloWorld Attribute
    {
        get;set;
    }
    //###END-THIS WORKS###


    //###PROBLEM ATTRIBUTE, Does not resolve###
    [Dependency]
    public ITimeregistreringerRepository TimeregistreringerRepository
    {
        get;
        set;
    }
    //###END-PROBLEM###

    public interface IHelloWorld
    {
        string SayHello();
    }

    public class MyHelloWorld : IHelloWorld
    {
        public string SayHello()
        {
            return "Hello, World!";
        }
    }
}

The implementation for TimeregistreringerRepository are as follows:

public class TimeregistreringerRepository : RepositoryBase, ITimeregistreringerRepository
{
    public TimeregistreringerRepository(ITimeregEntities entities) : base(entities)
    {
    }

    public IQueryable<Timeregistreringer> FindApproved()
    {
        return _entities
            .FindAll<Timeregistreringer>()
            .Where(t => t.Godkendt == true);
    }
}

public interface ITimeregistreringerRepository : IRepository
{
    IQueryable<Timeregistreringer> FindApproved();
}

public interface IRepository
{
    void Add<T>(T t) where T : IEntityWithKey;
}

public class RepositoryBase : IRepository
{
    protected ITimeregEntities _entities;
    public RepositoryBase(ITimeregEntities entities)
    {
        _entities = entities;
    }

    #region IRepository Members

    public IQueryable<T> FindAll<T>()
    {
        return _entities.FindAll<T>();
    }

    public void Add<T>(T t) where T : IEntityWithKey
    {
       _entities.Add<T>(t);
    }

    #endregion
}

Now for the error I get:

Resolution of the dependency failed, type = "[hidden by me].MVCWeb.Controllers.TimeregistreringerController", name = "". Exception message is: The current build operation (build key Build Key[[hidden by me].MVCWeb.Controllers.TimeregistreringerController, null]) failed: The value for the property "TimeregistreringerRepository" could not be resolved. (Strategy type BuildPlanStrategy, index 3)

Now notice that in the Method DependencyInjection() I do register ITimeregistreringerRepository to type TimeregistreringerRepository, and there is a [Dependency] attribute on that class-attribute, so I simply can't see what I am missing? Another thing to notice is that I have added an additional Attribute called "Attribute" which I have registered with IHelloWorld and MyHelloWorld, and this works perfectly.

Thank you for taking your time reading all of this :-) Looking forward to your ideas and suggestions!

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

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

发布评论

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

评论(1

情绪失控 2024-08-14 22:51:49

我的问题源于代码中的其他地方,但已包含在其他几个异常中,因此很难找到问题所在。在这种情况下描述这个问题并不有趣。

My problem stemmed from somewhere else in the code, but had been wrapped in several other exceptions, so it was very hard to locate the problem. Describing the issue is not interesting in this context.

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