public void AssembleTypes(GeneratedAssembly assembly, StoreOptions options) { assembly.Generation.Assemblies.Add(GetType().Assembly); assembly.Generation.Assemblies.Add(typeof(T).Assembly); assembly.Generation.Assemblies.AddRange(_applyMethods.ReferencedAssemblies()); assembly.Generation.Assemblies.AddRange(_createMethods.ReferencedAssemblies()); assembly.Generation.Assemblies.AddRange(_shouldDeleteMethods.ReferencedAssemblies()); assembly.Namespaces.Add("System"); assembly.Namespaces.Add("System.Linq"); _isAsync = _createMethods.IsAsync || _applyMethods.IsAsync; _aggregateMapping = options.Storage.FindMapping(typeof(T)); if (_aggregateMapping.IdMember == null) { throw new InvalidDocumentException( $"No identity property or field can be determined for the aggregate '{typeof(T).FullNameInCode()}', but one is required to be used as an aggregate in projections"); } buildLiveAggregationType(assembly); buildInlineAggregationType(assembly); }
internal void Compile() { _assembly = new GeneratedAssembly(new GenerationRules("Marten.Generated")); _assembly.Generation.Assemblies.Add(GetType().Assembly); _assembly.Generation.Assemblies.AddRange(_projectMethods.ReferencedAssemblies()); _assembly.Generation.Assemblies.AddRange(_createMethods.ReferencedAssemblies()); _assembly.Namespaces.Add("System.Linq"); _isAsync = _createMethods.IsAsync || _projectMethods.IsAsync; var baseType = _isAsync ? typeof(AsyncEventProjection <>) : typeof(SyncEventProjection <>); baseType = baseType.MakeGenericType(GetType()); _inlineType = _assembly.AddType(GetType().Name.Sanitize() + "GeneratedInlineProjection", baseType); var method = _inlineType.MethodFor("ApplyEvent"); method.DerivedVariables.Add(new Variable(GetType(), "Projection")); var eventHandling = MethodCollection.AddEventHandling(null, null, _createMethods, _projectMethods); method.Frames.Add(eventHandling); var assemblyGenerator = new AssemblyGenerator(); assemblyGenerator.ReferenceAssembly(typeof(IMartenSession).Assembly); assemblyGenerator.Compile(_assembly); }
internal GeneratedAssembly Compile(DocumentStore store) { _assembly = new GeneratedAssembly(new GenerationRules("Marten.Generated")); _assembly.Generation.Assemblies.Add(GetType().Assembly); _assembly.Generation.Assemblies.AddRange(_applyMethods.ReferencedAssemblies()); _assembly.Generation.Assemblies.AddRange(_createMethods.ReferencedAssemblies()); _assembly.Generation.Assemblies.AddRange(_shouldDeleteMethods.ReferencedAssemblies()); _assembly.Namespaces.Add("System.Linq"); _isAsync = _createMethods.IsAsync || _applyMethods.IsAsync; _aggregateMapping = store.Storage.MappingFor(typeof(T)); _storageType = typeof(IDocumentStorage <,>).MakeGenericType(typeof(T), _aggregateMapping.IdType); // TODO -- Validate the id strategy for the mapping // against the aggregation setup buildLiveAggregationType(); buildInlineAggregationType(); buildAsyncDaemonAggregation(); var assemblyGenerator = new AssemblyGenerator(); assemblyGenerator.ReferenceAssembly(typeof(IMartenSession).Assembly); assemblyGenerator.Compile(_assembly); return(_assembly); }
internal static ILiveAggregator <T> Build <T>(StoreOptions options) { var mapping = options.Storage.MappingFor(typeof(T)); var assembly = new GeneratedAssembly(new GenerationRules("Marten.Generated")); var applyMethods = new ApplyMethodCollection(typeof(T), typeof(T)); // TODO -- this doesn't do very much right now. var createMethods = new CreateMethodCollection(typeof(T), typeof(T)); assembly.Generation.Assemblies.Add(typeof(IMartenSession).Assembly); assembly.Generation.Assemblies.AddRange(applyMethods.ReferencedAssemblies()); assembly.Generation.Assemblies.AddRange(createMethods.ReferencedAssemblies()); var isAsync = applyMethods.IsAsync; assembly.Namespaces.Add("System.Linq"); var liveBaseType = isAsync ? typeof(AsyncLiveAggregatorBase <>) : typeof(SyncLiveAggregatorBase <>); liveBaseType = liveBaseType.MakeGenericType(typeof(T)); var liveType = assembly.AddType(typeof(T).NameInCode().Sanitize() + "LiveAggregation", liveBaseType); var overrideMethodName = isAsync ? "BuildAsync" : "Build"; var buildMethod = liveType.MethodFor(overrideMethodName); // TODO -- use constructor functions later // TODO -- use static Create() methods // TODO -- validate that there is some way to create the aggregate buildMethod.Frames.Code("if (!events.Any()) return null;"); buildMethod.Frames.Add(new CallCreateAggregateFrame(createMethods)); buildMethod.Frames.Add(new CallApplyAggregateFrame(applyMethods) { InsideForEach = true }); buildMethod.Frames.Return(typeof(T)); createMethods.BuildCreateMethod(liveType, mapping); applyMethods.BuildApplyMethod(liveType, mapping); var assemblyGenerator = new AssemblyGenerator(); assemblyGenerator.ReferenceAssembly(typeof(IMartenSession).Assembly); assemblyGenerator.Compile(assembly); return((ILiveAggregator <T>)Activator.CreateInstance(liveType.CompiledType)); }
internal GeneratedAssembly Compile(StoreOptions options) { _assembly = new GeneratedAssembly(new GenerationRules("Marten.Generated")); _assembly.Generation.Assemblies.Add(GetType().Assembly); _assembly.Generation.Assemblies.Add(typeof(T).Assembly); _assembly.Generation.Assemblies.AddRange(_applyMethods.ReferencedAssemblies()); _assembly.Generation.Assemblies.AddRange(_createMethods.ReferencedAssemblies()); _assembly.Generation.Assemblies.AddRange(_shouldDeleteMethods.ReferencedAssemblies()); _assembly.Namespaces.Add("System"); _assembly.Namespaces.Add("System.Linq"); _isAsync = _createMethods.IsAsync || _applyMethods.IsAsync; _aggregateMapping = options.Storage.MappingFor(typeof(T)); if (_aggregateMapping.IdMember == null) { // TODO -- possibly try to relax this!!! throw new InvalidDocumentException( $"No identity property or field can be determined for the aggregate '{typeof(T).FullNameInCode()}', but one is required to be used as an aggregate in projections"); } _storageType = typeof(IDocumentStorage <,>).MakeGenericType(typeof(T), _aggregateMapping.IdType); // TODO -- Validate the id strategy for the mapping // against the aggregation setup buildLiveAggregationType(); buildInlineAggregationType(); var assemblyGenerator = new AssemblyGenerator(); assemblyGenerator.ReferenceAssembly(typeof(IMartenSession).Assembly); assemblyGenerator.Compile(_assembly); Debug.WriteLine(_liveType.SourceCode); return(_assembly); }