Exemple #1
0
        public static void Show()
        {
            {
                //程序注册 基于特性
                IUnityContainer container = new UnityContainer();
                container.RegisterType <IBaseService, BaseService>();
                container.RegisterType <IUserMenuService, UserMenuService>(new InjectionConstructor(typeof(DbContext), 123));//指定构造函数的参数值
                container.RegisterType <IUserCompanyService, UserCompanyService>();
                container.RegisterType <DbContext, JDContext>();

                container.AddNewExtension <Interception>().Configure <Interception>()
                .SetInterceptorFor <IUserMenuService>(new InterfaceInterceptor());

                using (IUserMenuService service = container.Resolve <IUserMenuService>())
                {
                    User user = service.Find <User>(1); //继承接口无效
                    service.UserLastLogin(user);        //只有这个才有拦截
                }
            }

            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config.xml");//找配置文件的路径
                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

                IUnityContainer container = new UnityContainer();
                section.Configure(container, "ruanmouContainer");

                using (IUserMenuService service = container.Resolve <IUserMenuService>())
                {
                    User user = service.Find <User>(1);//都会生效
                    service.UserLastLogin(user);
                }
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班vip课程,今天是IOC+EF");

                //{
                //    AndroidPhone phone = new AndroidPhone();
                //}
                //{
                //    IPhone phone = new AndroidPhone();
                //}
                //{
                //    IPhone phone = ObjectFactory.CreatePhone();
                //}
                //IOCTest.Show();


                //using (JDContext context = new JDContext())
                //{
                //    User user = context.Set<User>().Find(2);
                //}

                //UserService service = new UserService();
                ////User user = service.Find(2);
                //User user = service.Find<User>(2);

                //IUserMenuService service = new UserMenuService();
                //User user = service.Find<User>(2);
                //service.UserLastLogin(user);

                IUnityContainer container = new UnityContainer();
                container.RegisterType <IBaseService, BaseService>();
                container.RegisterType <IUserMenuService, UserMenuService>();
                container.RegisterType <DbContext, JDContext>();

                using (IUserMenuService service = container.Resolve <IUserMenuService>())
                {
                    using (IUnitOfWork unitOfWork = new UnitOfWork())
                    {
                        User user = service.Find <User>(2);
                        service.Commit();
                        service.Commit();

                        unitOfWork.Commit();
                    }
                }



                //ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                //fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config.xml");//找配置文件的路径
                //Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                //UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

                //IUnityContainer container = new UnityContainer();
                //section.Configure(container, "ruanmouContainer");

                //ICategoryService categoryService = container.Resolve<ICategoryService>();
                //Category category = categoryService.Find(1);
                //categoryService.Update(category);
                //categoryService.Show();

                //IBaseService<Category> iBaseServie = container.Resolve<IBaseService<Category>>();

                //Category category1 = iBaseServie.Find(1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }