コード例 #1
0
        private static void buildUpdateStreamVersion(GeneratedType builderType, GeneratedAssembly assembly, EventGraph graph)
        {
            var operationType = assembly.AddType(UpdateStreamVersionOperationName, typeof(UpdateStreamVersion));

            var sql = $"update {graph.DatabaseSchemaName}.mt_streams set version = ? where id = ? and version = ?";

            if (graph.TenancyStyle == TenancyStyle.Conjoined)
            {
                sql += $" and {TenantIdColumn.Name} = ?";
            }

            var configureCommand = operationType.MethodFor("ConfigureCommand");

            configureCommand.DerivedVariables.Add(new Variable(typeof(StreamAction), nameof(UpdateStreamVersion.Stream)));

            configureCommand.Frames.Code($"var parameters = {{0}}.{nameof(CommandBuilder.AppendWithParameters)}(\"{sql}\");",
                                         Use.Type <CommandBuilder>());

            configureCommand.SetParameterFromMember <StreamAction>(0, x => x.Version);

            if (graph.StreamIdentity == StreamIdentity.AsGuid)
            {
                configureCommand.SetParameterFromMember <StreamAction>(1, x => x.Id);
            }
            else
            {
                configureCommand.SetParameterFromMember <StreamAction>(1, x => x.Key);
            }

            configureCommand.SetParameterFromMember <StreamAction>(2, x => x.ExpectedVersionOnServer);

            if (graph.TenancyStyle == TenancyStyle.Conjoined)
            {
                new TenantIdColumn().As <IStreamTableColumn>().GenerateAppendCode(configureCommand, 3);
            }

            builderType.MethodFor(nameof(EventDocumentStorage.UpdateStreamVersion))
            .Frames.Code($"return new Marten.Generated.{UpdateStreamVersionOperationName}({{0}});",
                         Use.Type <StreamAction>());
        }
コード例 #2
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;");
            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());
        }
コード例 #3
0
        private void configureCommandMethod(GeneratedType compiledType, HardCodedParameters hardcoded)
        {
            var method = compiledType.MethodFor(nameof(IQueryHandler.ConfigureCommand));

            method.Frames.Code($"var parameters = {{0}}.{nameof(CommandBuilder.AppendWithParameters)}(@{{1}});",
                               Use.Type <CommandBuilder>(), _plan.CorrectedCommandText());

            foreach (var parameter in _plan.Parameters)
            {
                parameter.GenerateCode(method, _storeOptions);
            }

            if (hardcoded.HasTenantId)
            {
                method.Frames.Code($"{{0}}.{nameof(CommandBuilder.AddNamedParameter)}({{1}}, session.Tenant.TenantId);",
                                   Use.Type <CommandBuilder>(), TenantIdArgument.ArgName);
            }

            if (hardcoded.HasAny())
            {
                method.Frames.Code($"_hardcoded.{nameof(HardCodedParameters.Apply)}(parameters);");
            }
        }
コード例 #4
0
ファイル: EventProjection.cs プロジェクト: cocowalla/marten
        protected override void assembleTypes(GeneratedAssembly assembly, StoreOptions options)
        {
            assembly.Rules.Assemblies.Add(GetType().Assembly);
            assembly.Rules.Assemblies.AddRange(_projectMethods.ReferencedAssemblies());
            assembly.Rules.Assemblies.AddRange(_createMethods.ReferencedAssemblies());

            assembly.UsingNamespaces.Add("System.Linq");

            _isAsync = _createMethods.IsAsync || _projectMethods.IsAsync;

            var baseType = _isAsync ? typeof(AsyncEventProjection <>) : typeof(SyncEventProjection <>);

            baseType    = baseType.MakeGenericType(GetType());
            _inlineType = assembly.AddType(_inlineTypeName, 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);
        }
コード例 #5
0
        private void buildHandlerMethod(GeneratedType compiledType)
        {
            var method = compiledType.MethodFor("BuildHandler");

            var handlerName = "_inner";

            // first build out the inner
            if (_plan.HandlerPrototype is IMaybeStatefulHandler h && h.DependsOnDocumentSelector())
            {
                handlerName = "cloned";

                var statistics = _plan.StatisticsMember == null ? "null" : $"query.{_plan.StatisticsMember.Name}";

                method.Frames.Code(
                    $"var cloned = _inner.{nameof(IMaybeStatefulHandler.CloneForSession)}(session, {statistics});");
            }

            if (_plan.IncludeMembers.Any())
            {
                var readers = _plan.IncludeMembers.Select(buildIncludeReader);

                var includeHandlerType = typeof(IncludeQueryHandler <>).MakeGenericType(_plan.OutputType);

                var readerArray = "{{" + readers.Join(", ") + "}}";

                var constructorHandlerType = typeof(IQueryHandler <>).MakeGenericType(_plan.OutputType);

                method.Frames.Code(
                    $"var includeWriters = new {typeof(IIncludeReader).FullNameInCode()}[]{readerArray};");
                method.Frames.Code(
                    $"var included = new {includeHandlerType.FullNameInCode()}(({constructorHandlerType.FullNameInCode()}){handlerName}, includeWriters);");

                handlerName = "included";
            }

            method.Frames.Code($"return {handlerName};");
        }
コード例 #6
0
 public dependency_inlining()
 {
     theAssembly = new GeneratedAssembly(new GenerationRules("Lamar.Generated"));
     theType     = theAssembly.AddType("GeneratedClass", typeof(Message1Handler));
     theMethod   = theType.MethodFor("Handle");
 }
        public void generate_method_for_Task_of_value_method()
        {
            var generatedType = new GeneratedType("Foo").Implements <IHasTaskMethods>();

            generatedType.MethodFor("AddNumbers").ReturnType.ShouldBe(typeof(Task <int>));
        }
        public void generate_method_for_return_type_of_Task()
        {
            var generatedType = new GeneratedType("Foo").Implements <IHasTaskMethods>();

            generatedType.MethodFor("DoStuff").ReturnType.ShouldBe(typeof(Task));
        }
        public void generate_method_for_single_return_value()
        {
            var generatedType = new GeneratedType("Foo").Implements <IHasMethods>();

            generatedType.MethodFor("AddNumbers").ReturnType.ShouldBe(typeof(int));
        }
        public void generate_method_for_void_signature()
        {
            var generatedType = new GeneratedType("Foo").Implements <IHasMethods>();

            generatedType.MethodFor("DoStuff").ReturnType.ShouldBe(typeof(void));
        }