public DomainEntityFactory(Action <T> initializer = null) { var concreteType = ProxyTypeBuilder.BuildDomainEntityProxy <T>(); New = new TypeConstructor <T, TArg1, TArg2>(concreteType, x => { initializer?.Invoke(x); x.OnCreated(); }); Existing = new TypeConstructor <T, TArg1, TArg2>(concreteType, x => { initializer?.Invoke(x); x.OnLoaded(); }); }
public void GetInstanceTest() { var person = ProxyTypeBuilder.GetInstace <IPerson>(); person.Name = "Okan"; person.Surname = "Gunes"; }
static void Main() { ProxyTest test = new ProxyTest(); test.SuperEvent1 += new EventHandler(test_SuperEvent1); Program sink = new Program(); ProxyTypeBuilder builder = new ProxyTypeBuilder("ProxyBuilder"); //Type newType = builder.GenerateInterfaceProxyImplementation<ISampleFace>(); ISampleFace proxy = builder.ObtainProxyInstance <ISampleFace>(sink); Stopwatch sw = new Stopwatch(); Thread.Sleep(500); sw.Start(); for (int i = 0; i < 10000000; i++) { proxy.TestMethod(12, null); object t = proxy.TestMethod2(13, sink); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); //builder.Save(); //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); }
void IProxyTypeSink.ReceivePropertySet(int methodId, object value) { SuperPoolProxyCall pendingCall = null; if (_pendingThreadsCalls.TryGetValue(Thread.CurrentThread.ManagedThreadId, out pendingCall) == false) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find corresponding thread proxy call information."); #endif return; } pendingCall.Parameters = new object[] { value }; pendingCall.ReturnType = null; ProxyTypeBuilder builder = ProxyTypeBuilder; if (builder == null) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find proxy type builder."); #endif return; } pendingCall.MethodInfo = builder.GetMethodInfoById(methodId); if (pendingCall.MethodInfo == null) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find method [" + methodId + "] info."); #endif return; } pendingCall.Sender.ProcessCall(pendingCall); }
/// <summary> /// Process a pool call. /// </summary> object ProcessReceiveCall(int methodId, Type returnType, object[] parameters) { SuperPoolProxyCall pendingCall = null; if (_pendingThreadsCalls.TryGetValue(Thread.CurrentThread.ManagedThreadId, out pendingCall) == false) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find corresponding thread proxy call information."); #endif return(null); } pendingCall.Parameters = parameters; pendingCall.ReturnType = returnType; ProxyTypeBuilder builder = ProxyTypeBuilder; if (builder == null) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find proxy type builder."); #endif return(ProxyTypeManager.GetTypeDefaultValue(returnType)); } pendingCall.MethodInfo = builder.GetMethodInfoById(methodId); if (pendingCall.MethodInfo == null) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find method [" + methodId + "] info."); #endif return(ProxyTypeManager.GetTypeDefaultValue(returnType)); } return(pendingCall.Sender.ProcessCall(pendingCall)); }
/// <summary> /// 创建实例工厂 /// </summary> /// <exception cref="NotSupportedException"></exception> /// <exception cref="ProxyTypeCreateException"></exception> /// <returns></returns> protected override Func <IActionInterceptor, THttpApi> CreateFactory() { var actionInvokers = this.GetActionInvokers(); var proxyType = ProxyTypeBuilder.Build(typeof(THttpApi), actionInvokers); var proxyTypeCtor = Lambda.CreateCtorFunc <IActionInterceptor, IActionInvoker[], THttpApi>(proxyType); return((interceptor) => proxyTypeCtor(interceptor, actionInvokers)); }
// <summary> // Build a CLR proxy type for the supplied EntityType. // </summary> // <param name="ospaceEntityType"> EntityType in O-Space that represents the CLR type to be proxied. </param> // <returns> EntityProxyTypeInfo object that contains the constructed proxy type, along with any behaviors associated with that type; or null if a proxy type cannot be constructed for the specified EntityType. </returns> private static EntityProxyTypeInfo BuildType( ModuleBuilder moduleBuilder, ClrEntityType ospaceEntityType, MetadataWorkspace workspace) { Debug.Assert( _typeMapLock.IsUpgradeableReadLockHeld, "EntityProxyTypeInfo.BuildType method was called without first acquiring an upgradeable read lock from _typeMapLock."); EntityProxyTypeInfo proxyTypeInfo; var proxyTypeBuilder = new ProxyTypeBuilder(ospaceEntityType); var proxyType = proxyTypeBuilder.CreateType(moduleBuilder); if (proxyType != null) { // Set the runtime assembly of the proxy types if it hasn't already been set. // This is used by the IsProxyType method. var typeAssembly = proxyType.Assembly(); if (!_proxyRuntimeAssemblies.Contains(typeAssembly)) { _proxyRuntimeAssemblies.Add(typeAssembly); AddAssemblyToResolveList(typeAssembly); } proxyTypeInfo = new EntityProxyTypeInfo( proxyType, ospaceEntityType, proxyTypeBuilder.CreateInitalizeCollectionMethod(proxyType), proxyTypeBuilder.BaseGetters, proxyTypeBuilder.BaseSetters, workspace); foreach (var member in proxyTypeBuilder.LazyLoadMembers) { InterceptMember(member, proxyType, proxyTypeInfo); } SetResetFKSetterFlagDelegate(proxyType, proxyTypeInfo); SetCompareByteArraysDelegate(proxyType); } else { proxyTypeInfo = null; } return(proxyTypeInfo); }
public Type GetActualTypeToCreate(Type objectType) { //Assert type if (objectType.IsSubclassOf(typeof(DomainEntity))) { return(ProxyTypeBuilder.BuildDomainEntityProxy(objectType)); } else if (objectType.IsSubclassOfGeneric(typeof(DomainValueObject <>))) { return(ProxyTypeBuilder.BuildSerializableProxy(objectType)); } else { return(objectType); } }
public void ProxyTypeBuilderTest() { var customerProxy = ProxyTypeBuilder.BuildDomainEntityProxy <Customer>(); var customer = (Customer)customerProxy.GetConstructorsEx().First().InvokeWithDefaults(); customer.ChangeTracker.BeginTracking(); customer.MainAddress = new Address("", "", "", "", null, null); customer.Code = 1; customer.Name = "12"; customer.Name = "121"; customer.Name = "121"; customer.Quantity = 1; //var c = ProxyTypeBuilder.CreateInstance<Customer>(() => new Customer(1, "123")); var c1 = ProxyTypeBuilder.CreateInstance(() => new Customer(1, customer.Name)); }
public void TypeConstructorTest() { var ctor1 = new TypeConstructor <Customer, int, string>(ProxyTypeBuilder.BuildDomainEntityProxy <Customer>(), (Customer x) => x.OnLoaded()); var id = 1; var name = "someone"; var c1 = ctor1.Construct(new object[] { id, name }); Assert.IsTrue(c1.Id == id && c1.Name == name && c1.GetEntityState() == EntityState.Existing); c1 = ctor1.Construct(("id", id), ("name", name)); Assert.IsTrue(c1.Id == id && c1.Name == name && c1.GetEntityState() == EntityState.Existing); c1 = ctor1.Construct(1, name); Assert.IsTrue(c1.Id == id && c1.Name == name && c1.GetEntityState() == EntityState.Existing); c1 = ctor1.ConstructWithDefaults(("id", id)); Assert.IsTrue(c1.Id == id && c1.Name == null && c1.GetEntityState() == EntityState.Existing); Assert.Throws <InvalidOperationException>(() => c1 = ctor1.Construct(("id", id))); }
/// <summary> /// Build a CLR proxy type for the supplied EntityType. /// </summary> /// <param name="ospaceEntityType"> EntityType in O-Space that represents the CLR type to be proxied. </param> /// <returns> EntityProxyTypeInfo object that contains the constructed proxy type, along with any behaviors associated with that type; or null if a proxy type cannot be constructed for the specified EntityType. </returns> private static EntityProxyTypeInfo BuildType( ModuleBuilder moduleBuilder, ClrEntityType ospaceEntityType, MetadataWorkspace workspace) { Debug.Assert( _typeMapLock.IsUpgradeableReadLockHeld, "EntityProxyTypeInfo.BuildType method was called without first acquiring an upgradeable read lock from _typeMapLock."); EntityProxyTypeInfo proxyTypeInfo; var proxyTypeBuilder = new ProxyTypeBuilder(ospaceEntityType); var proxyType = proxyTypeBuilder.CreateType(moduleBuilder); if (proxyType != null) { // Set the runtime assembly of the proxy types if it hasn't already been set. // This is used by the IsProxyType method. var typeAssembly = proxyType.Assembly; if (!_proxyRuntimeAssemblies.Contains(typeAssembly)) { _proxyRuntimeAssemblies.Add(typeAssembly); AddAssemblyToResolveList(typeAssembly); } proxyTypeInfo = new EntityProxyTypeInfo( proxyType, ospaceEntityType, proxyTypeBuilder.CreateInitalizeCollectionMethod(proxyType), proxyTypeBuilder.BaseGetters, proxyTypeBuilder.BaseSetters, workspace); foreach (var member in proxyTypeBuilder.LazyLoadMembers) { InterceptMember(member, proxyType, proxyTypeInfo); } SetResetFKSetterFlagDelegate(proxyType, proxyTypeInfo); SetCompareByteArraysDelegate(proxyType); } else { proxyTypeInfo = null; } return proxyTypeInfo; }
/// <summary> /// Handle event subscription (Proxy.Event.Subscribe) /// </summary> protected override void ProcessReceiveEventSubscription(int methodId, Delegate delegateInstance, bool isAdd) { SuperPoolProxyCall pendingCall = null; if (_pendingThreadsCalls.TryGetValue(Thread.CurrentThread.ManagedThreadId, out pendingCall) == false) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find corresponding thread proxy call information."); #endif return; } EventSubscriptionRequest subscriptionRequest = pendingCall.SubscriptionRequest; if (subscriptionRequest == null) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find corresponding subscription requests, event subscription failed."); #endif return; } if (pendingCall.Sender == null || pendingCall.Sender.Id == null) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to establish subscription sender information, subscription failed."); #endif return; } if (delegateInstance.Target != pendingCall.Sender.Source) { #if Matrix_Diagnostics InstanceMonitor.Error("Only a message super pool client source can subscribe to events."); #endif return; } ProxyTypeBuilder builder = ProxyTypeBuilder; if (builder == null) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find proxy type builder, event subscription failed."); #endif return; } GeneratedMethodInfo generatedMethodInfo = builder.GetMethodInfoById(methodId); if (generatedMethodInfo == null) { #if Matrix_Diagnostics InstanceMonitor.OperationError("Failed to find method [id, " + methodId + "] info, event subscription failed."); #endif return; } if (string.IsNullOrEmpty(generatedMethodInfo.EventName)) { generatedMethodInfo.EventName = GeneralHelper.GetEventExtendedNameByMethod(generatedMethodInfo.GetMethodInfo(), false, true); } // generatedMethodInfo.GetMethodInfo() >> I2.add_AEVent string extendedEventName = generatedMethodInfo.EventName; MethodInfo eventAddMethodInfo = generatedMethodInfo.GetMethodInfo(); // *IMPORTANT* the Call<> will cause the currently used pendingCall to be repopulated with information, // so we ned to extract the *sender id* BEFORE calling the actual Call(), since it will change the // pendingCall instance immediately. subscriptionRequest.SenderId = pendingCall.Sender.Id; subscriptionRequest.ExtendedEventName = extendedEventName; subscriptionRequest.IsAdd = isAdd; //subscriptionRequest.EventAddMethodInfo = eventAddMethodInfo; subscriptionRequest.DelegateInstanceMethodInfo = delegateInstance.Method; // Process locally. ((ISuperPoolIntercom)this).ProcessSubscriptionUpdate(subscriptionRequest); SuperPoolClient mainClient = IntercomClient; if (mainClient == null) { #if Matrix_Diagnostics InstanceMonitor.Error("Failed to obtain super pool main intercom client, so new client handling has failed."); #endif } else { // Notify other connected super pools of this subcription, // since the subscribee(s) may be attached on them. // *pendingCall swap done here, make sure to not use it on or after this line* mainClient.CallAll <ISuperPoolIntercom>().ProcessSubscriptionUpdate(subscriptionRequest); } }
static void Main() { ProxyTest test = new ProxyTest(); test.SuperEvent1 += new EventHandler(test_SuperEvent1); Program sink = new Program(); ProxyTypeBuilder builder = new ProxyTypeBuilder("ProxyBuilder"); //Type newType = builder.GenerateInterfaceProxyImplementation<ISampleFace>(); ISampleFace proxy = builder.ObtainProxyInstance<ISampleFace>(sink); Stopwatch sw = new Stopwatch(); Thread.Sleep(500); sw.Start(); for (int i = 0; i < 10000000; i++) { proxy.TestMethod(12, null); object t = proxy.TestMethod2(13, sink); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); //builder.Save(); //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); }