Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
govorovvs committed Nov 16, 2017
1 parent 4b3d8e3 commit 04ea60e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 74 deletions.
37 changes: 32 additions & 5 deletions src/FluentNHibernate.Specs/Automapping/OverrideSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,34 @@ public class when_using_an_automapping_override_to_create_a_join

It should_exclude_the_join_mapped_property_from_the_main_automapping = () =>
mapping.Properties.Select(x => x.Name).ShouldNotContain("One");


static AutoPersistenceModel model;
static ClassMapping mapping;
}


public class when_using_an_automapping_override_to_specify_a_discriminators_and_join_on_subclass
{
private Establish context = () =>
model = AutoMap.Source(new StubTypeSource(typeof(Parent), typeof(Child)))
.Override<Parent>(map =>
map.DiscriminateSubClassesOnColumn("type"))
.Override<Child>(map => map.Join("table", part => { }));

private Because of = () =>
mapping = model.BuildMappingFor<Parent>();



It should_not_create_the_join_mapping = () =>
mapping.Joins.ShouldBeEmpty();

It should_map_the_discriminator = () =>
mapping.Discriminator.ShouldNotBeNull();

It should_map_subclasses_as_joined_subclasses = () =>
mapping.Subclasses.ShouldEachConformTo(x => x.Joins.Any());

static AutoPersistenceModel model;
static ClassMapping mapping;
}
Expand All @@ -51,7 +78,7 @@ public class when_using_an_automapping_override_to_specify_a_discriminator
mapping.Subclasses.Count().ShouldEqual(1);
mapping.Subclasses.ShouldEachConformTo(x => x.SubclassType == SubclassType.Subclass);
};

static AutoPersistenceModel model;
static ClassMapping mapping;
}
Expand Down Expand Up @@ -99,15 +126,15 @@ public class when_multiple_overrides_present_in_one_class
parentMapping.TableName.ShouldEqual("fancyTableName_Parent");
bParentMapping.BatchSize.ShouldEqual(50);
};


static AutoPersistenceModel model;
static ClassMapping entityMapping;
static ClassMapping parentMapping;
static ClassMapping bParentMapping;
}

public class MultipleOverrides: IAutoMappingOverride<Entity>, IAutoMappingOverride<Parent>, IAutoMappingOverride<B_Parent>
public class MultipleOverrides : IAutoMappingOverride<Entity>, IAutoMappingOverride<Parent>, IAutoMappingOverride<B_Parent>
{
public void Override(AutoMapping<Entity> mapping)
{
Expand All @@ -124,4 +151,4 @@ public void Override(AutoMapping<B_Parent> mapping)
mapping.BatchSize(50);
}
}
}
}
12 changes: 6 additions & 6 deletions src/FluentNHibernate/Automapping/AutoMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AutoMapping<T> : ClassMap<T>, IAutoClasslike, IPropertyIgnorer

public AutoMapping(IList<Member> mappedMembers)
: this(mappedMembers, new AttributeStore(), new MappingProviderStore())
{}
{ }

AutoMapping(IList<Member> mappedMembers, AttributeStore attributes, MappingProviderStore providers)
: base(attributes, providers)
Expand Down Expand Up @@ -61,12 +61,12 @@ void IAutoClasslike.AlterModel(ClassMappingBase mapping)
if (Cache.IsDirty)
classMapping.Set(x => x.Cache, Layer.Defaults, ((ICacheMappingProvider)Cache).GetCacheMapping());

foreach (var join in providers.Joins)
classMapping.AddJoin(join.GetJoinMapping());

classMapping.Set(x => x.Tuplizer, Layer.Defaults, providers.TuplizerMapping);
}

foreach (var join in providers.Joins)
mapping.AddOrReplaceJoin(join.GetJoinMapping());

foreach (var property in providers.Properties)
mapping.AddOrReplaceProperty(property.GetPropertyMapping());

Expand Down Expand Up @@ -144,7 +144,7 @@ public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyCol

public IAutoClasslike JoinedSubClass(Type type, string keyColumn)
{
var genericType = typeof (AutoJoinedSubClassPart<>).MakeGenericType(type);
var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(type);
var joinedclass = (ISubclassMappingProvider)Activator.CreateInstance(genericType, keyColumn);

// remove any mappings for the same type, then re-add
Expand Down Expand Up @@ -193,7 +193,7 @@ public IAutoClasslike SubClass(Type type, string discriminatorValue)

// hide the base one D:
new void Join(string table, Action<JoinPart<T>> action)
{}
{ }

public void Join(string table, Action<AutoJoinPart<T>> action)
{
Expand Down
9 changes: 0 additions & 9 deletions src/FluentNHibernate/Mapping/CollectionCascadeExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ public CollectionCascadeExpression(TParent parent, Action<string> setter)
this.setter = setter;
}

/// <summary>
/// Cascade all actions, deleting any orphaned records
/// </summary>
public TParent AllDeleteOrphan()
{
setter("all-delete-orphan");
return parent;
}

/// <summary>
/// Cascade deletes, deleting any orphaned records
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public void AddJoin(JoinMapping mapping)
mappedMembers.AddJoin(mapping);
}

public void AddOrReplaceJoin(JoinMapping mapping)
{
mappedMembers.AddOrReplaceJoin(mapping);
}

public void AddFilter(FilterMapping mapping)
{
mappedMembers.AddFilter(mapping);
Expand Down
78 changes: 24 additions & 54 deletions src/FluentNHibernate/MappingModel/MappedMembers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.Utils;
using FluentNHibernate.Visitors;

namespace FluentNHibernate.MappingModel
Expand All @@ -18,7 +17,6 @@ internal class MappedMembers : IMapping, IHasMappedMembers
private readonly List<JoinMapping> joins;
private readonly List<FilterMapping> filters;
private readonly List<StoredProcedureMapping> storedProcedures;
private readonly List<IMapping> sequencedMappingObject;

public MappedMembers()
{
Expand All @@ -31,7 +29,6 @@ public MappedMembers()
joins = new List<JoinMapping>();
filters = new List<FilterMapping>();
storedProcedures = new List<StoredProcedureMapping>();
sequencedMappingObject = new List<IMapping>();
}

public IEnumerable<PropertyMapping> Properties
Expand Down Expand Up @@ -94,15 +91,12 @@ public void AddProperty(PropertyMapping property)
throw new InvalidOperationException("Tried to add property '" + property.Name + "' when already added.");

properties.Add(property);
sequencedMappingObject.Add(property);
}

public void AddOrReplaceProperty(PropertyMapping mapping)
{
properties.RemoveAll(x => x.Name == mapping.Name);
sequencedMappingObject.RemoveAll(x => x is PropertyMapping && ((PropertyMapping)x).Name == mapping.Name);
properties.Add(mapping);
sequencedMappingObject.Add(mapping);
}

public void AddCollection(Collections.CollectionMapping collection)
Expand All @@ -111,15 +105,12 @@ public void AddCollection(Collections.CollectionMapping collection)
throw new InvalidOperationException("Tried to add collection '" + collection.Name + "' when already added.");

collections.Add(collection);
sequencedMappingObject.Add(collection);
}

public void AddOrReplaceCollection(Collections.CollectionMapping mapping)
{
collections.RemoveAll(x => x.Name == mapping.Name);
sequencedMappingObject.RemoveAll(x => x is Collections.CollectionMapping && ((Collections.CollectionMapping)x).Name == mapping.Name);
collections.Add(mapping);
sequencedMappingObject.Add(mapping);
}

public void AddReference(ManyToOneMapping manyToOne)
Expand All @@ -128,15 +119,12 @@ public void AddReference(ManyToOneMapping manyToOne)
throw new InvalidOperationException("Tried to add many-to-one '" + manyToOne.Name + "' when already added.");

references.Add(manyToOne);
sequencedMappingObject.Add(manyToOne);
}

public void AddOrReplaceReference(ManyToOneMapping manyToOne)
{
references.RemoveAll(x => x.Name == manyToOne.Name);
sequencedMappingObject.RemoveAll(x => x is ManyToOneMapping && ((ManyToOneMapping)x).Name == manyToOne.Name);
references.Add(manyToOne);
sequencedMappingObject.Add(manyToOne);
}

public void AddComponent(IComponentMapping componentMapping)
Expand All @@ -145,15 +133,12 @@ public void AddComponent(IComponentMapping componentMapping)
throw new InvalidOperationException("Tried to add component '" + componentMapping.Name + "' when already added.");

components.Add(componentMapping);
sequencedMappingObject.Add(componentMapping);
}

public void AddOrReplaceComponent(IComponentMapping componentMapping)
{
components.RemoveAll(x => x.Name == componentMapping.Name);
sequencedMappingObject.RemoveAll(x => x is IComponentMapping && ((IComponentMapping)x).Name == componentMapping.Name);
components.Add(componentMapping);
sequencedMappingObject.Add(componentMapping);
}

public void AddOneToOne(OneToOneMapping mapping)
Expand All @@ -162,15 +147,12 @@ public void AddOneToOne(OneToOneMapping mapping)
throw new InvalidOperationException("Tried to add one-to-one '" + mapping.Name + "' when already added.");

oneToOnes.Add(mapping);
sequencedMappingObject.Add(mapping);
}

public void AddOrReplaceOneToOne(OneToOneMapping mapping)
{
oneToOnes.RemoveAll(x => x.Name == mapping.Name);
sequencedMappingObject.RemoveAll(x => x is OneToOneMapping && ((OneToOneMapping)x).Name == mapping.Name);
oneToOnes.Add(mapping);
sequencedMappingObject.Add(mapping);
}

public void AddAny(AnyMapping mapping)
Expand All @@ -179,15 +161,12 @@ public void AddAny(AnyMapping mapping)
throw new InvalidOperationException("Tried to add any '" + mapping.Name + "' when already added.");

anys.Add(mapping);
sequencedMappingObject.Add(mapping);
}

public void AddOrReplaceAny(AnyMapping mapping)
{
anys.RemoveAll(x => x.Name == mapping.Name);
sequencedMappingObject.RemoveAll(x => x is AnyMapping && ((AnyMapping)x).Name == mapping.Name);
anys.Add(mapping);
sequencedMappingObject.Add(mapping);
}

public void AddJoin(JoinMapping mapping)
Expand All @@ -196,7 +175,12 @@ public void AddJoin(JoinMapping mapping)
throw new InvalidOperationException("Tried to add join to table '" + mapping.TableName + "' when already added.");

joins.Add(mapping);
sequencedMappingObject.Add(mapping);
}

public void AddOrReplaceJoin(JoinMapping mapping)
{
joins.RemoveAll(x => x.TableName == mapping.TableName);
joins.Add(mapping);
}

public void AddFilter(FilterMapping mapping)
Expand All @@ -205,49 +189,36 @@ public void AddFilter(FilterMapping mapping)
throw new InvalidOperationException("Tried to add filter with name '" + mapping.Name + "' when already added.");

filters.Add(mapping);
sequencedMappingObject.Add(mapping);
}

public virtual void AcceptVisitor(IMappingModelVisitor visitor)
{
foreach (var mapping in sequencedMappingObject)
TypeSwitch.Do(mapping,
TypeSwitch.Case<Collections.CollectionMapping>(visitor.Visit),
TypeSwitch.Case<PropertyMapping>(visitor.Visit),
TypeSwitch.Case<ManyToOneMapping>(visitor.Visit),
TypeSwitch.Case<IComponentMapping>(visitor.Visit),
TypeSwitch.Case<OneToOneMapping>(visitor.Visit),
TypeSwitch.Case<AnyMapping>(visitor.Visit),
TypeSwitch.Case<JoinMapping>(visitor.Visit),
TypeSwitch.Case<FilterMapping>(visitor.Visit),
TypeSwitch.Case<StoredProcedureMapping>(visitor.Visit)
);
//foreach (var collection in Collections)
// visitor.Visit(collection);
foreach (var collection in Collections)
visitor.Visit(collection);

//foreach (var property in Properties)
// visitor.Visit(property);
foreach (var property in Properties)
visitor.Visit(property);

//foreach (var reference in References)
// visitor.Visit(reference);
foreach (var reference in References)
visitor.Visit(reference);

//foreach (var component in Components)
// visitor.Visit(component);
foreach (var component in Components)
visitor.Visit(component);

//foreach (var oneToOne in oneToOnes)
// visitor.Visit(oneToOne);
foreach (var oneToOne in oneToOnes)
visitor.Visit(oneToOne);

//foreach (var any in anys)
// visitor.Visit(any);
foreach (var any in anys)
visitor.Visit(any);

//foreach (var join in joins)
// visitor.Visit(join);
foreach (var join in joins)
visitor.Visit(join);

//foreach (var filter in filters)
// visitor.Visit(filter);
foreach (var filter in filters)
visitor.Visit(filter);

//foreach (var storedProcedure in storedProcedures)
// visitor.Visit(storedProcedure);
foreach (var storedProcedure in storedProcedures)
visitor.Visit(storedProcedure);
}

public bool IsSpecified(string property)
Expand All @@ -261,7 +232,6 @@ public void Set(string attribute, int layer, object value)
public void AddStoredProcedure(StoredProcedureMapping mapping)
{
storedProcedures.Add(mapping);
sequencedMappingObject.Add(mapping);
}

public bool Equals(MappedMembers other)
Expand Down

0 comments on commit 04ea60e

Please sign in to comment.