Click here to Skip to main content
14,123,558 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 28 Aug 2009
Licenced CPOL

Silverlight and the Unity Container - Property and Method Injection

, 28 Aug 2009
Rate this:
Please Sign up or sign in to vote.
Using Property and Method injection with Silverlight 3


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


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).

public interface INumberA {
    int NumA {get;set;}
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.

//Property Injection
public INumberA PropNumA { get { return this.objA; } set { this.objA = value; } }

//Property Injection
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:

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.

public int Sum {
    get {
    return intSum;
    set {
        if (intSum != value) {
            intSum = value;
public int NumA {
    get {
        return objA.NumA;
    set {
        if (objA.NumA != value) {
            objA.NumA = value;
            GetSum(objA.NumA, NumB);
public int NumB {
    get {
        return objB.NumB;
    set {
        if (objB.NumB != value) {
            objB.NumB = value;
            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.

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.


  • Initial revision - 23rd August, 2009


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


About the Author

Abhinav S
Software Developer (Senior)
India India

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190518.1 | Last Updated 28 Aug 2009
Article Copyright 2009 by Abhinav S
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid