public void DynamicProxyGenerator_GetFakeInstanceFor_ShouldWork()
        {
            // Arrange + Act
            IMyTestModel instance = DynamicProxyGenerator.GetFakeInstanceFor <IMyTestModel>();

            // Assert
            Assert.IsNotNull(instance);
        }
        public void DynamicProxyGenerator_GetFakeInstanceFor_OnlyGetters_WithInheritance_ShouldWork()
        {
            // Arrange + Act
            IMyTestModelWithGettersWithInheritance instance = DynamicProxyGenerator.GetFakeInstanceFor <IMyTestModelWithGettersWithInheritance>();

            // Assert
            Assert.IsNotNull(instance);
        }
Esempio n. 3
0
        public void Initialize()
        {
            this.db = new RedisDatabase();

            var typeRepo = new TypeRepository(new TypeMetadataGenerator(false));

            this.dbRecordBuilder = new DbRecordBuilder(typeRepo);
            this.stub            = new DynamicProxyStub(typeRepo, this.db, this.dbRecordBuilder);
            this.generator       = new DynamicProxyGenerator(typeRepo, this.stub, this.db);
        }
        public void CreateInterfaceProxyWithoutOptions()
        {
            var          dynamicProxyGenerator  = new DynamicProxyGenerator();
            ITestService implementationInstance = new TestService();
            var          interceptorMock        = new Mock <IInterceptronInterceptor>();
            var          interceptors           = new[] { interceptorMock.Object };

            var testService = dynamicProxyGenerator.CreateInterfaceProxy(implementationInstance, interceptors);

            Assert.IsInstanceOf <ITestService>(testService);

            AssertInterceptors(testService, interceptors);
        }
        public void DynamicProxyGenerator_GetFakeInstanceFor_OnlyGetters_WithInheritance_ShouldReturnValidInstance()
        {
            // Arrange
            int    id   = 1;
            string name = "123123";
            IMyTestModelWithGettersWithInheritance instance = DynamicProxyGenerator.GetFakeInstanceFor <IMyTestModelWithGettersWithInheritance>();

            // Act
            instance.GetType().GetProperty(nameof(IMyTestModelWithGetters.Id)).SetMethod.Invoke(instance, new object[] { id });
            instance.GetType().GetProperty(nameof(IMyTestModelWithGetters.Name)).SetMethod.Invoke(instance, new object[] { name });

            // Assert
            Assert.AreEqual(id, instance.Id);
            Assert.AreEqual(name, instance.Name);
        }
        public void DynamicProxyGenerator_GetFakeInstanceFor_WithInheritance_ShouldReturnValidInstance()
        {
            // Arrange
            int    id   = 1;
            string name = "123123";
            IMyTestModelWithInheritance instance = DynamicProxyGenerator.GetFakeInstanceFor <IMyTestModelWithInheritance>();

            // Act
            instance.Id   = id;
            instance.Name = name;

            // Assert
            Assert.AreEqual(id, instance.Id);
            Assert.AreEqual(name, instance.Name);
        }
Esempio n. 7
0
        /// <summary>
        /// 执行修改的数据字段
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public bool UpdateModify(TEntity entity, ITransaction transaction = null)
        {
            HashSet <string> modifiedPropertyNames = DynamicProxyGenerator.GetModifiedProperties(entity);

            if (modifiedPropertyNames == null || !modifiedPropertyNames.Any())
            {
                return(false);
            }

            SqlQuery select = _sqlGenerator.GetUpdate(modifiedPropertyNames, entity);

            using (DbContext dbContext = new DbContext(_connectionSeting.ConnectionString, transaction))
            {
                return(dbContext.Connection.Execute(select.Sql, select.Param, dbContext.DbTransaction) > 0);
            }
        }
Esempio n. 8
0
        public void Run()
        {
            int i = new int();
            DynamicProxyGenerator dpg = new DynamicProxyGenerator(typeof(Bird));
            var proxy = dpg.CreateProxy();
            var bird  = (Bird)FastObjectCreater.CreateInstance(proxy);

            //bird.Fly("大雁");
            //bird.GetWingCount();
            //bird.Show();
            bird.Sum(12, 1);

            //var dd = new DynamicProxySample();
            //dd.ShowMessage("ddd");

            Assert.IsTrue(true);
        }
        public void CreateClassProxyWithOptions()
        {
            var dynamicProxyGenerator  = new DynamicProxyGenerator();
            var implementationInstance = new TestService();
            var interceptorMock        = new Mock <IInterceptronInterceptor>();
            var interceptors           = new[] { interceptorMock.Object };
            var proxyGenerationOptions = new InterceptronProxyGenerationOptions();

            var testService = dynamicProxyGenerator.CreateClassProxy(implementationInstance, proxyGenerationOptions, interceptors);

            Assert.IsInstanceOf <TestService>(testService);

            var generationOptions = testService.GetProxyGenerationOptions();

            Assert.IsInstanceOf <DynamicProxyGenerationOptionAdapter>(generationOptions);

            AssertInterceptors(testService, interceptors);
        }
Esempio n. 10
0
        public static void RegisterDependency()
        {
//            //所有DLL路径
//            var dllPaths =
//                Directory.GetFiles(HttpContext.Current.Server.MapPath("/bin"), "*.dll");
//            //所有类型
//            var typeList =
//                dllPaths.Where(p => p.Contains("Complex.dll")).Select(path =>
//                {
//                    try
//                    {
//                        return Assembly.LoadFrom(path);
//                    }
//                    catch (Exception exp)
//                    {
//#if DEBUG
//                        throw exp;
//#endif
//                        //   Logger.Error(string.Format("IOC注册时,加载程序集[{0}]出错!", path), exp);
//                        return (Assembly)null;
//                    }
//                }

//                    ).Where(a => a != null).Select(a =>
//                    {
//                        try
//                        {
//                            return a.GetTypes();
//                        }
//                        catch (Exception exp)
//                        {
//#if DEBUG
//                            throw exp;
//#endif
//                            //  Logger.Error(string.Format("IOC注册时,获取程序集[{0}]内所有类型时出错!", a.GetName().Name), exp);
//                            return new Type[] { };
//                        }
//                    })
//                    .SelectMany(ts => ts).Where(t => t.Namespace != null && t.Namespace.Contains("Complex.Logical.Realization") && t.IsInterface == false && t.IsAbstract == false);



            var typeList =
                Assembly.LoadFrom(HttpContext.Current.Server.MapPath("/bin/") + "Complex.dll").GetTypes().Where(t => t.Namespace != null && t.Namespace.Contains("Realization") && t.IsInterface == false && t.IsAbstract == false);
            //所有ITransientLifetimeManagerRegister类型

            var ITransientLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(ITransientLifetimeManagerRegister)).Length > 0);
            //所有IContainerControlledLifetimeManagerRegister类型
            var IContainerControlledLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IContainerControlledLifetimeManagerRegister)).Length > 0);
            //所有IHierarchicalLifetimeManagerRegister类型
            var IHierarchicalLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IHierarchicalLifetimeManagerRegister)).Length > 0);
            //所有IExternallyControlledLifetimeManagerRegister类型
            var IExternallyControlledLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IExternallyControlledLifetimeManagerRegister)).Length > 0);
            //所有IPerThreadLifetimeManagerRegister类型
            var IPerThreadLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IPerThreadLifetimeManagerRegister)).Length > 0);
            //所有IPerResolveLifetimeManagerRegister类型
            var IPerResolveLifetimeManagerRegisterls =
                typeList.Where(
                    t => t.FindInterfaces((tt, o) => o.Equals(tt), typeof(IPerResolveLifetimeManagerRegister)).Length > 0);

            //注册ITransientLifetimeManagerRegister类型
            foreach (var t in ITransientLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ((ICO_AOPEnableAttribute)tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false)).ICOEnable, typeof(ITransientLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute     ds = (ICOConfigAttribute)t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute)iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if (ia.AOPEnable)
                    {
                        var  generator = new DynamicProxyGenerator(t, iType);
                        Type type      = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }
            //注册IContainerControlledLifetimeManagerRegister类型
            foreach (var t in IContainerControlledLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ((ICO_AOPEnableAttribute)tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false)).ICOEnable, typeof(IContainerControlledLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute     ds = (ICOConfigAttribute)t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute)iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if (ia.AOPEnable)
                    {
                        var  generator = new DynamicProxyGenerator(t, iType);
                        Type type      = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }

            //注册IHierarchicalLifetimeManagerRegister类型
            foreach (var t in IHierarchicalLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ((ICO_AOPEnableAttribute)tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false)).ICOEnable, typeof(IHierarchicalLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute     ds = (ICOConfigAttribute)t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute)iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if (ia.AOPEnable)
                    {
                        var  generator = new DynamicProxyGenerator(t, iType);
                        Type type      = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }

            //注册IExternallyControlledLifetimeManagerRegister类型
            foreach (var t in IExternallyControlledLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ((ICO_AOPEnableAttribute)tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false)).ICOEnable, typeof(IExternallyControlledLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute     ds = (ICOConfigAttribute)t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute)iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if (ia.AOPEnable)
                    {
                        var  generator = new DynamicProxyGenerator(t, iType);
                        Type type      = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }
            //注册IPerThreadLifetimeManagerRegister类型
            foreach (var t in IPerThreadLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ((ICO_AOPEnableAttribute)tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false)).ICOEnable, typeof(IPerThreadLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute     ds = (ICOConfigAttribute)t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute)iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if (ia.AOPEnable)
                    {
                        var  generator = new DynamicProxyGenerator(t, iType);
                        Type type      = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }
            //注册IPerResolveLifetimeManagerRegister类型
            foreach (var t in IPerResolveLifetimeManagerRegisterls)
            {
                var implementInterfaceList = t.FindInterfaces((tt, o) => !o.Equals(tt) && tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false) != null && ((ICO_AOPEnableAttribute)tt.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false)).ICOEnable, typeof(IPerResolveLifetimeManagerRegister));
                foreach (var iType in implementInterfaceList)
                {
                    ICOConfigAttribute     ds = (ICOConfigAttribute)t.GetCustomAttribute(typeof(ICOConfigAttribute), false);
                    ICO_AOPEnableAttribute ia = (ICO_AOPEnableAttribute)iType.GetCustomAttribute(typeof(ICO_AOPEnableAttribute), false);

                    if (ia.AOPEnable)
                    {
                        var  generator = new DynamicProxyGenerator(t, iType);
                        Type type      = generator.GenerateType();
                        DependencyUnityContainer.Current.RegisterType(iType, type, ds.Description, new TransientLifetimeManager());
                    }
                    else
                    {
                        DependencyUnityContainer.Current.RegisterType(iType, t, ds.Description, new TransientLifetimeManager());
                    }
                }
            }
        }
Esempio n. 11
0
 /// <summary>
 /// 根据主键生成代理类,代理类只监控调用了Set方法的属性,并保存属性名到列表中,最后通过GetModifiedProperties获取变更的属性名
 /// 可以根据修改的属性调用支持部分字段更新的ORM框架,或自动生成sql语句进行更新
 /// </summary>
 /// <returns></returns>
 public TEntity GenerateProxy()
 {
     return(DynamicProxyGenerator.CreateDynamicProxy <TEntity>());
 }