Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#

Extending Unity Container to Support DefaultValue Attribute

Rate me:
Please Sign up or sign in to vote.
4.84/5 (11 votes)
3 Apr 2012CPOL4 min read 41.4K   509   19  
Extending Microsoft Enterprise Library Unity block to support custom attributes and DefaultValueAttribute in particular.
using System;
using System.ComponentModel;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Unity;
using UnityDefaultValueExtension;

namespace UnityDefaultValueExtensionTest
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnityDefaultValueExtUnitTest
    {
        public UnityDefaultValueExtUnitTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion


        [TestMethod]
        public void Testing_DefaultValueDependencies()
        {
            IUnityContainer container = new UnityContainer()
                .RegisterType<IMockClassInterface, MockClass>(
                    typeof(MockClass).DefaultValueDependencies());

            IMockClassInterface iTest = container.Resolve<IMockClassInterface>();
            
            Assert.IsNotNull(iTest);
            Assert.AreEqual(0, iTest.IntProperty);
            Assert.AreEqual(0, iTest.IntProperty0);
            Assert.AreEqual(10, iTest.IntProperty1);
            Assert.AreEqual(11, iTest.IntProperty2);
            Assert.AreEqual(12, iTest.LongProperty);
            Assert.AreEqual(true, iTest.BoolProptrty);
        }


        [TestMethod]
        public void Testing_DefaultValueDependencies_WithOverrides()
        {
            IUnityContainer container = new UnityContainer()
                .RegisterType<IMockClassInterface, MockClass>(
                    typeof(MockClass).DefaultValueDependencies());
            container.RegisterType<MockClass>(new InjectionMember[] 
                    { 
                        new InjectionProperty("IntProperty", InjectionParameter<int>.ToParameter(100)),
                        new InjectionProperty("IntProperty1", InjectionParameter<int>.ToParameter(200)),
                        new InjectionProperty("LongProperty", InjectionParameter<long>.ToParameter((long)300))
                    });

            IMockClassInterface iTest = container.Resolve<IMockClassInterface>();

            Assert.IsNotNull(iTest);
            Assert.AreEqual(100, iTest.IntProperty);
            Assert.AreEqual(0, iTest.IntProperty0);
            Assert.AreEqual(200, iTest.IntProperty1);
            Assert.AreEqual(11, iTest.IntProperty2);
            Assert.AreEqual(300, iTest.LongProperty);
            Assert.AreEqual(true, iTest.BoolProptrty);
        }


        [TestMethod]
        public void Testing_RegisterTypeWithDefaultValues()
        {
            IUnityContainer container = new UnityContainer()
                .RegisterTypeWithDefaultValues<IMockClassInterface, MockClass>();

            IMockClassInterface iTest = container.Resolve<IMockClassInterface>();

            Assert.IsNotNull(iTest);
            Assert.AreEqual(0, iTest.IntProperty);
            Assert.AreEqual(0, iTest.IntProperty0);
            Assert.AreEqual(10, iTest.IntProperty1);
            Assert.AreEqual(11, iTest.IntProperty2);
            Assert.AreEqual(12, iTest.LongProperty);
            Assert.AreEqual(true, iTest.BoolProptrty);
        }


        [TestMethod]
        public void Testing_RegisterTypeWithDefaultValues_WithOverrides()
        {
            IUnityContainer container = new UnityContainer()
                .RegisterTypeWithDefaultValues<IMockClassInterface, MockClass>()
                .RegisterType<MockClass>(new InjectionMember[] 
                    { 
                        new InjectionProperty("IntProperty", InjectionParameter<int>.ToParameter(100)),
                        new InjectionProperty("IntProperty1", InjectionParameter<int>.ToParameter(200)),
                        new InjectionProperty("LongProperty", InjectionParameter<long>.ToParameter((long)300))
                    });

            IMockClassInterface iTest = container.Resolve<IMockClassInterface>();

            Assert.IsNotNull(iTest);
            Assert.AreEqual(100, iTest.IntProperty);
            Assert.AreEqual(0, iTest.IntProperty0);
            Assert.AreEqual(200, iTest.IntProperty1);
            Assert.AreEqual(11, iTest.IntProperty2);
            Assert.AreEqual(300, iTest.LongProperty);
            Assert.AreEqual(true, iTest.BoolProptrty);
        }



        [TestMethod]
        public void Testing_UnityDefaultValueExtender()
        {
            IUnityContainer container = new UnityContainer()
                .AddNewExtension<UnityDefaultValueExtender>() // Add extension
                .RegisterType<IMockClassInterface, MockClass>(typeof(MockClass).DefaultValueDependencies());

            IMockClassInterface iTest = container.Resolve<IMockClassInterface>();

            Assert.IsNotNull(iTest);
            Assert.AreEqual(0, iTest.IntProperty);
            Assert.AreEqual(0, iTest.IntProperty0);
            Assert.AreEqual(10, iTest.IntProperty1);
            Assert.AreEqual(11, iTest.IntProperty2);
            Assert.AreEqual(12, iTest.LongProperty);
            Assert.AreEqual(true, iTest.BoolProptrty);
        }



        [TestMethod]
        public void Testing_UnityDefaultValueExtender_WithOverrides()
        {
            IUnityContainer container = new UnityContainer()
                .AddNewExtension<UnityDefaultValueExtender>() // Add extension
                .RegisterType<IMockClassInterface, MockClass>(
                    typeof(MockClass).DefaultValueDependencies())
                .RegisterType<MockClass>(new InjectionMember[] 
                    { 
                        new InjectionProperty("IntProperty", InjectionParameter<int>.ToParameter(100)),
                        new InjectionProperty("IntProperty1", InjectionParameter<int>.ToParameter(200)),
                        new InjectionProperty("LongProperty", InjectionParameter<long>.ToParameter((long)300))
                    });

            IMockClassInterface iTest = container.Resolve<IMockClassInterface>();

            Assert.IsNotNull(iTest);
            Assert.AreEqual(100, iTest.IntProperty);
            Assert.AreEqual(0, iTest.IntProperty0);
            Assert.AreEqual(200, iTest.IntProperty1);
            Assert.AreEqual(11, iTest.IntProperty2);
            Assert.AreEqual(300, iTest.LongProperty);
            Assert.AreEqual(true, iTest.BoolProptrty);
        }
    }

    interface IMockClassInterface
    {
        int IntProperty { get; set; }
        int IntProperty0 { get; set; }
        int IntProperty1 { get; set; }
        int IntProperty2 { get; set; }
        long LongProperty { get; set; }
        bool BoolProptrty { get; set; }
    }

    public class MockClass : IMockClassInterface
    {
        public MockClass() { }

        #region TestInterface Members

        public int IntProperty { get; set; }

        //[Dependency]
        public int IntProperty0 { get; set; }

        //[Dependency]
        [DefaultValue(10)]
        public int IntProperty1 { get; set; }

        [DefaultValue(11)]
        public int IntProperty2 { get; set; }

        [DefaultValue(12)]
        public long LongProperty { get; set; }

        [DefaultValue(true)]
        public bool BoolProptrty { get; set; }

        #endregion
    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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)
United States United States
Senior Software Engineer with over 20+ years of experience in variety of technologies, development tools and programming languages.

Microsoft Certified Specialist programming in C#, JavaScript, HTML, CSS

Comments and Discussions