views:

112

answers:

2

I'm attempting to use Unity for the first time and I think I might have bitten off more than I can chew. We have a n-tier application that has a base library with several abstract types and then several business scenario specific libraries on top of it w/ concrete types. For ex: The abstract type lead has two implementations, one in a NewAutomotiveLibrary called NewAutomotiveLead and one in an AutomotiveFinanceLibrary called AutomotiveFinanceLead. Within the base library we have a set of adapters that perform logic on the base types like Lead.

I'm trying to use Unity for the first time to return an interface ILeadDuplication that when resolved, returns either an instance of NewAutomotiveLeadDuplication or AutomotiveFinanceLeadDuplication when I called resolve on ILeadDuplication and pass either a string value of either "NewAutomotive" or "AutomotiveFinance" (names mapped when RegisterType was called on the container). Like so:

  using (IUnityContainer container = new UnityContainer())
  {
    container
      .RegisterType<ILeadDuplication, AutomotiveFinanceLeadDuplication>("AutomotiveFinance")
      .RegisterType<ILeadDuplication, NewAutomotiveLeadDuplication>("NewAutomotive");

    ILeadDuplication dupe = container.Resolve<ILeadDuplication>("AutomotiveFinance");
    Console.WriteLine(dupe.Created);
  }

NOTE: This is for illustration, because the library doesn't know anything about the concreate classes for ILeadDuplication the actually registration would need to be done in the config file.

While this all works great, I need to take it a step further. When calling resolve, I need to be able to pass in an argument of type Lead which is the base type for either NewAutomotiveLead or AutomotiveFinanceLead.

I need to know if it's possible that Unity somehow magically look a property specific to the concrete instance AutomotiveFinanceLead such as "GrossMonthlyIncome" that doesn't exist on Lead and assign it to a new created AutomotiveFinanceLeadDuplication instances property GrossMonthlyIncome.

I'd effectively like to be able to perform a generic set of logic against instances of ILeadDuplication in the base library even thought the instances being generated and properties being mapped are unfamiliar to it.

Thanks!

A: 

Not quite sure what you are after but my guess is that you want to pass in a dependency at the construction time. This can be done in numerous ways but here´s an example.

[InjectionConstructor]
public class MyClass([Dependency("Named")] MyOtherClass other)
{

}

in order for that to work the MyOtherClass dependency named "Named" has to be in the container either via configuration or by adding it via the the RegisterType method.

Let me know if I´m off base and maybe I can help you further...I have worked quite a lot with Unity in different situations so as long as I know what you want to accomplish I might be able to help you out.

Johan Leino
A: 

I don't understand why you want the container to have that responsibility. But I think you could do it this way:

Just before you call Resolve() to get an new instance of ILeadDuplication, you could register the instance of Lead to use and the value of GrossMonthlyIncome to use:

  Lead lead = new NewAutomotiveLead()
  container.RegisterInstance<Lead>(lead);
  container.RegisterInstance<int>("GrossMonthlyIncome", lead.GrossMonthlyIncome);
  ILeadDuplication dupe = container.Resolve<ILeadDuplication>("AutomotiveFinance");

If the concrete type has a dependency named "GrossMonthlyIncome", the value will be injected there. IF the concrete type's constructor takes a Lead instance, the instance will be injected in that constructor parameter.

Sly