[1].[代码] 用于装饰要注入类的Attribute. 跳至 [1] [2] [3] [4] [5] [6] [7]

using System;
using System.Linq;
using Skight.eLiteWeb.Domain.BasicExtensions;

namespace Skight.eLiteWeb.Domain.Containers
{
    [AttributeUsage(AttributeTargets.Class)]
    public class RegisterInContainerAttribute : Attribute
    {
        public RegisterInContainerAttribute(LifeCycle life_cycle) {
            this.life_cycle = life_cycle;
        }

        public LifeCycle life_cycle { get; set; }
        public Type type_to_register_in_container { get; set; }

        public void register_using(Registration registration) {
            DiscreteItemResolver resolver = new TypeResolver(type_to_register_in_container);
            if (life_cycle == LifeCycle.singleton) resolver = new SingletonResolver(resolver);
            registration.register(resolver,type_to_register_in_container.all_interface().ToArray());
        }
    }
}

[2].[代码] 注入声明Attribute参数,指定是否单件方式生成注册类。 跳至 [1] [2] [3] [4] [5] [6] [7]

namespace Skight.eLiteWeb.Domain.Containers
{
    public enum LifeCycle
    {
        single_call,
        singleton
    }
}

[3].[代码] 注册声明Attribute的使用示例 跳至 [1] [2] [3] [4] [5] [6] [7]

//调用Container.get_an<FrontController>()将会得到FrontControllerImpl的对象

using Skight.eLiteWeb.Domain.Containers;

namespace Skight.eLiteWeb.Presentation.Web.FrontControllers
{
    [RegisterInContainer(LifeCycle.single_call)]
    public class FrontControllerImpl : FrontControllers.FrontController
    {
        private CommandResolver command_resolver;

        public FrontControllerImpl(CommandResolver commandResolver)
        {
            command_resolver = commandResolver;
        }

        public void process(WebRequest request)
        {
            command_resolver.get_command_to_process(request).process(request);
        }
    }
}

[4].[代码] Registration添加一个方法:反过来,为一个解析器(实现类)注册多个接口。 跳至 [1] [2] [3] [4] [5] [6] [7]

void register(DiscreteItemResolver resolver, params Type[] contracts);

[5].[代码] 扫描注册机,在系统启动时调用一次。 跳至 [1] [2] [3] [4] [5] [6] [7]

using System.Collections.Generic;
using System.Reflection;
using Skight.eLiteWeb.Domain.BasicExtensions;
using Skight.eLiteWeb.Domain.Containers;

namespace Skight.eLiteWeb.Application.Startup
{
    public class RegistrationScanner:StartupCommand
    {
        private readonly Registration registration;
        private readonly IEnumerable<Assembly> assemblies;

        public RegistrationScanner(Registration registration, params Assembly[] assemblies)
        {
            this.registration = registration;
            this.assemblies = assemblies;
        }

        public void run()
        {
            assemblies
                .each(assembly =>
                      assembly.GetTypes()
                              .each(type =>
                                    type.run_againste_attribute<RegisterInContainerAttribute>(
                                        attribute =>
                                            {
                                                attribute.type_to_register_in_container = type;
                                                attribute.register_using(registration);
                                            })));
        }
    }
}

[6].[代码] 系统启动:创建注册类,然后传入必要的Assembly,调用注册机注册所有加了Attribute的类。 跳至 [1] [2] [3] [4] [5] [6] [7]

using System;
using System.Collections.Generic;
using System.Reflection;
using Skight.eLiteWeb.Domain.Containers;
using Skight.eLiteWeb.Presentation.Web.FrontControllers;

namespace Skight.eLiteWeb.Application.Startup
{
    public class ApplicationStartup
    {
        public void run()
        {
            var registration = create_registration();
            new RegistrationScanner(registration,
                                               Assembly.GetAssembly(typeof (Container)),
                                               Assembly.GetAssembly(typeof (FrontController)),
                                               Assembly.GetAssembly(typeof (StartupCommand)))
                                               .run();

        }

        private Registration create_registration()
        {
            IDictionary<Type, DiscreteItemResolver> item_resolvers = new Dictionary<Type, DiscreteItemResolver>();
            Container.initialize_with(new ResolverImpl(item_resolvers));
            return new RegistrationImpl(item_resolvers);
        }
    }
}

[7].[代码] Global.asax系统入口,把所有的代码串起来。 跳至 [1] [2] [3] [4] [5] [6] [7]

<%@ Application Language="C#" %>
<%@ Import Namespace="Skight.eLiteWeb.Application.Startup" %>
<script  RunAt="server">

    private void Application_Start(object sender, EventArgs e)
    {
       new ApplicationStartup().run();
    }

</script>