views:

635

answers:

1

Hey folks,

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!

A: 

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.

CodeMonkey
Could you please explain more what was the problem? Because, Now I'm facing the same exception. Thanks...
Ravi
My problem was that I looked at the exception in Visual Studio. If I let the program continue running I got some much better exception information.
CodeMonkey