使用Unity和ASP.NET MVC进行依赖注入

时间:2009-10-18 20:30:58

标签: asp.net-mvc c#-3.0 dependency-injection unity-container

我正在尝试使用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
}

现在我得到了错误:

  

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

现在请注意,在Method DependencyInjection()中,我执行注册ITimeregistreringerRepository以输入TimeregistreringerRepository,并且 是该类属性的[Dependency]属性,所以我根本看不出我错过了什么?需要注意的另一件事是我添加了一个名为“Attribute”的属性,我在IHelloWorld和MyHelloWorld中注册了这个属性,这非常有效。

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

1 个答案:

答案 0 :(得分:0)

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