public void SetupEntityClasses(EntityModel model) { var ns = model.App.GetType().Namespace + ".Proxies"; _assemblyInfo = _assemblyInfo ?? DynamicAssemblyInfo.Create(ns); var attrHandler = new AttributeHandler(addStandardAttributes: true); foreach (var ent in model.Entities) { var className = ns + "." + ent.EntityType.Name.Substring(1); //Cut-off I var controller = new EntityEmitController(ent, _assemblyInfo, className, attrHandler); var emitter = new ProxyEmitter(controller); emitter.ImplementInterface(ent.EntityType); var classType = emitter.CreateClass(); var factory = emitter.GetProxyFactory <Func <EntityRecord, EntityBase> >(); ent.ClassInfo = new EntityClassInfo() { Type = classType, CreateInstance = factory }; //assign member Clr info var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; foreach (var member in ent.Members) { member.ClrClassMemberInfo = classType.GetProperty(member.MemberName, flags); } } }
public void TestFuncProxy() { // create ProxyInfo var asm = DynamicAssemblyInfo.Create("Proxemity.UnitTests.FuncProxies"); var controller = new FuncProxyEmitController(asm); // Create emitter var emitter = new ProxyEmitter(controller); emitter.ImplementInterface(typeof(ISampleFuncInterface)); // create type var proxyType = emitter.CreateClass(); // create and setup target object; FuncProxyBase has a single constructor which accepts single parameter (Target) // emitter will build similar constructor and static factory method on the generated proxy. var target = new FuncProxyTarget(); var factory = emitter.GetProxyFactory <Func <FuncProxyTarget, FuncProxyBase> >(); var proxy = factory(target); // cast the entity instance to entity interface and test it var iFunc = proxy as ISampleFuncInterface; Assert.IsNotNull(iFunc, "Failed to retrieve IFunc interface."); var d = DateTime.Now; var obj = new object(); var iRes = iFunc.IntMethod(d, obj); Assert.AreEqual("IntMethod", target.LastMethod, "Method name mismatch"); Assert.AreEqual(d, target.LastArgs[0]); Assert.AreEqual(obj, target.LastArgs[1]); Assert.AreEqual(proxy, target.ExtraValues[0]); //should be reference to proxy instance itself - see FuncProxyEmitController Assert.AreEqual(Singleton.InstanceField, target.ExtraValues[1]); Assert.AreEqual(Singleton.InstanceProp, target.ExtraValues[2]); iFunc.VoidMethod("a", 123); Assert.AreEqual("VoidMethod", target.LastMethod); Assert.AreEqual("a", (string)target.LastArgs[0]); Assert.AreEqual(123, (int)target.LastArgs[1]); iFunc.BaseMethod("x"); Assert.AreEqual("BaseMethod", target.LastMethod); Assert.AreEqual("x", (string)target.LastArgs[0]); // We specified a custom attribute on class in ProxyInfo (see above) when we initialized emitter var classAttr = proxyType.GetCustomAttributes(false).OfType <SampleCustomAttribute>().FirstOrDefault(); Assert.IsNotNull(classAttr, "Custom attr not found on proxy class."); Assert.AreEqual("v1", classAttr.Value1, "Value1 on attr does not match."); Assert.AreEqual("v2", classAttr.Value2, "Value2 on attr does not match."); //Emit controller creates custom attributes on methods; verify these var intMethod = proxyType.GetMember("IntMethod")[0]; var scAttr = intMethod.GetCustomAttributes(false).OfType <SampleCustomAttribute>().FirstOrDefault(); Assert.IsNotNull(scAttr, "Custom attr not found on method."); Assert.AreEqual("v3", scAttr.Value1, "Value1 on attr does not match."); Assert.AreEqual("v4", scAttr.Value2, "Value2 on attr does not match."); var catAttr = intMethod.GetCustomAttributes(false).OfType <CategoryAttribute>().FirstOrDefault(); Assert.IsNotNull(catAttr, "Category attr not found on method."); Assert.AreEqual("IntMethods", catAttr.Category, "Value1 on attr does not match."); }
public void TestEntityProxy() { var asm = DynamicAssemblyInfo.Create("Proxemity.UnitTests.EntityProxies"); var controller = new EntityEmitController(asm); var emitter = new ProxyEmitter(controller); emitter.ImplementInterface(typeof(IMyEntity)); var proxyType = emitter.CreateClass(); // create and setup target object var factory = emitter.GetProxyFactory <Func <EntityBase> >(); var myEntity = factory(); //that's the way to create instance myEntity.Record = new EntityRecord(); // cast the entity instance to entity interface and test it var iMyEntity = myEntity as IMyEntity; Assert.IsNotNull(iMyEntity, "Failed to retrieve IMyEntity interface."); try { //var x = iMyEntity.IntNProp; // write/read properties iMyEntity.IntProp = 123; var intPropBack = iMyEntity.IntProp; Assert.AreEqual(123, intPropBack, "Returned int prop value does not match."); } catch (Exception ex) { var str = ex.ToString(); throw; } iMyEntity.StringProp = "blah"; var propBack = iMyEntity.StringProp; Assert.AreEqual("blah", propBack, "Returned string prop value does not match."); //check attributes //attribute on member var stringProp = proxyType.GetProperty("StringProp"); var mca = stringProp.GetCustomAttribute <CategoryAttribute>(); Assert.IsNotNull(mca, "Category attr not found on property."); Assert.AreEqual("Strings", mca.Category, "Category value on property does not match."); var ca = proxyType.GetCustomAttribute <CategoryAttribute>(inherit: true); Assert.AreEqual("Cat1", ca.Category, "Category value on type does not match."); var da = proxyType.GetCustomAttribute <DescrAttr>(inherit: true); Assert.IsNotNull(da, "Expected Description attr on class."); Assert.AreEqual("Entity description.", da.Description, "Descr on type does not match."); //attribute on member //var stringProp = proxyType.GetProperty("StringProp"); ca = stringProp.GetCustomAttribute <CategoryAttribute>(); Assert.IsNotNull(ca, "Category attr not found on property."); Assert.AreEqual("Strings", ca.Category, "Category value on property does not match."); var sc = stringProp.GetCustomAttribute <SampleCustomAttribute>(); Assert.IsNotNull(sc, "SampleCustom attr not found on property."); Assert.AreEqual("v1", sc.Value1, "Value1 prop value on property does not match."); Assert.AreEqual("v2", sc.Value2, "Value1 prop value on property does not match."); }