Question

Update

I've managed to create something that is satisfactory. You can see the relevant parts of the code here on PasteBin. If there is there something that I could improve please let me know. I've got a nagging feeling this isn't very efficient.

Clarification

While it certainly seems to work with static dependencies as suggested by d_z, I was hoping, to avoid instantiating objects not utlilized, that I could use something similar to this:

public class HomeController : Controller
{
  [Dependency]
  protected IBaseData ActionData { get; set; }

  public ActionResult Index()
  {
    return View(ActionData);
  }

  public ActionResult About()
  {
    return View(ActionData);
  }
}

The data in the IndexData and AboutData instances in reality aren't static. The instance properties are set with data from a database. The DbProvider is injected into these classes.

In the end what I want is to minimize the memory footprint and database accesses.

Original

Let's say we have the following basic controller (with corresponding views):

public class HomeController : Controller
{
  public ActionResult Index()
  {
    return View();
  }

  public ActionResult About()
  {
    return View();
  }
}

We also have two classes with the same interface:

public class IndexData : IBaseData
{
  public string Name { get; set; }
  public string Index { get; set; }

  public IndexData()
  {
    Name = "Index";
    Index = "This is an Index instance";
  }
}

public class AboutData : IBaseData
{
  public string Name { get; set; }
  public string About { get; set; }

  public AboutData()
  {
    Name = "About";
    About = "This is an About instance";
  }
}

What I'd like is for Unity to inject an instance of one of these classes, depending on which action is executed, into the corresponding view. I've tried to get my head around how to achieve this but I'm stumped. Is this even possible?

Was it helpful?

Solution

What you are requesting makes little sense. Dependency Injection is about injecting (design time) behavior (a.k.a. services). What you are trying to do however is to inject runtime data. So this is not a task that yout IoC container should handle.

Next the view should be completely ignorant of any dependency injection. The controller should return all data that the view needs from its action method. Make sure that your About and Index action methods return the proper instance.

OTHER TIPS

To register several mappings for a type in Unity you have to create named registration like this:

myContainer.RegisterType<IBaseData, IndexData>("Index");
myContainer.RegisterType<IBaseData, AboutData>("About");

So after this in your actions you can resolve an instance accordingly:

Index:

IBaseData data = myContainer.Resolve<IBaseData>("Index");

About:

IBaseData data = myContainer.Resolve<IBaseData>("About");

Or for static dependencies it works like this:

[Dependency("Index")]
IBaseData data { get; set; }

Take a look here and here for details

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top