轻量级IOC容器IOCFactory发布。

发布时间:2020-07-27 22:16:12 作者:持刀不抢劫
来源:网络 阅读:1336

这是我的第三篇讨论IOC工厂的文章了,貌似我已经跟IOC工厂杠上了。轻量级IOC容器IOCFactory发布。

前两篇是说的造轮子的过程。貌似没什么人感兴趣,那这次就直接发布轮子吧。


轻量级IOC容器IOCFactory发布。

上图是 我的容器与 微软的企业库 unity的性能比较。

可以看见。效率是微软企业库的6倍。

使用依赖注入模式 效率也比微软企业库 要快。


目前支持以下几种模式对象的创建

普通模式,

单例模式,

依赖注入模式,

装饰模式,

以及单例注入模式


5种模式的获取方式类似都是采用

Factory.Get<IType>([key])的方式进行获取。


但是注册有所不同。


factory.Regist<T, Q>(IOCFactoryModel.InstType.Normal);//普通模式
 factory.Regist<T, Q>(InstType.DI);//依赖注入模式
factory.Regist<T, Q>(InstType.Singleton);//单例模式
factory.RegistDecorate<T, Q>("catDog", "cat");//装饰者模式
factory.Regist<T, Q>(InstType.DISingleton)//单例注入模式


下面是使用的示例代码

namespace IOCFactoryUnitTest
{
    public interface Animal
    {
        string Hawl();
    }
    public interface Toy
    {
        string Play();
    }
    public class Ball : Toy
    {
        public string Play()
        {
            return "roll and roll";
        }
    }
    public class Dog : Animal
    {
        public string Hawl()
        {
            var returnValue = "wang wang wang";
            return returnValue;
        }
    }
    public class Cat : Animal
    {
        public string Hawl()
        {
            var returnValue = "miao miao miao";
            return returnValue;
        }
    }
    public class CatDog : Animal
    {
        private Animal toDecorate;
        public CatDog(Animal toDecorate)
        {
            this.toDecorate = toDecorate;
        }
        public string Hawl()
        {
            var returnValue = toDecorate.Hawl();
            returnValue += " wang wang wang";
            return returnValue;
        }
    }
    public class MachineCatDog : Animal
    {
        private Animal toDecorate;
        public MachineCatDog(Animal toDecorate)
        {
            this.toDecorate = toDecorate;
        }
        public string Hawl()
        {
            var returnValue = toDecorate.Hawl();
            returnValue += " hmm hmm hmm";
            return returnValue;
        }
    }
    public class SingleTonTest : Animal
    {
        private SingleTonTest() { }
        public string Hawl()
        {
            return this.GetHashCode().ToString();
        }
    }
    public class DITest
    {
        Animal animal;
        Toy toy;
        public DITest(Animal animal, Toy toy)
        {
            this.animal = animal;
            this.toy = toy;
        }
        public string Test()
        {
            return animal.Hawl() + this.toy.Play();
        }
    }
    [TestClass]
    public class IOCFactoryTest
    {
        [ClassInitialize()]
        public static void Init(TestContext context)
        {
            Factory factory = Factory.GetInst();
            factory.Regist<Animal, Cat>("cat", InstType.Normal);
            factory.Regist<Animal, Dog>("dog", InstType.Normal);
            factory.Regist<Animal, SingleTonTest>(InstType.Singleton);
            factory.RegistDecorate<Animal, CatDog>("catDog", "cat");
            factory.RegistDecorate<Animal, MachineCatDog>("catDog", "cat");
            factory.Regist<Toy, Ball>(InstType.Normal);
            factory.Regist<DITest, DITest>(InstType.DISingleton);
        }
        [TestCleanup]
        public void CleanUp()
        {
        }
        [TestMethod]
        public void NormalInstTest()
        {
            Factory factory = Factory.GetInst();
            var result = new Dog();
            var dog = factory.Get<Animal>("dog");
            Assert.AreEqual(dog.Hawl(), result.Hawl());
            var cat = factory.Get<Animal>("cat");
            Assert.AreNotEqual(cat.Hawl(), result.Hawl());
        }
        [TestMethod]
        public void SingleInstTest()
        {
            Factory factory = Factory.GetInst();
            var obj1 = factory.Get<Animal>();
            var obj2 = factory.Get<Animal>();
            Assert.AreEqual(obj1, obj2);
        }
        [TestMethod]
        public void DecorateInstTest()
        {
            Factory factory = Factory.GetInst();
            var dog = factory.Get<Animal>("dog");
            var cat = factory.Get<Animal>("cat");
            var catDog = factory.Get<Animal>("catDog");
            Assert.AreEqual(cat.Hawl() + " " + dog.Hawl() + " hmm hmm hmm", catDog.Hawl());
        }
        [TestMethod]
        public void DIInstTest()
        {
            Factory factory = Factory.GetInst();
            var ani = factory.Get<Animal>();
            var toy = factory.Get<Toy>();
            var result = factory.Get<DITest>();
            var result2 = factory.Get<DITest>();
            var exp = new DITest(ani, toy);
            Assert.AreEqual(exp.Test(), result.Test());
            Assert.AreEqual(result, result2);
        }
        [TestMethod]
        public void MultiThreadTest()
        {
            try
            {
                int count = 100;
                Action func = () => { for (var i = 0; i < count; i++) { DIInstTest(); } };
                var a = new Thread(new ThreadStart(func));
                var b = new Thread(new ThreadStart(func));
                var c = new Thread(new ThreadStart(func));
                a.Start();
                b.Start();
                c.Start();
            }
            catch (Exception ex)
            {
                Assert.Fail();
            }
            Assert.IsTrue(true);
        }
    }
}

类库已经上传到附件。有兴趣的朋友可以下载

点击 访问github获取本项目的最新代码

附件:http://down.51cto.com/data/2363596
推荐阅读:
  1. Spring IOC 容器源码分析
  2. Spring ioc容器介绍

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ioc容器 轻量级

上一篇:[xmind] Encoding / Hashing / Encryption

下一篇:CentOS下安装jdk1.8

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》