I am trying to setup IoC container in my Asp.Net MVC 5 application so I can access these objects anywhere in my application.
I choose to use Unity.Mvc container for the job.
Now, I have a class that looks like this
using Project1.Foundation.Contracts;
using Project2.Respositories.Contracts.UnitsOfWork;
using Unity.Attributes;
namespace Project2.Presenters.Bases
{
public class BasePresenter
{
[Dependency]
public IUserPassport Passport { get; set; }
[Dependency]
public IUnitOfWork UnitOfWork { get; set; }
}
}
In Project2.dll
I added the [Dependency]
attribute over both properties that I need to inject as you can see above. But when the application run, both properties are null and not set to an instance.
I also tried to use InjectionProperty
instead of having to add [Dependency]
like so
container.RegisterType<BasePresenter>(new InjectionProperty("Passport", new ResolvedArrayParameter<IUserPassport>()),
new InjectionProperty("UnitOfWork", new ResolvedArrayParameter<IUnitOfWork>()));
However, I am still getting the same results, when the class is constructed like so new ChildPresenter()
both properties are still null.
Background
Here is how I register the needed types, from a different assembly (Project1.dll), In the UnityConfig
class, I have the following method.
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IUnitOfWork, UnitOfWork>(new ContainerControlledLifetimeManager());
container.RegisterInstance<IPrincipal>(Thread.CurrentPrincipal);
container.RegisterType<IUserPassport, UserPassport>(new ContainerControlledLifetimeManager());
}
In another words, I create the IoC in one assembly and I want to use it in a different assembly Not really sure if it matters, I just thought it should be part of the question for celerity.
How can I correctly inject the property using Unity.Mvc
IoC container?
Response To comments below
Question from comment below: Why can't I just pass the parameters in the constructor?
If I do that my code will look like this
public class ConsumerController
{
protected IUserPasspoer Passport { get; set; }
protected IUnitOfWork UnitOfWork { get; set; }
public ConsumerController(IUserPasspoer passport, IUnitOfWork unitOfWork)
{
Passport = passport;
UnitOfWork = unitOfWork;
}
public ActionResult Index(int? id)
{
var presenter = new SomeIndexPresenter();
return View(presenter);
}
public ActionResult Create(int? id)
{
var presenter = new ChildPresenter(Passport, UnitOfWork);
return View(presenter);
}
public ActionResult Create(ChildPresenter presenter)
{
if(ModelState.IsValid)
{
presenter.Save();
return ReditectToAction("Index");
}
return View(presenter);
}
}
public class BasePresenter
{
protected IUserPasspoer Passport { get; set; }
protected IUnitOfWork UnitOfWork { get; set; }
public ConsumerController(IUserPasspoer passport, IUnitOfWork unitOfWork)
{
Passport = passport;
UnitOfWork = unitOfWork;
}
}
public class ChildPresenter : BasePresenter
{
public ConsumerController(IUserPasspoer passport, IUnitOfWork unitOfWork)
:base (passport, unitOfWork)
{
}
}
On the other side, if I can utilize property injection which should be one of the benefits of using Ioc
, then I code will look like this
public class ConsumerController : Controller
{
public ActionResult Index(int? id)
{
var presenter = new SomeIndexPresenter();
return View(presenter);
}
public ActionResult Create(int? id)
{
var presenter = new ChildPresenter();
return View(presenter);
}
[HttpPost]
public ActionResult Create(ChildPresenter presenter)
{
if(ModelState.IsValid)
{
presenter.Save();
return ReditectToAction("Index");
}
return View(presenter);
}
}
public class BasePresenter
{
[Dependency]
protected IUserPasspoer Passport { get; set; }
[Dependency]
protected IUnitOfWork UnitOfWork { get; set; }
}
public class ChildPresenter : BasePresenter
{
}