HOWTO !

The default mapping source is AttributeMappingSource. It uses the custom attribute ExportAttribute, like MEF. So if you used MEF, nothing should be made for component.But the automatic binding does not support for now in sparrow.(默认的映射方式是AttributeMappingSource。使用MEF使用的自定义Attribute ExportAttribute来标识映射规则。如果你之前使用MEF注入,那么组件类库就无需做修改,但是Sparrow目前不支持自动装配对象)。


    [Export(typeof(Test))]
    [Export(typeof(ITest))]
    class Test : ITest
    {
        public string Name
        {
            get;
            set;
        }

        public string Blog
        {
            get;
            set;
        }
    }

    [Export(typeof(ITest))]
    class Test2 : ITest
    {
        public string Name
        {
            get;
            set;
        }
    }

    class Test3 : ITest
    {
        public string Name
        {
            get;
            set;
        }
    }

    class Test4 : ITest
    {
        public string Name
        {
            get;
            set;
        }
    }

    interface ITest
    {
        string Name
        {
            get;
            set;
        }
    }

    interface INoMapping
    {

    }


 using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Sparrow.Tests.IoC
{
    using Sparrow.IoC;   
    using Moq;

    /// <summary>
    /// Summary description for ObjectContainerTests
    /// </summary>
    [TestClass]
    public class ObjectContainerTests
    {
        public ObjectContainerTests()
        {
           
        }

        [ClassInitialize()]
        public static void Initialize(TestContext testContext) {

            AttributeMappingSource.TypeFinder = new WinTypeFinder();
        }


        [TestMethod]
        public void resolve_by_class_type()
        {
            var instance = ObjectContainer.Resolve<Test>();
            Assert.IsInstanceOfType(instance, typeof(Test));        
        }

        [TestMethod]
        public void resolve_by_interface()
        {
            var instance = ObjectContainer.Resolve<ITest>();
            Assert.IsInstanceOfType(instance, typeof(Test));
        }

        [TestMethod]
        public void resolve_all_by_interface()
        {
            var instances = ObjectContainer.ResolveAll<ITest>();
            Assert.IsTrue(instances.Any(i => i is Test));
            Assert.IsTrue(instances.Any(i => i is Test2));
        }

        [TestMethod]
        public void resolve_by_code_map()
        {
            
            ObjectContainer.Export<Test3>().To<ITest>();

            var instances = ObjectContainer.ResolveAll<ITest>();

            Assert.IsTrue(instances.Any(i => i is Test));
            Assert.IsTrue(instances.Any(i => i is Test2));
            Assert.IsTrue(instances.Any(i => i is Test3));
        }

        [TestMethod]
        public void resolve_instance_count_should_be_equals_exports()
        {
            var finder = new Mock<ITypeFinder>();
            finder.Setup(i => i.GetTypes()).Returns(() =>
            {
                var types = new List<Type>();

                types.Add(typeof(Test));
                types.Add(typeof(Test2));

                return types;                
            });

            AttributeMappingSource.TypeFinder = finder.Object;
            AttributeMappingSource.Mappings = null;//reset mappings,let it read mapping from new typefinder.

            ObjectContainer.Export<Test3>().To<ITest>();//map a new type to the interface
        

            var instances = ObjectContainer.ResolveAll<ITest>();

            Assert.AreEqual(3, instances.Count());
            Assert.IsTrue(instances.Any(i => i is Test));
            Assert.IsTrue(instances.Any(i => i is Test2));
            Assert.IsTrue(instances.Any(i => i is Test3));


        }

        [TestMethod]
        public void resolve_class_instance_without_mapping_should_create_an_instance_for_it()
        {
            var instance = ObjectContainer.Resolve<Test4>();

            Assert.IsInstanceOfType(instance, typeof(Test4));
        }


        [TestMethod]
        [ExpectedException(typeof(MissingMappingException))]
        public void resolve_interfence_instance_without_mapping_should_throw_missing_mapping_exception()
        {
            var instance = ObjectContainer.Resolve<INoMapping>();
        }

    }
}

Last edited Nov 22, 2010 at 3:22 PM by cnlangzi, version 5

Comments

No comments yet.