private void InitializeDefaultSnapshot() { Guid snapshotId = Guid.NewGuid(); var snapshotNode = new Node <Guid, object, EdgeData>(NodeType.Snapshot, null); Guid rootObjectId = objectInstancesService.NewInstance(typesService.GetTypeId(rootEntityType)); snapshotNode.AddEdge(new Edge <Guid, EdgeData>(rootObjectId, new EdgeData(EdgeType.RootObject, null))); provider.SetNode(snapshotId, snapshotNode); snapshotsService.AddSnapshot(snapshotId); }
public Guid GetTypeId(Type type) { return(typesService.GetTypeId(type)); }
/// <summary> /// Generates entity proxy type for given entity interface /// </summary> /// <param name="type">Interface type, it must be registered previously with the types service</param> /// <param name="saveAssemblyToDisk">Defines if assembly should be saved to disk</param> /// <param name="assemblyFileName">If assembly saving is on, defines path and file name of the generated assembly</param> /// <returns>Generated proxy type</returns> public Type GenerateProxyType(Type type, bool saveAssemblyToDisk, string assemblyFileName) { Type dictionaryType = null; if (Utils.IsDictionaryType(type, ref dictionaryType)) { return(GenerateDictionaryProxyType(type)); } Type collectionType = null; if (Utils.IsCollectionType(type, ref collectionType)) { return(GenerateCollectionProxyType(type)); } if (!type.IsInterface) { throw new ArgumentException("Type should be an interface:" + type.AssemblyQualifiedName); } Guid typeId = typesService.GetTypeId(type); if (typeId == Guid.Empty) { throw new ArgumentException("Type not registered:" + type.AssemblyQualifiedName); } AssemblyName aName = new AssemblyName(Constants.GeneratedAssemblyName); AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.RunAndSave); // For a single-module assembly, the module name is usually // the assembly name plus an extension. ModuleBuilder mb = ab.DefineDynamicModule(aName.Name); // Create the type. TypeBuilder typeBuilder = mb.DefineType(type.Name + Constants.ProxyTypeSufix, TypeAttributes.Public); typeBuilder.AddInterfaceImplementation(type); FieldBuilder fbReadOnly = typeBuilder.DefineField(Constants.ReadOnlyFieldName, typeof(Boolean), FieldAttributes.Public); FieldBuilder fbinstaceId = typeBuilder.DefineField(Constants.InstanceIdFieldName, typeof(Guid), FieldAttributes.Public); FieldBuilder fbPrimaryKeyId = typeBuilder.DefineField(Constants.PrimaryKeyIdFieldName, typeof(Guid), FieldAttributes.Public | FieldAttributes.Static); FieldBuilder fbTypeId = typeBuilder.DefineField(Constants.TypeIdFieldName, typeof(Guid), FieldAttributes.Private | FieldAttributes.Static); FieldBuilder fbFacade = typeBuilder.DefineField(Constants.FacadeFieldName, typeof(IRuntimeProxyFacade), FieldAttributes.Private); // Define the constructor for proxy (IRuntimeProxyFacade facade, Guid instanceId, Boolean readOnly) Type[] parameterTypes = { typeof(IRuntimeProxyFacade), typeof(Guid), typeof(Boolean) }; ConstructorBuilder constructor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes); ILGenerator constructorIL = constructor.GetILGenerator(); constructorIL.Emit(OpCodes.Ldarg_0); constructorIL.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes)); constructorIL.Emit(OpCodes.Ldarg_0); constructorIL.Emit(OpCodes.Ldarg_2); constructorIL.Emit(OpCodes.Stfld, fbinstaceId); constructorIL.Emit(OpCodes.Ldarg_0); constructorIL.Emit(OpCodes.Ldarg_3); constructorIL.Emit(OpCodes.Stfld, fbReadOnly); constructorIL.Emit(OpCodes.Ldarg_0); constructorIL.Emit(OpCodes.Ldarg_1); constructorIL.Emit(OpCodes.Stfld, fbFacade); constructorIL.Emit(OpCodes.Ret); // Define the static constructor for type ConstructorBuilder staticConstructor = typeBuilder.DefineTypeInitializer(); ILGenerator staticConstructorIL = staticConstructor.GetILGenerator(); staticConstructorIL.DeclareLocal(typeof(StaticProxyFacade)); staticConstructorIL.Emit(OpCodes.Nop); staticConstructorIL.Emit(OpCodes.Call, typeof(MethodBase).GetMethod("GetCurrentMethod")); staticConstructorIL.Emit(OpCodes.Callvirt, typeof(Type).GetMethod("get_DeclaringType")); staticConstructorIL.Emit(OpCodes.Callvirt, typeof(Type).GetMethod("get_GUID")); staticConstructorIL.Emit(OpCodes.Stsfld, fbTypeId); // Set result to static field typeId staticConstructorIL.Emit(OpCodes.Call, typeof(StaticProxyFacade).GetMethod("get_Instance")); // Get instance of proxy facade staticConstructorIL.Emit(OpCodes.Stloc_0); // Set to local 0 staticConstructorIL.Emit(OpCodes.Ldloc_0); staticConstructorIL.Emit(OpCodes.Ldsfld, fbTypeId); staticConstructorIL.Emit(OpCodes.Callvirt, typeof(StaticProxyFacade).GetMethod("GetTypePrimaryKeyMemberId")); //Get id of primary key member staticConstructorIL.Emit(OpCodes.Stsfld, fbPrimaryKeyId); //Set to static field Collection <PropertyInfo> properties = new Collection <PropertyInfo>(); // Get list of properties from the inherited interfaces also Utils.ExtractProperties(type, properties); // define all properties and related code in static constructor foreach (PropertyInfo propertyInfo in properties) { if (IsRevisionIdProperty(propertyInfo)) { GenerateRevisionIdProperty(typeBuilder, propertyInfo, fbinstaceId); } else if (IsDataProperty(propertyInfo)) { GenerateDataProperty(staticConstructorIL, typeBuilder, propertyInfo, propertyInfo.Name, fbinstaceId, fbTypeId, fbReadOnly, fbFacade); } else { throw new ArgumentException("Invalid property " + propertyInfo.ToString() + " in " + type.ToString()); } } //finish the static constructor. staticConstructorIL.Emit(OpCodes.Ret); // Generate destructor ILGenerator finalizerIL = typeBuilder.DefineMethod("Finalize", MethodAttributes.Virtual | MethodAttributes.Family | MethodAttributes.HideBySig).GetILGenerator(); finalizerIL.BeginExceptionBlock(); finalizerIL.Emit(OpCodes.Ldarg_0); finalizerIL.Emit(OpCodes.Ldfld, fbFacade); finalizerIL.Emit(OpCodes.Ldarg_0); finalizerIL.Emit(OpCodes.Callvirt, typeof(IRuntimeProxyFacade).GetMethod("ProxyCollected")); finalizerIL.BeginFinallyBlock(); finalizerIL.Emit(OpCodes.Ldarg_0); finalizerIL.Emit(OpCodes.Call, typeof(Object).GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance)); finalizerIL.EndExceptionBlock(); finalizerIL.Emit(OpCodes.Ret); AddEqualsHashcode(typeBuilder); //Add GUID attribute which corresponds to registered type ID typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(GuidAttribute).GetConstructor(new Type[] { typeof(string) }), new object[] { typeId.ToString() })); // Finish the type. Type generatedType = typeBuilder.CreateType(); if (saveAssemblyToDisk) { ab.Save(assemblyFileName); } return(generatedType); }