예제 #1
0
        private void buildLiveAggregationType()
        {
            var liveBaseType = _isAsync
                ? typeof(AsyncLiveAggregatorBase <>)
                : typeof(SyncLiveAggregatorBase <>);

            liveBaseType = liveBaseType.MakeGenericType(typeof(T));


            _liveType =
                _assembly.AddType(GetType().NameInCode().Sanitize() + "LiveAggregation", liveBaseType);

            var overrideMethodName = _isAsync ? "BuildAsync" : "Build";
            var buildMethod        = _liveType.MethodFor(overrideMethodName);

            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));

            _liveType.AllInjectedFields.Add(new InjectedField(GetType()));

            _createMethods.BuildCreateMethod(_liveType, _aggregateMapping);
            _applyMethods.BuildApplyMethod(_liveType, _aggregateMapping);

            _liveType.Setters.AddRange(_applyMethods.Setters());
            _liveType.Setters.AddRange(_createMethods.Setters());
            _liveType.Setters.AddRange(_shouldDeleteMethods.Setters());
        }
예제 #2
0
        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));
        }
        private void buildLiveAggregationType()
        {
            var liveBaseType = _isAsync
                ? typeof(AsyncLiveAggregatorBase <>)
                : typeof(SyncLiveAggregatorBase <>);

            liveBaseType = liveBaseType.MakeGenericType(typeof(T));


            _liveType =
                _assembly.AddType(GetType().NameInCode().Sanitize() + "LiveAggregation", liveBaseType);

            var overrideMethodName = _isAsync ? "BuildAsync" : "Build";
            var buildMethod        = _liveType.MethodFor(overrideMethodName);

            buildMethod.DerivedVariables.Add(Variable.For <IQuerySession>("(IQuerySession)session"));

            buildMethod.Frames.Code("if (!events.Any()) return null;");

            buildMethod.Frames.Add(new DeclareAggregateFrame(typeof(T)));


            var callCreateAggregateFrame = new CallCreateAggregateFrame(_createMethods);

            // This is the existing snapshot passed into the LiveAggregator
            var snapshot = buildMethod.Arguments.Single(x => x.VariableType == typeof(T));

            callCreateAggregateFrame.CoalesceAssignTo(snapshot);

            buildMethod.Frames.Add(callCreateAggregateFrame);
            buildMethod.Frames.Add(new CallApplyAggregateFrame(_applyMethods)
            {
                InsideForEach = true
            });

            buildMethod.Frames.Return(typeof(T));

            _liveType.AllInjectedFields.Add(new InjectedField(GetType()));

            _createMethods.BuildCreateMethod(_liveType, _aggregateMapping);
            _applyMethods.BuildApplyMethod(_liveType, _aggregateMapping);

            _liveType.Setters.AddRange(_applyMethods.Setters());
            _liveType.Setters.AddRange(_createMethods.Setters());
            _liveType.Setters.AddRange(_shouldDeleteMethods.Setters());
        }