예제 #1
0
        public static HbmMapping GetMapings(ObjectRelationalMapper orm)
        {

            domainEntities =
                typeof(Entity).Assembly.GetTypes()
                    .Where(t => typeof(Entity).IsAssignableFrom(t) && !t.IsGenericType)
                    .ToList();
            orm.Cascade<Claim, CPS.Domain.Action>(CascadeOn.None);
            orm.Cascade<Branch, Claim>(CascadeOn.None);
            orm.Cascade<Domain.Action,Document>(CascadeOn.All);

            orm.Component<Address>();
            orm.Component<Incident>();
            orm.Component<ContactInfo>();

            var patterns = 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 ListIndexAsPropertyPosColumnNameApplier())
                .Merge(new PluralizedTablesPack(orm, new EnglishInflector()))
                .Merge(new MsSQL2008DateTimeApplier());


            var mapper = new Mapper(orm, patterns);

            var mapping = mapper.CompileMappingFor(domainEntities);
            Debug.WriteLine(mapping.AsString());
            return mapping;
        }
예제 #2
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);
 }
예제 #3
0
        private void ConfOrmMapping(ObjectRelationalMapper orm, Mapper mapper)
        {
            orm.TablePerClassHierarchy <Product>();

            orm.ManyToMany <Customer, Product>();
            orm.Cascade <Movie, ActorRole>(Cascade.All);

            orm.NaturalId <Customer>(c => c.Name);
        }
예제 #4
0
        public HbmMapping GenerateMappigs()
        {
            var orm = new ObjectRelationalMapper();

            orm.Patterns.PoidStrategies.Add(new NativePoidPattern());

            //// map .NET4 ISet<T> as a NHibernate's set
            orm.Patterns.Sets.Add(new UseSetWhenGenericCollectionPattern());
            var englishInflector = new EnglishInflector();
            IPatternsAppliersHolder patternsAppliers = (new SafePropertyAccessorPack())
                                                       .Merge(new ClassPluralizedTableApplier(englishInflector))
                                                       .Merge(new DiscriminatorValueAsEnumValuePack <ChildA, Types>(orm))
                                                       .Merge(new DiscriminatorValueAsEnumValuePack <ChildB, Types>(orm))
                                                       .Merge(new CoolPatternsAppliersHolder(orm));

            IEnumerable <Type> allPersistEntities = GetDomainEntities();

            IEnumerable <Type> roots = allPersistEntities.Where(t => t.IsAbstract && t.InheritedFromBaseEntity());

            IEnumerable <Type> hierarchyEntities = allPersistEntities.Where(t => typeof(IHierarchyEntity).IsAssignableFrom(t));

            IEnumerable <Type> separateEntities = allPersistEntities.Except(roots).Except(hierarchyEntities);

            orm.TablePerConcreteClass(separateEntities);

            var hierarchyRoots = hierarchyEntities.Where(t => t.IsAbstract && t.InheritedFromBaseEntity());

            orm.TablePerClassHierarchy(hierarchyRoots);

            orm.Cascade <ChildA, Container>(CascadeOn.Persist | CascadeOn.Merge);
            orm.Cascade <Container, ChildA>(CascadeOn.Persist);

            orm.Cascade <ChildB, Container>(CascadeOn.Persist | CascadeOn.Merge);
            orm.Cascade <Container, ChildB>(CascadeOn.Persist);

            var mapper = new Mapper(orm, patternsAppliers);

            HbmMapping mapping = mapper.CompileMappingFor(allPersistEntities);

            File.WriteAllText(@"c:\Test\mappings.xml", Serialize(mapping)); // сохраняем маппинги в файл.
            return(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);
        }
        public void IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Person>();
            orm.TablePerClass<Animal>();
            orm.ManyToMany<Person, Animal>();
            orm.Cascade<Person, Animal>(Cascade.Persist);
            HbmMapping mapping = GetMapping(orm);

            VerifyMapping(mapping);
        }
예제 #7
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 IntegrationWithObjectRelationalMapper()
        {
            var orm = new ObjectRelationalMapper();

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

            VerifyMapping(mapping);
        }
예제 #9
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);
        }
        public void WhenCascadeIsTurnedOffInOrmShouldntApplyOnDeleteCascade()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass(new[] { typeof(UpAggregateRoot), typeof(DownAggregateRoot) });
            orm.Cascade<UpAggregateRoot, DownAggregateRoot>(Cascade.None);

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

            HbmClass rc = mapping.RootClasses.Single();
            var subNodes = (HbmBag)rc.Properties.Single(p => p.Name == "DownAggregateRoots");
            subNodes.Key.ondelete.Should().Be(HbmOndelete.Noaction);
        }
예제 #11
0
        public void WhenExplicitRequiredByClassApplyCascade()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Node>();
            orm.Cascade<Node, Node>(CascadeOn.Persist);

            HbmMapping mapping = GetMapping(orm);

            HbmClass rc = mapping.RootClasses.Single();
            var parent = (HbmManyToOne)rc.Properties.Single(p => p.Name == "Parent");
            parent.cascade.Should().Contain("persist");
            var subNodes = (HbmBag)rc.Properties.Single(p => p.Name == "Subnodes");
            subNodes.cascade.Should().Contain("persist");
        }
        public void WhenExplicitRequiredByClassApplyCascade()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Node>();
            orm.Cascade<Node, Node>(Cascade.Persist);

            HbmMapping mapping = GetMapping(orm);

            HbmClass rc = mapping.RootClasses.Single();
            var parent = (HbmManyToOne)rc.Properties.Single(p => p.Name == "Parent");
            parent.cascade.Should().Contain("persist");
            var subNodes = (HbmBag)rc.Properties.Single(p => p.Name == "Subnodes");
            subNodes.cascade.Should().Contain("persist");
        }
        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 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);
        }
예제 #15
0
        public void WhenCascadeIsTurnedOffInOrmShouldntApplyOnDeleteCascade()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass(new[] { typeof(UpAggregateRoot), typeof(DownAggregateRoot) });
            orm.Cascade <UpAggregateRoot, DownAggregateRoot>(Cascade.None);

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

            HbmClass rc       = mapping.RootClasses.Single();
            var      subNodes = (HbmBag)rc.Properties.Single(p => p.Name == "DownAggregateRoots");

            subNodes.Key.ondelete.Should().Be(HbmOndelete.Noaction);
        }
예제 #16
0
        public void WhenInterfaceIsImplementedByEntityAndExplicitCascadeDeclaredOnInterfaceThenApplyDeclaredCascade()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Node>();

            orm.Cascade <INode, INode>(CascadeOn.Persist);

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

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

            hbmBag.Cascade.Should().Contain("persist").And.Not.Contain("delete-orphan");
        }
        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");
        }
예제 #18
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);
        }
예제 #19
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);
        }
예제 #20
0
        public void WithExplicitCascadeToNone()
        {
            var orm = new ObjectRelationalMapper();

            orm.TablePerClass <Parent>();
            orm.TablePerClass <Child>();
            orm.ManyToOne <Child, Parent>();
            orm.Cascade <Parent, Child>(CascadeOn.None);
            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.Cascade.Satisfy(c => string.IsNullOrEmpty(c));
        }
예제 #21
0
        public void WhenInterfaceIsImplementedByEntityAndExplicitCascadeDeclaredOnConcreteThenNotApplyOnDelete()
        {
            var orm = new ObjectRelationalMapper();

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

            orm.Cascade <Parent, Child>(CascadeOn.Persist);

            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.Key.ondelete.Should().Be(HbmOndelete.Noaction);
        }
        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");
        }
예제 #23
0
        private static ObjectRelationalMapper GetORM()
        {
            var orm = new ObjectRelationalMapper();
              orm.TablePerClassHierarchy<Product>();
              orm.TablePerClass<ActorRole>();

              orm.NaturalId<Product>(p => p.Name);

              orm.Cascade<Movie, ActorRole>(
            Cascade.All | Cascade.DeleteOrphans);

              orm.Patterns.PoidStrategies
            .Add(new GuidOptimizedPoidPattern());

              orm.Patterns.Versions
            .Add(new MyVersionPattern());

              return orm;
        }
        public void WithExplicitCascadeToNone()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Parent>();
            orm.TablePerClass<Child>();
            orm.ManyToOne<Child, Parent>();
            orm.Cascade<Parent, Child>(Cascade.None);
            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.Cascade.Satisfy(c => string.IsNullOrEmpty(c));
        }
        public void WithExplicitCascadeToAll()
        {
            var orm = new ObjectRelationalMapper();
            orm.TablePerClass<Parent>();
            orm.TablePerClass<Child>();
            orm.ManyToOne<Child, Parent>();
            orm.Cascade<Parent, Child>(Cascade.All | Cascade.DeleteOrphans);
            HbmMapping mapping = GetMapping(orm);

            VerifyMapping(mapping);
        }