示例#1
0
        /// <summary>
        /// inject dependency object
        /// </summary>
        /// <param name="obj">target class instance</param>
        public static void Inject(object obj)
        {
            //flyweight
            //IKernel kernel = new StandardKernel();
            IKernel kernel = FactoryCore.getKernel();

            // get fields
            Type type   = obj.GetType();
            var  fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            // find interface
            foreach (var field in fields)
            {
                Type field_type = field.FieldType;
                if (field_type.IsInterface)
                {
                    try
                    {
                        // inject
                        object instance = kernel.Get(field_type);
                        field.SetValue(obj, instance);
                    }
                    catch (KeyNotFoundException)
                    {
                        //log
                        // not found interface key.
                    }
                }
            }
        }
示例#2
0
        protected void Application_Start()
        {
            // create DI container
            //カプセル化
            IKernel kernel = FactoryCore.getKernel();

            // binding Di container
            kernel.Binding <IRepository, MysqlRepo>();
            kernel.Binding <IAccountProc, AccountProc>();
            kernel.Binding <IReserveProc, ReserveProc>();
            kernel.Binding <ITestProc, TestProc>();

            FactoryCore.getSocket().Run();
            //initialize
            FactoryCore.getList();
        }