private void buildStorageOperationMethods(DocumentOperations operations, GeneratedType type) { buildOperationMethod(type, operations, "Upsert"); buildOperationMethod(type, operations, "Insert"); buildOperationMethod(type, operations, "Update"); if (_mapping.UseOptimisticConcurrency) { buildOperationMethod(type, operations, "Overwrite"); } else { type.MethodFor("Overwrite").Frames.ThrowNotSupportedException(); } type.MethodFor("DeleteForDocument").Frames.Code($@" return new Marten.Generated.{operations.DeleteById.TypeName}(Identity({{0}})); ", new Use(_mapping.DocumentType)); type.MethodFor("DeleteForId").Frames.Code($@" return new Marten.Generated.{operations.DeleteById.TypeName}({{0}}); ", new Use(_mapping.IdType)); type.MethodFor("DeleteForWhere").Frames.Code($@" return new Marten.Generated.{operations.DeleteByWhere.TypeName}({{0}}); ", Use.Type <IWhereFragment>()); }
// TODO -- just inject the type alias and simplify the operation classes private void buildOperationMethod(GeneratedType type, DocumentOperations operations, string methodName) { var operationType = (GeneratedType)typeof(DocumentOperations).GetProperty(methodName).GetValue(operations); var method = type.MethodFor(methodName); if (_mapping.IsHierarchy()) { method.Frames .Code($@" return new Marten.Generated.{operationType.TypeName} ( {{0}}, Identity({{0}}), {{1}}.Versions.ForType<{_mapping.DocumentType.FullNameInCode()}, {_mapping.IdType.FullNameInCode()}>(), {{2}} );" , new Use(_mapping.DocumentType), Use.Type <IMartenSession>(), Use.Type <DocumentMapping>()); } else { method.Frames .Code($@" return new Marten.Generated.{operationType.TypeName} ( {{0}}, Identity({{0}}), {{1}}.Versions.ForType<{_mapping.DocumentType.FullNameInCode()}, {_mapping.IdType.FullNameInCode()}>() );" , new Use(_mapping.DocumentType), Use.Type <IMartenSession>()); } }
private GeneratedType buildDocumentStorageType(GeneratedAssembly assembly, DocumentOperations operations, string typeName, Type baseType, GeneratedType selectorType) { var type = assembly.AddType(typeName, baseType); writeIdentityMethod(type); buildStorageOperationMethods(operations, type); type.MethodFor(nameof(ISelectClause.BuildSelector)) .Frames.Code($"return new Marten.Generated.{selectorType.TypeName}({{0}}, {{1}});", Use.Type <IMartenSession>(), Use.Type <DocumentMapping>()); buildLoaderCommands(type); writeNotImplementedStubs(type); return(type); }
public DocumentPersistence <T> Generate <T>() { var assembly = new GeneratedAssembly(new GenerationRules("Marten.Generated")); var operations = new DocumentOperations(assembly, _mapping); assembly.Namespaces.Add(typeof(CommandExtensions).Namespace); assembly.Namespaces.Add(typeof(TenantIdArgument).Namespace); assembly.Namespaces.Add(typeof(NpgsqlCommand).Namespace); var queryOnly = new DocumentStorageBuilder(_mapping, StorageStyle.QueryOnly, x => x.QueryOnlySelector) .Build(assembly, operations); var lightweight = new DocumentStorageBuilder(_mapping, StorageStyle.Lightweight, x => x.LightweightSelector) .Build(assembly, operations); var identityMap = new DocumentStorageBuilder(_mapping, StorageStyle.IdentityMap, x => x.IdentityMapSelector) .Build(assembly, operations); var dirtyTracking = new DocumentStorageBuilder(_mapping, StorageStyle.DirtyTracking, x => x.DirtyCheckingSelector) .Build(assembly, operations); var bulkWriterType = new BulkLoaderBuilder(_mapping).BuildType(assembly); var compiler = new AssemblyGenerator(); compiler.ReferenceAssembly(typeof(IDocumentStorage <>).Assembly); compiler.ReferenceAssembly(typeof(T).Assembly); compiler.Compile(assembly); var slot = new DocumentPersistence <T> { QueryOnly = (IDocumentStorage <T>)Activator.CreateInstance(queryOnly.CompiledType, _mapping), Lightweight = (IDocumentStorage <T>)Activator.CreateInstance(lightweight.CompiledType, _mapping), IdentityMap = (IDocumentStorage <T>)Activator.CreateInstance(identityMap.CompiledType, _mapping), DirtyTracking = (IDocumentStorage <T>)Activator.CreateInstance(dirtyTracking.CompiledType, _mapping), BulkLoader = _mapping.IsHierarchy() ? (IBulkLoader <T>)Activator.CreateInstance(bulkWriterType.CompiledType, _mapping) : (IBulkLoader <T>)Activator.CreateInstance(bulkWriterType.CompiledType) }; return(slot); }
public GeneratedType Build(GeneratedAssembly assembly, DocumentOperations operations) { var selectorType = _selectorTypeSource(operations); return(buildDocumentStorageType(assembly, operations, _typeName, _baseType, selectorType)); }