Exemple #1
0
        public void TestReverseMappingDoNothing()
        {
            var sut = new ComponentCollectionAssociation <Foo, Baz, Bar, Qux>(_ => _.Bazes, _ => _.Quxes);

            var foo = new Foo();

            var bar = new Bar
            {
                Quxes = new[] { new Qux(), new Qux() }
            };

            CreateWriter(sut)(bar, foo);

            foo.Bazes.Should().BeNull();
        }
        public void TestReverseMappingDoNothing()
        {
            var sut = CreateSut();

            var foo = new Foo();

            var bar = new Bar
            {
                Names = new[] { "a", "b" }
            };

            CreateWriter(sut)(bar, foo);

            foo.Bazes.Should().BeNull();
        }
Exemple #3
0
        public void TestStringToIntDefaultMapping()
        {
            Foo foo;
            Bar bar;

            var sut = new PropertyToPropertyWithConversionAssociation <Foo, int, Bar, string>(_ => _.Id, _ => _.ToString(), _ => _.Name, _ => int.Parse(_));

            foo = new Foo {
                Id = 123
            };
            bar = new Bar {
                Name = string.Empty
            };
            CreateWriter(sut)(bar, foo);
            foo.Id.Should().Be(default(int));
        }
        public void TestMappingAssignsDefaultValue()
        {
            var sut       = new ComponentToComponentAssociation <Foo, Baz, Bar, Qux>(_ => _.Component, _ => _.Component);
            var component = new PropertyToPropertyAssociation <Baz, Qux, int>(_ => _.Id, _ => _.Id);

            var foo = new Foo();
            var bar = CreateReader(sut, component)(foo);

            bar.Should().NotBeNull();
            bar.Component.Should().BeNull();

            foo = new Foo {
                Component = new Baz()
            };
            bar = new Bar();
            CreateWriter(sut, component)(bar, foo);
            foo.Component.Should().BeNull();
        }
        public void TestInverseMappingAssignsOnWrite()
        {
            var foo = new Foo {
                Component = new Baz()
            };

            var bar = new Bar {
                Id = 11
            };

            Expression <Func <Foo, int> > source = _ => _.Component.Id;

            var sut = new ExpressionToPropertyAssociation <Foo, Bar, int>(source, _ => _.Id);

            sut.Write();

            CreateWriter(sut)(bar, foo);

            foo.Component.Id.Should().Be(11);
        }
        public void TestInverseMappingAppliesMapper()
        {
            var foo = new Foo {
                Component = new Baz {
                    Id = 15
                }
            };
            var bar = new Bar {
                Component = new Qux {
                    Id = 16
                }
            };

            var sut       = new ComponentToComponentAssociation <Foo, Baz, Bar, Qux>(_ => _.Component, _ => _.Component);
            var component = new PropertyToPropertyAssociation <Baz, Qux, int>(_ => _.Id, _ => _.Id);

            CreateWriter(sut, component)(bar, foo);

            foo.Component.Id.Should().Be(bar.Component.Id);
        }
        public void TestInverseMappingLeavesExisitingTarget()
        {
            var foo = new Foo {
                Component = new Baz {
                    Id = 15
                }
            };
            var bar = new Bar {
                Component = new Qux {
                    Id = 16
                }
            };

            var before = foo.Component;

            var sut = new ComponentToComponentAssociation <Foo, Baz, Bar, Qux>(_ => _.Component, _ => _.Component);

            CreateWriter(sut)(bar, foo);

            bar.Component.Should().NotBeNull();
            foo.Component.Should().BeSameAs(before);
        }
Exemple #8
0
        public void TestIntToStringMapping()
        {
            Foo foo;
            Bar bar;

            var sut = new PropertyToPropertyWithConversionAssociation <Foo, int, Bar, string>(_ => _.Id, _ => _.ToString(), _ => _.Name, _ => int.Parse(_));

            foo = new Foo {
                Id = 123
            };
            bar = CreateReader(sut)(foo);
            bar.Should().NotBeNull();
            bar.Name.Should().Be(foo.Id.ToString());

            foo = new Foo {
                Id = 123
            };
            bar = new Bar {
                Name = "555"
            };
            CreateWriter(sut)(bar, foo);
            foo.Id.Should().Be(int.Parse(bar.Name));
        }
Exemple #9
0
        public void TestStringMapping()
        {
            Foo foo;
            Bar bar;

            var sut = new PropertyToPropertyAssociation <Foo, Bar, string>(_ => _.Name, _ => _.Name);

            foo = new Foo {
                Name = "aaa"
            };
            bar = CreateReader(sut)(foo);
            bar.Should().NotBeNull();
            bar.Id.Should().Be(foo.Id);

            foo = new Foo {
                Name = "aaa"
            };
            bar = new Bar {
                Name = "bbb"
            };
            CreateWriter(sut)(bar, foo);
            foo.Id.Should().Be(bar.Id);
        }
Exemple #10
0
        public void TestIntMapping()
        {
            Foo foo;
            Bar bar;

            var sut = new PropertyToPropertyAssociation <Foo, Bar, int>(_ => _.Id, _ => _.Id);

            foo = new Foo {
                Id = 5
            };
            bar = CreateReader(sut)(foo);
            bar.Should().NotBeNull();
            bar.Id.Should().Be(foo.Id);

            foo = new Foo {
                Id = 5
            };
            bar = new Bar {
                Id = 6
            };
            CreateWriter(sut)(bar, foo);
            foo.Id.Should().Be(bar.Id);
        }
Exemple #11
0
 public TeamCityFlowWriter([NotNull] IFlowServiceMessageProcessor target, [NotNull] CreateWriter closeBlock, [NotNull] IDisposable disposableHander)
     : base(target, disposableHander)
 {
     if (target == null)
     {
         throw new ArgumentNullException(nameof(target));
     }
     if (closeBlock == null)
     {
         throw new ArgumentNullException(nameof(closeBlock));
     }
     if (disposableHander == null)
     {
         throw new ArgumentNullException(nameof(disposableHander));
     }
     _closeBlock = closeBlock;
 }
Exemple #12
0
    public static void Write(OutputModel outputModel, DatabasePlan databasePlan, PartitionPlan partitionPlan)
    {
        var s = $@"
namespace {databasePlan.Namespace};

public class {partitionPlan.ClassName} : Cosmogenesis.Core.DbPartitionBase
{{
    protected virtual {databasePlan.Namespace}.{databasePlan.DbClassName} {databasePlan.DbClassName} {{ get; }} = default!;

    {PkClass(partitionPlan)}

    /// <summary>Mocking constructor</summary>
    protected {partitionPlan.ClassName}() {{ }}

    internal protected {partitionPlan.ClassName}(
        {new[] { ConstructorClassParameter(databasePlan), ConstructorKeyParameter(partitionPlan) }.Where(x => !string.IsNullOrEmpty(x)).JoinNonEmpty()})
        : base(
            db: {databasePlan.DbClassNameArgument},
            partitionKey: {partitionPlan.GetPkPlan.FullMethodName}({partitionPlan.GetPkPlan.DocumentToParametersMapping("pkData")}),
            serializer: {databasePlan.Namespace}.{databasePlan.SerializerClassName}.Instance)
    {{
        this.{databasePlan.DbClassName} = {databasePlan.DbClassNameArgument} ?? throw new System.ArgumentNullException(nameof({databasePlan.DbClassNameArgument}));
        {PkClassSetter(partitionPlan)}
    }}

    {databasePlan.Namespace}.{partitionPlan.QueryBuilderClassName}? queryBuilder;
    /// <summary>
    /// Methods to build queries for later execution.
    /// </summary>
    public virtual {databasePlan.Namespace}.{partitionPlan.QueryBuilderClassName} QueryBuilder => this.queryBuilder ??= new(
        {databasePlan.DbClassNameArgument}: this.{databasePlan.DbClassName},
        partitionKey: this.PartitionKey);

    {databasePlan.Namespace}.{partitionPlan.QueryClassName}? query;
    /// <summary>
    /// Methods to execute queries.
    /// </summary>
    public virtual {databasePlan.Namespace}.{partitionPlan.QueryClassName} Query => this.query ??= new(
        {databasePlan.DbClassNameArgument}: this.{databasePlan.DbClassName},
        {partitionPlan.QueryBuilderClassNameArgument}: this.QueryBuilder);

    /// <summary>
    /// A batch of operations to be executed atomically (or not at all) within a {partitionPlan.Name} in the {databasePlan.Name} database.
    /// </summary>
    public virtual {databasePlan.Namespace}.{partitionPlan.BatchClassName} CreateBatch() => new(
        transactionalBatch: this.CreateBatchForPartition(),
        partitionKey: this.PartitionKeyString,
        validateStateBeforeSave: this.{databasePlan.DbClassName}.ValidateStateBeforeSave,
        {partitionPlan.ClassNameArgument}: this);

    {databasePlan.Namespace}.{partitionPlan.ReadClassName}? read;
    /// <summary>
    /// Methods to read documents.
    /// </summary>
    public virtual {databasePlan.Namespace}.{partitionPlan.ReadClassName} Read => this.read ??= new(
        {databasePlan.DbClassNameArgument}: this.{databasePlan.DbClassName}, 
        partitionKey: this.PartitionKey);

    {databasePlan.Namespace}.{partitionPlan.ReadOrThrowClassName}? readOrThrow;
    /// <summary>
    /// Methods to read documents, or throw DbConflictException is they are not found.
    /// </summary>
    public virtual {databasePlan.Namespace}.{partitionPlan.ReadOrThrowClassName} ReadOrThrow => this.readOrThrow ??= new(
        {databasePlan.DbClassNameArgument}: this.{databasePlan.DbClassName}, 
        partitionKey: this.PartitionKey);

    {databasePlan.Namespace}.{partitionPlan.CreateClassName}? create;
    /// <summary>
    /// Methods to create documents.
    /// </summary>
    public virtual {databasePlan.Namespace}.{partitionPlan.CreateClassName} Create => this.create ??= new(this);

    {databasePlan.Namespace}.{partitionPlan.ReadOrCreateClassName}? readOrCreate;
    /// <summary>
    /// Methods to read documents, or create them if they did not yet exist.
    /// </summary>
    public virtual {databasePlan.Namespace}.{partitionPlan.ReadOrCreateClassName} ReadOrCreate => this.readOrCreate ??= new(this);

{ReadMany(databasePlan, partitionPlan)}
{CreateOrReplace(databasePlan, partitionPlan)}
{string.Concat(partitionPlan.Documents.Select(x => Create(partitionPlan, x)))}
{string.Concat(partitionPlan.Documents.Select(x => CreateOrReplace(partitionPlan, x)))}
{string.Concat(partitionPlan.Documents.Select(x => ReadOrCreate(partitionPlan, x)))}
{string.Concat(partitionPlan.Documents.Select(ReplaceIfMutable))}
{string.Concat(partitionPlan.Documents.Select(DeleteIfTransient))}
}}
";

        outputModel.Context.AddSource($"partition_{partitionPlan.ClassName}.cs", s);

        BatchWriter.Write(outputModel, databasePlan, partitionPlan);
        CreateWriter.Write(outputModel, databasePlan, partitionPlan);
        ReadOrCreateWriter.Write(outputModel, databasePlan, partitionPlan);
        CreateOrReplaceWriter.Write(outputModel, databasePlan, partitionPlan);
        ReadWriter.Write(outputModel, databasePlan, partitionPlan);
        ReadOrThrowWriter.Write(outputModel, databasePlan, partitionPlan);
        ReadManyWriter.Write(outputModel, databasePlan, partitionPlan);
        QueryBuilderWriter.Write(outputModel, databasePlan, partitionPlan);
        QueryWriter.Write(outputModel, databasePlan, partitionPlan);
    }
 public TeamCityFlowWriter([NotNull] IFlowServiceMessageProcessor target, [NotNull] CreateWriter closeBlock, [NotNull] IDisposable disposableHander)
     : base(target, disposableHander)
 {
     myCloseBlock = closeBlock;
 }