views:

59

answers:

3

Hi All, I'm working on a project that's using the MS Application Blocks. I see the 'Unity' dll is available to me. It's version 1.1 by the way. How can I use dependency injection here?

I have a class

public class ScheduleDataDetailsDC
{
    public int ScheduleID;
    public List<ScheduleRateLineItem> MinRateList;
    public List<ScheduleRateLineItem> MaxRateList;
    public List<ScheduleRateLineItem> VotRateList;
    public List<ScheduleLOSRateDC> LosRateList;
    public List<ScheduleRateParamsDC> RateParams;
}

So when I new it up I am doing this...

    new ScheduleDataDetailsDC{
                    LosRateList = new List<ScheduleLOSRateDC>()
                    , MaxRateList = new List<ScheduleRateLineItemDC>()
                    , MinRateList = new List<ScheduleRateLineItemDC>()
                    , RateParams = new List<ScheduleRateParamsDC>()
                    , VotRateList = new List<ScheduleRateLineItemDC>()
                }

Can Unity 1.1 Help me in anyway here? I would like to just be able to use var x = new ScheduleDetailsDC(), and those 5 inner lists be initialized for me. Can Unity do anything for me here? Please note I've never used DI before.

Thanks for any pointers, ~ck in San Diego

+2  A: 

The best thing to do would be to initialise the lists in the constructor and deny direct access to them from other classes by making them into properties:

public class ScheduleDataDetailsDC
{
    public ScheduleDataDetailsDC()
    {
        this.MinRateList = new List<ScheduleRateLineItem>();
        //inialise other lists
    }

    public List<ScheduleRateLineItem> MinRateList { get; private set; }
    ...
}

It doesn't seem as though dependency injection can really be of use here since the class seems to be a simple data container, although it's difficult to tell without more context.

Lee
A: 

Yes Unity can help you, but I think it's not the case. You can just initialize your lists incide your object giving them default instances, Unity as any other IoC container shouldn't be used as a simple object builder (despite it could).

Restuta
A: 

I'm not sure specifically what the details of the 1.1 release of Unity are, but generally speaking whenever you are using an Inversion of Control Container, you have to go through the following steps:

  1. Register the types your IoC container (Unity in your case) knows about. This includes all of the main types that you plan to request, plus all of the dependent types. In your case you will need to let it know about ScheduleDataDetailsDC, and what, exactly needs to go into each of the lists that are considered dependencies
  2. Your types should specify all of the required dependencies as constructor arguments. This is what the IoC Container will look at to determine what needs to be injected. If you have optional dependencies then you can use Property Injection to support that (if your IoC container supports it, which I think Unity does)
  3. You must request an instance of your registered type from the container. How exactly you do this depends on you container. There should be a method like Get<T>() or Resolve<T>. Generally your going to request instances of the "Highest Level" classes, i.e the ones that are used somewhere near the entry point for your software. If you do this, and you have applied Dependency Injection for all dependent classes down the line (and you've correctly registered all of the dependent types) you should get an object with all of it's dependencies supplied, and likewise all of that objects dependencies should be supplied, and on down the line.

You also tend to see Interfaces used in conjunction with IoC a lot since you can bind a concrete type to the interface type, and then specify that interface as your dependency. This allows you to apply business rules and configuration values during the binding process that will give you the ability to use different concrete implementations in cases where you would need to do such a thing.

So given all of this, it's hard to say exactly what would be involved in utilizing Unity in the situation you've outlined above. Firstly you would need to register ScheduleDataDetailsDC, but to get the dependencies in place you would also need to register each of the List types, or more specifically each concrete object that would go in each list (and then, of course all of the dependencies for those classes). Since I'm not really sure what the roles of those lists are, it's hard for me to say how you could go about doing that (or even if you could go about doing that).

ckramer