Пример #1
0
        public void ComposingPatternsAppliersPacks()
        {
            // Thanks to Lorenzo Vegetti to provide the domain of this example.
            // This example refers to a little-domain, interesting from the point of view of mapping with ConfORM.
            // Here you can see how apply general convetion, how and when compose patterns-appliers-packs and patterns-appliers,
            // how ConfORM can apply different mapping depending on the type of enum (see the difference on CostOptions) and so on...
            // You don't have to organize the mapping all in one class, as in this example, and you don't have to use the IModuleMapping...
            // You can organize the mapping as you feel more confortable for your application; in some case a class is enough, in other cases would
            // be better the separation per module/concern, you are not closed in "mapping per class" box.

            var orm = new ObjectRelationalMapper();

            // With the follow line I'm adding the pattern for the POID strategy ("native" instead the default "High-Low")
            orm.Patterns.PoidStrategies.Add(new NativePoidPattern());

            // composition of patterns-appliers-packs and patterns-appliers for Lorenzo's domain
            // Note: for bidirectional-one-to-many association Lorenzo is not interested in the default cascade behavior,
            // implemented in the BidirectionalOneToManyRelationPack, because he is using a sort of soft-delete in his base class VersionModelBase.
            // He can implements a custom pack of patterns-appliers to manage the situation when classes does not inherits from VersionModelBase by the way
            // he don't want the cascade atall, so the BidirectionalOneToManyInverseApplier will be enough
            IPatternsAppliersHolder patternsAppliers =
                (new SafePropertyAccessorPack())
                    .Merge(new OneToOneRelationPack(orm))
                    .Merge(new BidirectionalManyToManyRelationPack(orm))
                    .Merge(new DiscriminatorValueAsClassNamePack(orm))
                    .Merge(new CoolColumnsNamingPack(orm))
                    .Merge(new TablePerClassPack())
                    .Merge(new PluralizedTablesPack(orm, new EnglishInflector()))
                    .Merge(new ListIndexAsPropertyPosColumnNameApplier())
                    .Merge(new BidirectionalOneToManyInverseApplier(orm))
                    .Merge(new EnumAsStringPack())
                    .Merge(new DatePropertyByNameApplier())
                    .Merge(new MsSQL2008DateTimeApplier());

            // Instancing the Mapper using the result of Merge
            var mapper = new Mapper(orm, patternsAppliers);

            // Setting the version property using the base class
            orm.VersionProperty<VersionModelBase>(v => v.Version);

            // Note: I'm declaring the strategy only for the base entity
            orm.TablePerClassHierarchy<Cost>();
            orm.TablePerClass<EditionQuotation>();
            orm.TablePerClass<ProductQuotation>();
            orm.TablePerClass<Quotation>();

            AppliesGeneralConventions(mapper);

            // EditionQuotation.PrintCost don't use lazy-loading
            mapper.Customize<EditionQuotation>(map => map.ManyToOne(eq => eq.PrintCost, m2o => m2o.Lazy(LazyRelation.NoLazy)));

            // Customizes some others DDL's stuff outside conventions
            CustomizeColumns(mapper);

            // Note : I have to create mappings for the whole domain
            HbmMapping mapping =
                mapper.CompileMappingFor(
                    typeof (GenericModelBase<>).Assembly.GetTypes().Where(t => t.Namespace == typeof (GenericModelBase<>).Namespace));
            Console.Write(mapping.AsString());
        }
Пример #2
0
        public HbmMapping CreateMapping()
        {
            var orm = new ObjectRelationalMapper();

            //主键生成策略(自增)
            orm.Patterns.PoidStrategies.Add(new NativePoidPattern());
            var entities = new[]
            {
                typeof(User),
                typeof(PlusMaterial),
                typeof(StockIn),
                typeof(StockInDetail),
                typeof(StockOut),
                typeof(StockOutDetail),
            };
            orm.TablePerClass(entities);

            //关系映射
            orm.ManyToOne<StockInDetail, StockIn>();
            orm.ManyToOne<StockOutDetail, StockOut>();

            //数据库命名规则
            var mapper = new Mapper(orm, new CoolPatternsAppliersHolder(orm));
            orm.TablePerClass(entities);
            var hc = mapper.CompileMappingFor(Assembly.Load("PMMS.Entities").GetTypes());
            return hc;
        }
Пример #3
0
        public void DefiningAndCustomizingVersionThroughBaseImplementation()
        {
            // In this example I'll show how you can work with Version and how ConfORM understands OOP

            var orm = new ObjectRelationalMapper();
            var mapper = new Mapper(orm, new CoolPatternsAppliersHolder(orm));

            // In this case I will use the definition of table-to-class strategy class by class
            orm.TablePerClass<CurrencyDefinition>();
            orm.TablePerClass<Company>();
            orm.TablePerClass<Customer>();
            orm.TablePerClass<Provider>();

            // Defining relations
            orm.OneToOne<Company, Customer>();
            orm.OneToOne<Company, Provider>();

            // In the follow line I'm defining which is the property used as Version for all classes inherited from VersionedEntity
            orm.VersionProperty<VersionedEntity>(ve=> ve.Version);

            // In the follow line I'm customizing the column-name for the property used as Version for all classes inherited from VersionedEntity....
            // Note : VersionedEntity is not an entity, it is only a base class.
            mapper.Class<VersionedEntity>(cm => cm.Version(ve => ve.Version, vm => vm.Column("Revision")));

            // In the follow line I'm customizing the column-name for the property used as Version only for the class Provider
            // Note : You can move the follow line before the previous and the result does not change, this is because ConfORM can understand
            // which is a base customization and which is the specific customization.
            mapper.Class<Provider>(cm => cm.Version(ve => ve.Version, vm => vm.Column("IncrementalVersion")));

            // Note : I have to create mappings for the whole domain; Entity and VersionedEntity are excluded from de mapping because out-side
            // root-entities hierarchy (root-entities are : CurrencyDefinition, Company, Customer, Provider)
            var mapping = mapper.CompileMappingFor(typeof(Entity).Assembly.GetTypes().Where(t => t.Namespace == typeof(Entity).Namespace));
            Console.Write(mapping.AsString());
        }
 public void WhenNotRegisteredAsManyToOneRecognizeRelation()
 {
     var mapper = new ObjectRelationalMapper();
     mapper.TablePerClass<AEntity>();
     mapper.TablePerClass<BEntity>();
     mapper.IsOneToMany(typeof(BEntity), typeof(AEntity)).Should().Be.True();
     mapper.IsManyToOne(typeof(AEntity), typeof(BEntity)).Should().Be.True();
 }
 public void WhenExplicitRegisteredRecognizeInverseRelation()
 {
     var mapper = new ObjectRelationalMapper();
     mapper.TablePerClass<AEntity>();
     mapper.TablePerClass<BEntity>();
     mapper.ManyToOne<AEntity, BEntity>();
     mapper.IsOneToMany(typeof(BEntity), typeof(AEntity)).Should().Be.True();
 }
 public void WhenExplicitRegisteredRecognizeInverseRelationAsImplicit()
 {
     var mapper = new ObjectRelationalMapper();
     mapper.TablePerClass<User>();
     mapper.TablePerClass<Person>();
     mapper.OneToOne<User, Person>();
     mapper.IsMasterOneToOne(typeof(Person), typeof(User)).Should().Be.False();
 }
 public void WhenExplicitRegisteredRecognizeRelation()
 {
     var mapper = new ObjectRelationalMapper();
     mapper.TablePerClass<User>();
     mapper.TablePerClass<Person>();
     mapper.OneToOne<User, Person>();
     mapper.IsOneToOne(typeof(User), typeof(Person)).Should().Be.True();
 }
        public void RecognizeOneToMany()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Parent>();
            orm.TablePerClass<Image>();

            orm.IsOneToMany(typeof (Child), typeof (Image)).Should().Be.True();
        }
        public void CanDiscoverManyToOneFromComponentToEntity()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<AEntity>();
            orm.TablePerClass<BEntity>();
            orm.Component<AComponent>();

            orm.IsManyToOne(typeof(AComponent), typeof(BEntity)).Should().Be.True();
        }
        public void WhenFindInterfaceForRootClassThenRecognizeRelation()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Contact>();
            orm.TablePerClass<UserGroup>();
            orm.ManyToOne<UserGroup, ISecurity>();

            orm.IsManyToOne(typeof (UserGroup), typeof (ISecurity)).Should().Be.True();
        }
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Parent>();
            orm.TablePerClass<Child>();
            orm.ManyToOne<Child, Parent>();
            HbmMapping mapping = GetMapping(orm);

            VerifyMapping(mapping);
        }
Пример #12
0
        public void CanDiscoverManyToOneFromComponentToEntity()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <AEntity>();
            orm.TablePerClass <BEntity>();
            orm.Component <AComponent>();

            orm.IsManyToOne(typeof(AComponent), typeof(BEntity)).Should().Be.True();
        }
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<AEntity>();
            orm.TablePerClass<BEntity>();
            orm.ManyToOne<AEntity, BEntity>();
            HbmMapping mapping = GetMapping(orm);

            VerifyAEntityMapping(mapping);
        }
        public void DomainDefinition(ObjectRelationalMapper orm)
        {
            orm.TablePerClass<Animal>();
            orm.TablePerClass<User>();
            orm.TablePerClass<StateProvince>();
            orm.TablePerClassHierarchy<Zoo>();

            orm.ManyToMany<Human, Human>();
            orm.OneToOne<User, Human>();
        }
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Person>();
            orm.TablePerClass<Animal>();
            orm.ManyToMany<Person, Animal>();
            HbmMapping mapping = GetMapping(orm);

            VerifyMapping(mapping);
        }
Пример #16
0
        public void WithoutExplicitCascade()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();
            orm.ManyToOne <Child, Parent>();
            HbmMapping mapping = GetMapping(orm);

            VerifyMapping(mapping);
        }
        public void WhenExplicitRegisteredAsOneToOneNotRecognizeRelation()
        {
            var mapper = new ObjectRelationalMapper();
            mapper.TablePerClass<AEntity>();
            mapper.TablePerClass<BEntity>();
            mapper.OneToOne<AEntity, BEntity>();
            mapper.IsOneToMany(typeof(BEntity), typeof(AEntity)).Should().Be.False();

            // the default behaviour map an unidirectional one-to-one as a many-to-one (for NHibernate)
            // mapper.IsManyToOne(typeof(AEntity), typeof(BEntity)).Should().Be.False();
        }
Пример #18
0
        public void WhenNullThenNoThrow()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();

            var pattern = new PolymorphismBidirectionalOneToManyMemberPattern(orm);

            pattern.Executing(p => p.Match(null)).NotThrows();
        }
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();
            orm.ManyToOne <Child, Parent>();
            HbmMapping mapping = GetMapping(orm);

            VerifyMapping(mapping);
        }
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<AEntity>();
            orm.TablePerClass<BEntity>();
            orm.ManyToOne<AEntity, BEntity>();
            orm.Cascade<AEntity, BEntity>(Cascade.Persist | Cascade.Remove);
            HbmMapping mapping = GetMapping(orm);

            VerifyMappingWithCascade(mapping);
        }
Пример #21
0
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <AEntity>();
            orm.TablePerClass <BEntity>();
            orm.ManyToOne <AEntity, BEntity>();
            HbmMapping mapping = GetMapping(orm);

            VerifyAEntityMapping(mapping);
        }
Пример #22
0
        public void WhenTheDomainDoesNotContainMoreImplsThenNoMatch()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <MyEntity>();
            orm.TablePerClass <MyRelatedRoot1>();

            var pattern = new PolymorphismBidirectionalAnyToManyPattern(orm);

            pattern.Match(ForClass <MyRelatedRoot1> .Property(x => x.Items)).Should().Be.False();
        }
Пример #23
0
        public void WhenInterfaceOnChildThenMatch()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();

            var pattern = new PolymorphismBidirectionalOneToManyMemberPattern(orm);

            pattern.Match(ForClass <Parent> .Property(x => x.Children)).Should().Be.True();
        }
Пример #24
0
        public void WhenManySideIsNotAnEntityThenNoMatch()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <MyRelatedRoot1>();
            orm.TablePerClass <MyRelatedRoot2>();

            var pattern = new PolymorphismBidirectionalAnyToManyPattern(orm);

            pattern.Match(ForClass <MyRelatedRoot1> .Property(x => x.Items)).Should().Be.False();
        }
Пример #25
0
        public void WhenNoGenericThenNoMatch()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();

            var pattern = new PolymorphismBidirectionalOneToManyMemberPattern(orm);

            pattern.Match(ForClass <Parent> .Property(x => x.Whatever)).Should().Be.False();
        }
Пример #26
0
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Person>();
            orm.TablePerClass <Animal>();
            orm.ManyToMany <Person, Animal>();
            HbmMapping mapping = GetMapping(orm);

            VerifyMapping(mapping);
        }
Пример #27
0
        public void WhenExplicitRegisteredAsOneToOneNotRecognizeRelation()
        {
            var mapper = new ObjectRelationalMapper();

            mapper.TablePerClass <AEntity>();
            mapper.TablePerClass <BEntity>();
            mapper.OneToOne <AEntity, BEntity>();
            mapper.IsOneToMany(typeof(BEntity), typeof(AEntity)).Should().Be.False();

            // the default behaviour map an unidirectional one-to-one as a many-to-one (for NHibernate)
            // mapper.IsManyToOne(typeof(AEntity), typeof(BEntity)).Should().Be.False();
        }
Пример #28
0
        public void WhenNoPolymorphicThenNoMatch()
        {
            // using the concrete implementation of IDomainInspector this test is more like an integration-test but it is exactly what I need.
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();

            var pattern = new PolymorphismBidirectionalOneToManyMemberPattern(orm);

            pattern.Match(ForClass <Parent> .Property(x => x.Children)).Should().Be.False();
        }
        public void WhenDefineRootEntityThenRegister()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<MyClass>();
            orm.TablePerClass<Related>();
            orm.NaturalId<MyClass>(x => x.Name, x => x.Related, x => x.MyComponent, x => x.Any);

            orm.IsMemberOfNaturalId(ForClass<MyClass>.Property(x => x.Name)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass<MyClass>.Property(x => x.Related)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass<MyClass>.Property(x => x.MyComponent)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass<MyClass>.Property(x => x.Any)).Should().Be.True();
        }
Пример #30
0
        private HbmMapping GetMapping()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <MyEntity>();
            orm.TablePerClass <MyRelatedRoot1>();
            orm.TablePerClass <MyRelatedRoot2>();

            var mapper = new Mapper(orm);

            return(mapper.CompileMappingFor(new[] { typeof(MyEntity), typeof(MyRelatedRoot1), typeof(MyRelatedRoot2) }));
        }
Пример #31
0
        public void WhenInterfaceOnChildAndPropertyExclusionOnInterfaceThenNoMatch()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();
            orm.ExcludeProperty <IChild>(c => c.Parent);

            var pattern = new PolymorphismBidirectionalOneToManyMemberPattern(orm);

            pattern.Match(ForClass <Parent> .Property(x => x.Children)).Should().Be.False();
        }
Пример #32
0
        public void WithExplicitCascadeToAll()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();
            orm.ManyToOne <Child, Parent>();
            orm.Cascade <Parent, Child>(CascadeOn.All | CascadeOn.DeleteOrphans);
            HbmMapping mapping = GetMapping(orm);

            VerifyMapping(mapping);
        }
        public void WhenDefineWithNullThenRegisterValid()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<MyClass>();
            orm.TablePerClass<Related>();
            orm.NaturalId<MyClass>(x => x.Name, null, x => x.MyComponent, null);

            orm.IsMemberOfNaturalId(ForClass<MyClass>.Property(x => x.Name)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass<MyClass>.Property(x => x.Related)).Should().Be.False();
            orm.IsMemberOfNaturalId(ForClass<MyClass>.Property(x => x.MyComponent)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass<MyClass>.Property(x => x.Any)).Should().Be.False();
        }
Пример #34
0
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <AEntity>();
            orm.TablePerClass <BEntity>();
            orm.ManyToOne <AEntity, BEntity>();
            orm.Cascade <AEntity, BEntity>(Cascade.Persist | Cascade.Remove);
            HbmMapping mapping = GetMapping(orm);

            VerifyMappingWithCascade(mapping);
        }
Пример #35
0
        public void WhenManyToManyShouldNotMatch()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Contact>();
            orm.TablePerClass <Company>();
            orm.ManyToMany <Contact, Company>();

            var        pattern    = new PolymorphismBidirectionalOneToManyMemberPattern(orm);
            MemberInfo memberInfo = ForClass <Contact> .Property(x => x.Companies);

            pattern.Match(memberInfo).Should().Be.False();
        }
Пример #36
0
        public void WhenDefineWithNullThenRegisterValid()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <MyClass>();
            orm.TablePerClass <Related>();
            orm.NaturalId <MyClass>(x => x.Name, null, x => x.MyComponent, null);

            orm.IsMemberOfNaturalId(ForClass <MyClass> .Property(x => x.Name)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass <MyClass> .Property(x => x.Related)).Should().Be.False();
            orm.IsMemberOfNaturalId(ForClass <MyClass> .Property(x => x.MyComponent)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass <MyClass> .Property(x => x.Any)).Should().Be.False();
        }
Пример #37
0
        public void WhenDefineRootEntityThenRegister()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <MyClass>();
            orm.TablePerClass <Related>();
            orm.NaturalId <MyClass>(x => x.Name, x => x.Related, x => x.MyComponent, x => x.Any);

            orm.IsMemberOfNaturalId(ForClass <MyClass> .Property(x => x.Name)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass <MyClass> .Property(x => x.Related)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass <MyClass> .Property(x => x.MyComponent)).Should().Be.True();
            orm.IsMemberOfNaturalId(ForClass <MyClass> .Property(x => x.Any)).Should().Be.True();
        }
Пример #38
0
        public void WhenExplicitlyDeclaredThenEachCollectionMapToItsCorrectParentPropertyIntegration()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Team>();
            orm.TablePerClass <Match>();
            orm.Bidirectional <Team, Match>(t => t.MatchesAtHome, m => m.HomeTeam);
            orm.Bidirectional <Match, Team>(m => m.RoadTeam, t => t.MatchesOnRoad);

            HbmMapping mapping = GetMapping(orm);

            VerifyBagsHasTheCorrectKey(mapping);
        }
Пример #39
0
        public void WhenAnyToManyThenNoMatch()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <MyEntity>();
            orm.TablePerClass <MyRelatedRoot1>();
            orm.TablePerClass <MyRelatedRoot2>();

            var applier = new OneToManyKeyColumnApplier(orm);

            applier.Match(new PropertyPath(null, ForClass <MyRelatedRoot1> .Property(x => x.Items))).Should().Be.False();
            applier.Match(new PropertyPath(null, ForClass <MyRelatedRoot2> .Property(x => x.Items))).Should().Be.False();
        }
        public void WhenOrmCascadeIsAllThenApplyOndeleteCascade()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Parent>();
            orm.TablePerClass<Child>();
            orm.ManyToOne<Child, Parent>();
            orm.Cascade<Parent, Child>(Cascade.All);
            HbmMapping mapping = GetMapping(orm);

            HbmClass rc = mapping.RootClasses.First(r => r.Name.Contains("Parent"));
            var relation = rc.Properties.First(p => p.Name == "Children");
            var collection = (HbmBag)relation;
            collection.Key.ondelete.Should().Be(HbmOndelete.Cascade);
        }
        public void WhenOrmCascadeDoesNotIncludeDeleteNorDeleteOrhphanThenNotApplyOndeleteCascade()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Parent>();
            orm.TablePerClass<Child>();
            orm.ManyToOne<Child, Parent>();
            orm.Cascade<Parent, Child>(Cascade.Persist | Cascade.ReAttach);
            HbmMapping mapping = GetMapping(orm);

            HbmClass rc = mapping.RootClasses.First(r => r.Name.Contains("Parent"));
            var relation = rc.Properties.First(p => p.Name == "Children");
            var collection = (HbmBag)relation;
            collection.Key.ondelete.Should().Be(HbmOndelete.Noaction);
        }
        public void WhenExplicitManyToManyThenShouldMapSimpleRelationAsManyToOneByDefault()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<User>();
            orm.TablePerClass<Group>();
            orm.ManyToMany<Group, User>();

            orm.IsManyToOne(typeof(Group), typeof(User)).Should().Be.True();
            orm.IsManyToMany(typeof(Group), typeof(User)).Should().Be.True();
            orm.IsOneToMany(typeof(Group), typeof(User)).Should().Be.False();
            orm.IsOneToOne(typeof(Group), typeof(User)).Should().Be.False();
            orm.IsManyToMany(typeof(User), typeof(Group)).Should().Be.True();
            orm.IsOneToMany(typeof(User), typeof(Group)).Should().Be.False();
        }
Пример #43
0
        public void WhenInterfaceIsImplementedByEntityThenApplyInverse()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();

            var mapper  = new Mapper(orm);
            var mapping = mapper.CompileMappingFor(new[] { typeof(Parent) });

            var hbmClass = mapping.RootClasses.Single(x => x.Name == "Parent");
            var hbmBag   = (HbmBag)hbmClass.Properties.Single(x => x.Name == "Children");

            hbmBag.Inverse.Should().Be.True();
        }
Пример #44
0
        public void WhenInterfaceIsImplementedByEntityThenApplyCascade()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();

            var mapper  = new Mapper(orm);
            var mapping = mapper.CompileMappingFor(new[] { typeof(Parent) });

            var hbmClass = mapping.RootClasses.Single(x => x.Name == "Parent");
            var hbmBag   = (HbmBag)hbmClass.Properties.Single(x => x.Name == "Children");

            hbmBag.Cascade.Should().Contain("all").And.Contain("delete-orphan");
        }
        public void WhenManyToManyIsNotManyToOne()
        {
            var mapper = new ObjectRelationalMapper();
            mapper.TablePerClass<AEntity>();
            mapper.TablePerClass<BEntity>();
            mapper.ManyToMany<AEntity, BEntity>();
            mapper.IsOneToMany(typeof(AEntity), typeof(BEntity)).Should().Be.False();
            mapper.IsOneToMany(typeof(BEntity), typeof(AEntity)).Should().Be.False();

            // Commented because CfgORM-5
            // many-to-many should work only inside a collection.
            // many-to-one should work only outside a collection.
            //mapper.IsManyToOne(typeof(AEntity), typeof(BEntity)).Should().Be.False();
            //mapper.IsManyToOne(typeof(BEntity), typeof(AEntity)).Should().Be.False();
        }
Пример #46
0
        public void WhenTheDomainContainsMoreImplsThenMatch()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <MyEntity>();
            orm.TablePerClass <MyRoot1>();
            orm.TablePerClass <MyRelatedRoot2>();

            orm.AddToDomain(typeof(MyRelatedNoRoot1));

            var pattern = new PolymorphismBidirectionalAnyToManyPattern(orm);

            pattern.Match(ForClass <MyRelatedNoRoot1> .Property(x => x.Items)).Should().Be.True();
            pattern.Match(ForClass <MyRelatedRoot2> .Property(x => x.Items)).Should().Be.True();
        }
Пример #47
0
        public void WhenExplicitManyToManyThenShouldMapSimpleRelationAsManyToOneByDefault()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <User>();
            orm.TablePerClass <Group>();
            orm.ManyToMany <Group, User>();

            orm.IsManyToOne(typeof(Group), typeof(User)).Should().Be.True();
            orm.IsManyToMany(typeof(Group), typeof(User)).Should().Be.True();
            orm.IsOneToMany(typeof(Group), typeof(User)).Should().Be.False();
            orm.IsOneToOne(typeof(Group), typeof(User)).Should().Be.False();
            orm.IsManyToMany(typeof(User), typeof(Group)).Should().Be.True();
            orm.IsOneToMany(typeof(User), typeof(Group)).Should().Be.False();
        }
Пример #48
0
        public void WhenInterfaceIsImplementedByEntityThenRecognizeOneToMany()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <MyEntity>();
            orm.TablePerClass <MyRelation>();
            orm.TablePerClass <MyRelation1>();

            var mapper  = new Mapper(orm);
            var mapping = mapper.CompileMappingFor(new[] { typeof(MyEntity) });

            HbmClass rc = mapping.RootClasses.First(r => r.Name.Contains("MyEntity"));
            var      hbmBagOfIRelation = (HbmBag)rc.Properties.Where(p => p.Name == "Relations").Single();

            hbmBagOfIRelation.ElementRelationship.Should().Be.InstanceOf <HbmOneToMany>();
        }
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<AEntity>();
            orm.TablePerClass<BEntity>();
            orm.OneToOne<AEntity, BEntity>();
            orm.Cascade<AEntity, BEntity>(Cascade.Persist | Cascade.Remove);
            HbmMapping mapping = GetMapping(orm);

            // the default behaviour map an unidirectional one-to-one as a many-to-one (for NHibernate)
            HbmClass rc = mapping.RootClasses.First(r => r.Name.Contains("AEntity"));
            rc.Properties.Should().Have.Count.EqualTo(2);
            rc.Properties.Select(p => p.Name).Should().Have.SameValuesAs("Name", "B");
            var relation = rc.Properties.First(p => p.Name == "B");
            ((HbmManyToOne)relation).cascade.Should().Contain("persist").And.Contain("delete");
        }
Пример #50
0
 private static HbmMapping GetMapping()
 {
     var orm = new ObjectRelationalMapper();
     var mapper = new Mapper(orm,
     new CoolPatternsAppliersHolder(orm));
     orm.TablePerClassHierarchy<Product>();
     orm.TablePerClass<ActorRole>();
     orm.Patterns.PoidStrategies.Add(
     new GuidOptimizedPoidPattern());
     orm.VersionProperty<Entity>(x => x.Version);
     orm.NaturalId<Product>(p => p.Name);
     orm.Cascade<Movie, ActorRole>(
     Cascade.All | Cascade.DeleteOrphans);
     mapper.AddPropertyPattern(mi =>
     mi.GetPropertyOrFieldType() == typeof(Decimal) &&
     mi.Name.Contains("Price"),
     pm => pm.Type(NHibernateUtil.Currency));
     mapper.AddPropertyPattern(mi =>
     orm.IsRootEntity(mi.DeclaringType) &&
     !"Description".Equals(mi.Name),
     pm => pm.NotNullable(true));
     mapper.Subclass<Movie>(cm =>
     cm.List(movie => movie.Actors,
     colm => colm.Index(
     lim => lim.Column("ActorIndex")), m => { }));
     var domainClasses = typeof(Entity).Assembly.GetTypes()
     .Where(t => typeof(Entity).IsAssignableFrom(t));
     return mapper.CompileMappingFor(domainClasses);
 }
Пример #51
0
        public void WhenOrmCascadeIsAllThenApplyOndeleteCascade()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();
            orm.ManyToOne <Child, Parent>();
            orm.Cascade <Parent, Child>(CascadeOn.All);
            HbmMapping mapping = GetMapping(orm);

            HbmClass rc         = mapping.RootClasses.First(r => r.Name.Contains("Parent"));
            var      relation   = rc.Properties.First(p => p.Name == "Children");
            var      collection = (HbmBag)relation;

            collection.Key.ondelete.Should().Be(HbmOndelete.Cascade);
        }
Пример #52
0
        public void WhenOrmCascadeDoesNotIncludeDeleteNorDeleteOrhphanThenNotApplyOndeleteCascade()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();
            orm.ManyToOne <Child, Parent>();
            orm.Cascade <Parent, Child>(CascadeOn.Persist | CascadeOn.ReAttach);
            HbmMapping mapping = GetMapping(orm);

            HbmClass rc         = mapping.RootClasses.First(r => r.Name.Contains("Parent"));
            var      relation   = rc.Properties.First(p => p.Name == "Children");
            var      collection = (HbmBag)relation;

            collection.Key.ondelete.Should().Be(HbmOndelete.Noaction);
        }
Пример #53
0
        public void ComposingPatternsAppliersPacksUsingInflector()
        {
            // In this example you can see how use the inflector adding some special class name translation.

            var orm = new ObjectRelationalMapper();

            // Creation of inflector adding some special class name translation
            var inflector = new SpanishInflector();

            AddCustomData_Dictionary(inflector);

            IPatternsAppliersHolder patternsAppliers =
                (new SafePropertyAccessorPack())
                .Merge(new OneToOneRelationPack(orm))
                .Merge(new BidirectionalManyToManyRelationPack(orm))
                .Merge(new BidirectionalOneToManyRelationPack(orm))
                .Merge(new DiscriminatorValueAsClassNamePack(orm))
                .Merge(new CoolColumnsNamingPack(orm))
                .Merge(new TablePerClassPack())
                .Merge(new PluralizedTablesPack(orm, inflector))                         // <===
                .Merge(new ListIndexAsPropertyPosColumnNameApplier());

            // Instancing the Mapper using the result of Merge
            var mapper = new Mapper(orm, patternsAppliers);

            orm.TablePerClass <UsuarioWeb>();

            var mapping = mapper.CompileMappingFor(new[] { typeof(UsuarioWeb) });

            Console.Write(mapping.AsString());
        }
        public void WhenCustomizePropertyOnInterfaceThenApplyCustomizationOnImplementations()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<MyClass1>();
            orm.TablePerClass<MyClass2>();

            var mapper = new Mapper(orm);
            mapper.Customize<IHasMessage>(x=> x.Property(hasMessage=> hasMessage.Message, pm=> pm.Length(123)));
            var mappings = mapper.CompileMappingFor(new[] { typeof(MyClass1), typeof(MyClass2) });

            HbmClass hbmMyClass1 = mappings.RootClasses.Single(hbm => hbm.Name == "MyClass1");
            hbmMyClass1.Properties.OfType<HbmProperty>().Where(p=> p.Name == "Message").Single().length.Should().Be("123");

            HbmClass hbmMyClass2 = mappings.RootClasses.Single(hbm => hbm.Name == "MyClass2");
            hbmMyClass2.Properties.OfType<HbmProperty>().Where(p => p.Name == "Message").Single().length.Should().Be("123");
        }
        public void ComposingPatternsAppliersPacksUsingInflector()
        {
            // In this example I'll use the same domain, but composing others packs, changing the strategy for the hierarchy

            var orm = new ObjectRelationalMapper();

            // The follow line show how compose patterns-appliers-packs and patterns-appliers
            IPatternsAppliersHolder patternsAppliers =
                (new SafePropertyAccessorPack())
                    .Merge(new OneToOneRelationPack(orm))
                    .Merge(new BidirectionalManyToManyRelationPack(orm))
                    .Merge(new BidirectionalOneToManyRelationPack(orm))
                    .Merge(new DiscriminatorValueAsClassNamePack(orm))
                    .Merge(new CoolColumnsNamingPack(orm))
                    .Merge(new TablePerClassPack())
                    .Merge(new PluralizedTablesPack(orm, new EnglishInflector()))
                    .Merge(new ListIndexAsPropertyPosColumnNameApplier())
                    .Merge(new DatePropertyByNameApplier())
                    .Merge(new MsSQL2008DateTimeApplier());

            // Instancing the Mapper using the result of Merge
            var mapper = new Mapper(orm, patternsAppliers);

            // Note: I'm declaring the strategy only for the base entity
            orm.TablePerClass<Animal>();

            // Note : I have to create mappings for the whole domain
            var mapping = mapper.CompileMappingFor(typeof(Animal).Assembly.GetTypes().Where(t => t.Namespace == typeof(Animal).Namespace));
            Console.Write(mapping.AsString());
        }
        public void ComposingPatternsAppliersPacksUsingInflector()
        {
            // In this example I'll use the same domain, but composing others packs, changing the strategy for the hierarchy

            var orm = new ObjectRelationalMapper();

            // The follow line show how compose patterns-appliers-packs and patterns-appliers
            IPatternsAppliersHolder patternsAppliers =
                (new SafePropertyAccessorPack())
                .Merge(new OneToOneRelationPack(orm))
                .Merge(new BidirectionalManyToManyRelationPack(orm))
                .Merge(new BidirectionalOneToManyRelationPack(orm))
                .Merge(new DiscriminatorValueAsClassNamePack(orm))
                .Merge(new CoolColumnsNamingPack(orm))
                .Merge(new TablePerClassPack())
                .Merge(new PluralizedTablesPack(orm, new EnglishInflector()))
                .Merge(new ListIndexAsPropertyPosColumnNameApplier())
                .Merge(new DatePropertyByNameApplier())
                .Merge(new MsSQL2008DateTimeApplier());

            // Instancing the Mapper using the result of Merge
            var mapper = new Mapper(orm, patternsAppliers);

            // Note: I'm declaring the strategy only for the base entity
            orm.TablePerClass <Animal>();

            // Note : I have to create mappings for the whole domain
            var mapping = mapper.CompileMappingFor(typeof(Animal).Assembly.GetTypes().Where(t => t.Namespace == typeof(Animal).Namespace));

            Console.Write(mapping.AsString());
        }
 public void RegisteringComponentAndEntityThrow()
 {
     var mapper = new ObjectRelationalMapper();
     mapper.Component<AComponent>();
     ActionAssert.Throws(() => mapper.TablePerClass<AComponent>()).Should().Be.InstanceOf<MappingException>();
     ActionAssert.Throws(() => mapper.TablePerConcreteClass<AComponent>()).Should().Be.InstanceOf<MappingException>();
     ActionAssert.Throws(() => mapper.TablePerClassHierarchy<AComponent>()).Should().Be.InstanceOf<MappingException>();
 }
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Node>();
            HbmMapping mapping = GetMapping(orm);

            VerifyMapping(mapping);
        }
        public void WhenRegisterPersistentPropertyOnInterfaceThenShouldRecognizePropertyOfConcreteImpl()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Person>();
            orm.PersistentProperty<IEntity>(p => p.IsValid);

            orm.IsPersistentProperty(ForClass<Person>.Property(p => p.IsValid)).Should().Be.True();
        }
        public void WhenRegisterExclusionPropertyOnInterfaceThenShouldExcludePropertyOfConcreteImpl()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Person>();
            orm.ExcludeProperty<IEntity>(p => p.Something);

            orm.IsPersistentProperty(ForClass<Person>.Property(p => p.Something)).Should().Be.False();
        }