Exemple #1
0
        public void TestInheritedInterfaces()
        {
            var factory = new AdaptiveFactory <Base>();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            Type ibase    = factory.Implement(typeof(IBase));
            Type iderived = factory.Implement(typeof(IDerived));

            Assert.AreNotSame(ibase, iderived);
            Assert.IsTrue(typeof(IDisposable).IsAssignableFrom(ibase));
            Assert.IsTrue(typeof(IBase).IsAssignableFrom(ibase));
            Assert.IsTrue(typeof(IBase).IsAssignableFrom(iderived));
            Assert.IsTrue(typeof(IDerived).IsAssignableFrom(iderived));

            using (IBase b = (IBase)Activator.CreateInstance(ibase))
            {
                Assert.IsNotNull(b.DoSomething());
                Assert.AreEqual("DoSomething", b.DoSomething().Name);
            }
            using (IDerived d = (IDerived)Activator.CreateInstance(iderived))
            {
                Assert.AreEqual("DoSomething", d.DoSomething().Name);
                Assert.AreEqual("DoMore", d.DoMore().Name);
            }
        }
        private static CommandInfo InitCommand(DapperAccessLayer layer, MethodInfo method)
        {
            CommandAttribute attr = method.GetCustomAttributes(typeof(CommandAttribute), false).Cast <CommandAttribute>().Single();

            CommandInfo info = new CommandInfo()
            {
                Attribute      = attr,
                TimeoutSeconds = attr.CommandTimeout > 0 ? attr.CommandTimeout : (int?)null
            };

            // Find all method parameters that shall be wrapped by runtime DTO type
            PropertyDefinition[] properties = method.GetParameters()
                                              .Where(p => IsDatabaseType(p.ParameterType))
                                              .Select(p => new PropertyDefinition(p.Name, p.ParameterType))
                                              .ToArray();

            // If any, create runtime dto type
            if (properties.Length > 0)
            {
                // First create an interface definining the properties
                string interfaceTypeName = method.DeclaringType.FullName.Replace('+', '_') + "." + method.Name + "_" + method.GetHashCode().ToString();
                Type   interfaceType     = s_Factory.Module.Assembly.GetType(interfaceTypeName);
                if (interfaceType == null)
                {
                    interfaceType = s_Factory.Module.DefineInterface(interfaceTypeName, null, properties);
                }

                // Then implement that by runtime dto type
                info.ParameterType           = s_ParameterFactory.Implement(interfaceType);
                info.ParameterTypeProperties = info.ParameterType.GetProperties();
            }

            return(info);
        }
        public void DtoFactoryTest()
        {
            AdaptiveFactory <object> rawFactory = AdaptiveFactory.CreateDtoFactory <object>(null, null);
            IDTO dto = Activator.CreateInstance(rawFactory.Implement <IDTO>()) as IDTO;

            dto.Key = 42;
            Assert.AreEqual(42, dto.Key);

            AdaptiveFactory <NotifyPropertySub> changeFactory = AdaptiveFactory.CreateDtoFactory <NotifyPropertySub>("OnChanging", "OnChanged");

            dto = Activator.CreateInstance(changeFactory.Implement <IDTO>()) as IDTO;
            Assert.IsTrue(dto is INotifyPropertyChanged);
            Assert.IsTrue(dto is INotifyPropertyChanging);

            List <string> changed  = new List <string>();
            List <string> changing = new List <string>();

            ((INotifyPropertyChanged)dto).PropertyChanged   += (s, e) => changed.Add(e.PropertyName);
            ((INotifyPropertyChanging)dto).PropertyChanging += (s, e) => changing.Add(e.PropertyName);

            dto.Key = 42;
            Assert.AreEqual(1, changed.Count);
            Assert.AreEqual(1, changing.Count);
            Assert.AreEqual("Key", changed[0]);
        }
        public void ObjectAsBaseTest()
        {
            //TODO: Same for props and events
            //Better naming
            // Comments
            // out and ref params
            // rewrite test, one big with all features and lots of handlers

            // Create factory using plain object as base
            var factory = new AdaptiveFactory <object>();

            factory.ImplementMethods(m => m.Name == "Max").UsingSharedExecuter(GetMaxHandler);
            factory.ImplementMethods().UsingSharedExecuter(InitObjectAsBase, ExecObjectAsBase);
            factory.ImplementProperties(p => p.PropertyType == typeof(int)).UsingGetterAndSetter(InitProp, GetIntProp, SetIntProp);
            factory.ImplementEvents().UsingAdderAndRemover(InitEvent, AddEvent, RemoveEvent);
            //factory.AddMethodHandler<string>().ForAllMethods().WithInitializer(InitObjectAsBase).WithExecutor(ExecObjectAsBase);

            // Create implementation
            Type type = factory.Implement(typeof(ILayer));

            Assert.AreEqual(typeof(object), type.BaseType);
            Assert.AreNotEqual(typeof(object), type);

            // Verify same implementation next time
            Assert.AreSame(type, factory.Implement(typeof(ILayer)));

            // Create instance and call it
            ILayer layer = (ILayer)Activator.CreateInstance(type);

            Assert.AreEqual(3, layer.Add(1, 2));
            Assert.AreEqual(30, layer.Add(10, 20));

            var d = layer.GetOnly;

            layer.GetAndSet = 22;
            layer.Fire     += layer_Fire;
            layer.Fire     -= layer_Fire;
            // TODO: Impl setter, check box/unbox scenarios, generic setter/getter, generic TInfo

            int max = layer.Max(1, 3, 2);

            Assert.AreEqual(3, max);
        }
Exemple #5
0
        public void TestGenericBase()
        {
            var factory = new AdaptiveFactory <GenericBase <int> >();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            IGeneric <int> g = Activator.CreateInstance(factory.Implement <IGeneric <int> >()) as IGeneric <int>;

            Assert.AreEqual("DoGeneric", g.DoGeneric(42).Name);
            Assert.IsTrue(g is GenericBase <int>);
        }
Exemple #6
0
        public void TestGenericInterface()
        {
            var factory = new AdaptiveFactory <Base>();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            Type           t = factory.Implement <IGeneric <int> >();
            IGeneric <int> g = (IGeneric <int>)Activator.CreateInstance(t);

            Assert.AreEqual("DoGeneric", g.DoGeneric(42).Name);
        }
Exemple #7
0
        public void TestMultipleFactories()
        {
            var factory1 = new AdaptiveFactory <GenericBase <int> >();

            factory1.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            var factory2 = new AdaptiveFactory <GenericBase <int> >();

            factory2.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            Type t1 = factory1.Implement <IGeneric <int> >();
            Type t2 = factory2.Implement <IGeneric <int> >();

            Assert.AreNotSame(t1, t2);
        }
Exemple #8
0
        public void InvalidMethodSignaturesTest()
        {
            var factory = new AdaptiveFactory <LayerBase>();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec2");

            try
            {
                factory.Implement <IRefParamInterface>();
                Assert.Fail();
            }
            catch (ArgumentException)
            {
            }
        }
Exemple #9
0
        public void ValidMethodSignaturesTest()
        {
            var factory = new AdaptiveFactory <LayerBase>();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec");
            factory.ImplementProperties(p => true).UsingGetterAndSetter("Init", "Get", "Set");

            Type            type  = factory.Implement(typeof(IValidInterface));
            IValidInterface layer = (IValidInterface)Activator.CreateInstance(type);

            layer.VoidMethod();
            layer.SingleArgument(0);
            layer.DefaultArgument();
            Assert.AreEqual(42, layer.ValueTypeReturn());
            Assert.AreEqual("ABC", string.Join("", layer.RefTypeReturn()));

            layer.Number = 42;
            Assert.AreEqual(42, layer.Number);
        }
        public void ProxyFactoryTest()
        {
            AdaptiveFactory <ProxyBase> proxyFactory = AdaptiveFactory.CreateProxyFactory <ProxyBase>("Target");

            Assert.AreEqual(1, proxyFactory.MethodHandlers.Count);
            Assert.AreEqual(0, proxyFactory.PropertyHandlers.Count);
            Assert.AreEqual(0, proxyFactory.EventHandlers.Count);

            Target  target = new Target();
            ITarget proxy  = (ITarget)Activator.CreateInstance(proxyFactory.Implement <ITarget>(), target);

            Assert.IsNotNull(proxy);

            Assert.AreEqual(5, proxy.Add(2, 3));
            target.Value = 42;
            Assert.AreEqual(42, proxy.Value);
            proxy.Value = 21;
            Assert.AreEqual(21, target.Value);
        }
Exemple #11
0
        public void DefineInterfaceTest()
        {
            Type t = typeof(II);

            Type type = AdaptiveFactory.DefaultModule.DefineInterface("X", null, new PropertyDefinition("A", typeof(int)));

            Assert.IsTrue(type.IsInterface);
            Assert.IsTrue(type.IsPublic);
            Assert.IsNotNull(type.GetProperty("A"));
            Assert.IsTrue(type.GetProperty("A").CanRead);
            Assert.IsTrue(type.GetProperty("A").CanWrite);

            AdaptiveFactory <object> factory = new AdaptiveFactory <object>();

            factory.ImplementProperties().UsingBackingField();

            Type dtoType = factory.Implement(type);

            Assert.IsTrue(type.IsAssignableFrom(dtoType));
            Assert.IsNotNull(dtoType.GetProperty("A"));
        }
 /// <summary>
 /// Creates/gets runtime type that implements given interface.
 /// </summary>
 /// <param name="interfaceType"></param>
 /// <returns></returns>
 public static Type Implement(Type interfaceType)
 {
     return(s_Factory.Implement(interfaceType));
 }
 /// <summary>
 /// Implements type that implements given interface type T.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static Type Implement <T>()
 {
     return(s_Factory.Implement <T>());
 }
 public static T Create <T>() where T : class
 {
     return((T)Activator.CreateInstance(s_Factory.Implement <T>()));
 }