BarbarianIOC.Test/Program.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarbarianIOC.Enums;
using BarbarianIOC.Exceptions;
using BarbarianIOC.Registration;

namespace BarbarianIOC.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Container container = new Container();
            int someMockAge = 23; // this could come from anywhere (App.Config / database etc etc)

            try
            {
                //Register ALL components
                container.RegisterComponents(
                    //where you can use concrete type
                    new Component().For<Foo>().WithInstanceMode(InstanceMode.Transient),
                    //or you can use an interface and it's implementation
                    new Component().ServiceFor<IBaz, Baz>().WithInstanceMode(InstanceMode.Transient),
                    //and you can also declare singleton instace mode if you like
                    new Component().For<SomeIBazDependantClass>().WithInstanceMode(InstanceMode.Singleton),
                    //and even supply some non IOC provided constructor params by way of an anonymous object
                    new Component().For<SomeFooDependantClass>()
                        .DependsOn(new
                        {
                            age = someMockAge
                        })
                        .WithInstanceMode(InstanceMode.Transient)
                );

                //allow the container to wire stuff up (essentially create Expression.New for all components to allow Container to compile and create some
                //quicker lookup delegates)
                container.WireUp();

                //resolve some intances
                SomeIBazDependantClass x1 = container.Resolve<SomeIBazDependantClass>();
                SomeIBazDependantClass x2 = container.Resolve<SomeIBazDependantClass>();
                //As SomeIBazDependantClass is meant to be Singleton, ibazDependantAreSame should be True
                bool ibazDependantAreSame = Object.ReferenceEquals(x1, x2);


                SomeFooDependantClass y1 = container.Resolve<SomeFooDependantClass>();
                SomeFooDependantClass y2 = container.Resolve<SomeFooDependantClass>();
                //As SomeFooDependantClass is meant to be Singleton, SomeFooDependantClass should be False
                bool fooDependantAreSame = Object.ReferenceEquals(y1, y2);

            }
            catch (ContainerConfigurationException ccEx)
            {
                Console.WriteLine("Oh no something went wrong!!!!!!");
                Console.WriteLine(ccEx.Message);
            }

            Console.ReadLine();
后面还有 3 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。