Click here to Skip to main content
15,888,803 members
Articles / Programming Languages / C#

Silverlight and the Unity Container - Property and Method Injection

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
28 Aug 2009CPOL1 min read 20.6K   174   9  
Using Property and Method injection with Silverlight 3

Introduction

In this article, Property Injection (and Method injection) will be demonstrated. This will be done using the unity container.

Background

Code for this article has been written using Silverlight 3 and Unity Application Block 1.2 for Silverlight. The XAML and its output are very similar to a previous article (on Constructor Injection). Most of the concepts mentioned here are also similar to the older article.

Using the Code

Again, the two numbers that are going to be added are represented as interfaces in the sample code. We are then implementing these interfaces in two number classes (one each for the numbers).

C#
//Interface
public interface INumberA {
    int NumA {get;set;}
} 
//Number
public class NumberA:INumberA {
    private int intNumA;
    public int NumA
    {
        get {return intNumA;}
        set { intNumA = value; }
    }
}

The Total class this time does not contain a constructor. Instead it contains two properties. These properties are actually the number interfaces the total "depends" on.

The Dependency attribute is important here. This is what tells Unity what types this class depends on.

C#
//Property Injection
[Dependency()]
public INumberA PropNumA { get { return this.objA; } set { this.objA = value; } }

//Property Injection
[Dependency()]
public INumberB PropNumB { get { return this.objB; } set { this.objB = value; } }

Instead if we were using method injection, this is what this code would look like:

C#
[InjectionMethod]
public void initialize(INumberA objA, INumberB objB) {
   this.objA = objA;
   this.objB = objB;
}

A method with the InjectionMethod attribute is created. The dependencies are set to be the two parameters.

The Total class also implements the INotifyPropertyChanged interface and some properties that are used in binding to the XAML.

C#
public int Sum {
    get {
    return intSum;
    }
    set {
        if (intSum != value) {
            intSum = value;
            OnPropertyChanged("Sum");
        }
    }
}
public int NumA {
    get {
        return objA.NumA;
    }
    set {
        if (objA.NumA != value) {
            objA.NumA = value;
            OnPropertyChanged("NumA");
            GetSum(objA.NumA, NumB);
        }
    }
}
public int NumB {
    get {
        return objB.NumB;
    }
    set {
        if (objB.NumB != value) {
            objB.NumB = value;
            OnPropertyChanged("NumB");
            GetSum(NumA, objB.NumB);
        }
    }
}

private void GetSum(int num1, int num2)
{
    Sum = objA.NumA + objB.NumB;
}

Hardly anything changes in the xaml.cs code behind file.

C#
IUnityContainer container = new UnityContainer()
//Interfaces used - so register the correct type
.RegisterType<INumberA, NumberA>()
.RegisterType<INumberB, NumberB>();
//Unity takes care of instantiation of all dependent classes
this.DataContext = container.Resolve<Total>();

The total is displayed and calculated once the application is run and the numbers are entered on the web page.

Points of Interest

Note that nothing changes in MainPage.xaml.cs, i.e. no matter what injection style is used, for this sample, the client remains the same.

History

  • Initial revision - 23rd August, 2009

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Comments and Discussions

 
-- There are no messages in this forum --