• Lenic.DI Another IOC Container Library Using Delegate


    Lenic.DI

    Another IOC Container Library Using Delegate.

    Characteristic

    • Use delegate generated object.
    • Support the uncertain parameters.
    • Lazy load.

    Basic Usage

    IContainer container = new Container();
    
    container.Register(null, c => new Person() { Id = 3, Name = "张三" });
    
    var p = container.Resolve<Person>();
    
    Assert.IsInstanceOfType(p, typeof(Person));
    Assert.AreEqual(3, p.Id);
    Assert.AreEqual("张三", p.Name);

    Singleton Lifetime:

    IContainer container = new Container();
    
    container.Register(new SingleLifetime(), c => new Person() { Id = 4, Name = "李四" });
    
    var p = container.Resolve<Person>();
    
    Assert.IsInstanceOfType(p, typeof(Person));
    Assert.AreSame(p, container.Resolve<Person>());
    Assert.AreEqual(4, p.Id);
    Assert.AreEqual("李四", p.Name);

    And, has other lifetime, Waiting for you to explore.

    Use it with iterative method:

    IContainer container = new Container();
    
    container.Register(null, c => new Person() { Id = 3, Name = "张三" })
             .RegisterNamed(null, "x", c => new Person() { Id = 4, Name = c.Resolve<Person>().Name });
    
    var p = container.ResolveNamed<Person>("x");
    
    Assert.IsInstanceOfType(p, typeof(Person));
    Assert.AreEqual(4, p.Id);
    Assert.AreEqual("张三", p.Name);

    Auto Register:

    IContainer container = new Container();
    container.OnResolveError += ContainerExtensions.DefaultRegister();
    
    var p = container.Resolve<Person>();
    Assert.IsNotNull(p);

    Auto Interface Register:

    IContainer container = new Container();
    var interfaceRegister = ContainerExtensions.InterfaceRegister();
    container.OnResolveError += interfaceRegister;
    
    var im = container.Resolve<IPerson>();
    Assert.IsNotNull(im);
    Assert.IsInstanceOfType(im, typeof(IPerson));

    Auto Generic Register:

    IContainer container = new Container();
    container.Register(null, c => 16);
    container.OnResolveError += ContainerExtensions.GenericRegister(typeof(IList<>), typeof(List<>));
    
    var g = container.Resolve<IList<string>>();
    Assert.IsNotNull(g);
    Assert.IsInstanceOfType(g, typeof(IList<string>));

    And other, will be coming...

    Look it on CodePlex, Thank you.

  • 相关阅读:
    案例5-1.3 整型关键字的散列映射 (25分)--散列表(除留余数法+线性探测法)
    基础实验5-2.2 电话聊天狂人 (25分)-散列表
    习题8.4 畅通工程之最低成本建设问题 (30分)--最小生成树
    练习4.2 平衡二叉树的根 (25分)
    案例7-1.5 与零交换 (25分)--dfs
    习题2.8 输出全排列 (20分)--dfs
    nginx 日志切割
    mysql添加索引
    mysql常见错误码及说明
    MYSQL数据类型分类
  • 原文地址:https://www.cnblogs.com/lenic/p/3117893.html
Copyright © 2020-2023  润新知