/// <summary>
        /// Configures the factory instance
        /// </summary>
        static DapperAccessLayer()
        {
            s_Factory.ImplementAttributedMethods <QueryAttribute>()
            .UsingSharedExecuter(InitQuery, ExecQuery)
            .WithSyntaxChecker(CheckQuery);

            s_Factory.ImplementAttributedMethods <NonQueryAttribute>()
            .UsingSharedExecuter(InitNonQuery, ExecNonQuery)
            .WithSyntaxChecker(CheckNonQuery);

            s_ParameterFactory.ImplementProperties().UsingBackingField();
        }
        static TestAccessLayer()
        {
            s_Factory = new AdaptiveFactory <TestAccessLayer>();

            // Method handlers
            s_Factory.ImplementMethods(m => m.Name == "A").UsingSharedExecuter(InitA, ExecA);
            s_Factory.ImplementMethods(m => m.Name == "B").UsingSharedExecuter(InitB, ExecB);
            s_Factory.ImplementMethods(m => m.Name == "C").UsingSharedExecuter("InitC", "ExecC");

            // Property handlers
            s_Factory.ImplementProperties(p => p.Name.StartsWith("P")).UsingGetterAndSetter(InitP, GetP, SetP);
            s_Factory.ImplementProperties(p => p.Name.StartsWith("Q")).UsingGetterAndSetter(InitQ, GetP, SetP);
            s_Factory.ImplementProperties(p => p.Name.StartsWith("R"))
            .UsingBackingField()
            .WithGetInspector(GetInspector)
            .WithGetInspector("GenericGetInspector")
            .WithSetInspector("SetInspector", "SetInspector");

            // Event handlers
            s_Factory.ImplementEvents().UsingAdderAndRemover("InitEvent", "AddEvent", "RemoveEvent");

            // TODO: State without initializer, generic getter/setter, setter does not work, split getter/setter
        }
        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 #4
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);
        }
Exemple #5
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"));
        }