Skip to content

NH-2930 - Add ability to specify abstract="true" in mapping by code #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.MappingByCode.ExpliticMappingTests
namespace NHibernate.Test.MappingByCode.ExplicitMappingTests
{
public class BasicMappingOfSimpleClass
{
Expand All @@ -16,6 +16,25 @@ public class MyClass
public string Something { get; set; }
}

[Test]
public void AbstractClass()
{
//NH-3527
var mapper = new ModelMapper();
mapper.Class<MyClass>(ca =>
{
ca.Abstract(true);
ca.Id(x => x.Id, map =>
{
map.Column("MyClassId");
map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
});
ca.Property(x => x.Something, map => map.Length(150));
});
var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
Assert.AreEqual(hbmMapping.RootClasses[0].@abstract, true);
}

[Test]
public void MapClassWithIdAndProperty()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,23 @@ public void WhenRegisteredAsJoinedSubclassThenIsEntity()

Assert.That(inspector.IsEntity(typeof(Inherited1)), Is.True);
}

[Test]
public void JoinedSubclassIsAbstract()
{
//NH-3527
var modelMapper = new ModelMapper();
modelMapper.Class<MyClass>(c => { });
modelMapper.JoinedSubclass<Inherited1>(c =>
{
c.Abstract(true);
c.Extends(typeof(MyClass));
});

var mappings = modelMapper.CompileMappingForAllExplicitlyAddedEntities();

Assert.IsTrue(mappings.JoinedSubclasses[0].@abstract);
Assert.IsTrue(mappings.JoinedSubclasses[0].extends == typeof(MyClass).FullName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,23 @@ public void WhenRegisteredAsSubclassThenIsEntity()

Assert.That(inspector.IsEntity(typeof(Inherited1)), Is.True);
}

[Test]
public void SubclassIsAbstract()
{
//NH-3527
var modelMapper = new ModelMapper();
modelMapper.Class<MyClass>(c => { });
modelMapper.Subclass<Inherited1>(c =>
{
c.Abstract(true);
c.Extends(typeof(MyClass));
});

var mappings = modelMapper.CompileMappingForAllExplicitlyAddedEntities();

Assert.IsTrue(mappings.SubClasses[0].@abstract);
Assert.IsTrue(mappings.SubClasses[0].extends == typeof(MyClass).FullName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,23 @@ public void WhenRegisteredAsUnionSubclassThenIsEntity()

Assert.That(inspector.IsEntity(typeof(Inherited1)), Is.True);
}

[Test]
public void UnionSubclassIsAbstract()
{
//NH-3527
var modelMapper = new ModelMapper();
modelMapper.Class<MyClass>(c => { });
modelMapper.UnionSubclass<Inherited1>(c =>
{
c.Abstract(true);
c.Extends(typeof(MyClass));
});

var mappings = modelMapper.CompileMappingForAllExplicitlyAddedEntities();

Assert.IsTrue(mappings.UnionSubclasses[0].@abstract);
Assert.IsTrue(mappings.UnionSubclasses[0].extends == typeof(MyClass).FullName);
}
}
}
2 changes: 2 additions & 0 deletions src/NHibernate/Mapping/ByCode/IClassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IClassAttributesMapper : IEntityAttributesMapper, IEntitySqlsMa
void ComponentAsId(MemberInfo idProperty, Action<IComponentAsIdMapper> idMapper);
void ComposedId(Action<IComposedIdMapper> idPropertiesMapping);

void Abstract(bool isAbstract);
void Discriminator(Action<IDiscriminatorMapper> discriminatorMapping);
void DiscriminatorValue(object value);
void Table(string tableName);
Expand Down Expand Up @@ -53,6 +54,7 @@ public interface IClassAttributesMapper<TEntity> : IEntityAttributesMapper, IEnt

void ComposedId(Action<IComposedIdMapper<TEntity>> idPropertiesMapping);

void Abstract(bool isAbstract);
void Discriminator(Action<IDiscriminatorMapper> discriminatorMapping);
void DiscriminatorValue(object value);
void Table(string tableName);
Expand Down
5 changes: 4 additions & 1 deletion src/NHibernate/Mapping/ByCode/IJoinedSubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ public interface IJoinedSubclassAttributesMapper : IEntityAttributesMapper, IEnt
void Key(Action<IKeyMapper> keyMapping);
void Extends(System.Type baseType);
void SchemaAction(SchemaAction action);
void Filter(string filterName, Action<IFilterMapper> filterMapping);
void Filter(string filterName, Action<IFilterMapper> filterMapping);
void Abstract(bool isAbstract);
}

public interface IJoinedSubclassMapper : IJoinedSubclassAttributesMapper, IPropertyContainerMapper {}

public interface IJoinedSubclassAttributesMapper<TEntity> : IEntityAttributesMapper, IEntitySqlsMapper where TEntity : class
{
void Abstract(bool isAbstract);
void Extends(System.Type baseType);
void Table(string tableName);
void Catalog(string catalogName);
void Schema(string schemaName);
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Mapping/ByCode/ISubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface ISubclassAttributesMapper : IEntityAttributesMapper, IEntitySql
void DiscriminatorValue(object value);
void Extends(System.Type baseType);
void Filter(string filterName, Action<IFilterMapper> filterMapping);
void Abstract(bool isAbstract);
}

public interface ISubclassMapper : ISubclassAttributesMapper, IPropertyContainerMapper
Expand All @@ -18,6 +19,8 @@ public interface ISubclassAttributesMapper<TEntity> : IEntityAttributesMapper, I
{
void DiscriminatorValue(object value);
void Filter(string filterName, Action<IFilterMapper> filterMapping);
void Extends(System.Type baseType);
void Abstract(bool isAbstract);
}

public interface ISubclassMapper<TEntity> : ISubclassAttributesMapper<TEntity>, IPropertyContainerMapper<TEntity> where TEntity : class
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Mapping/ByCode/IUnionSubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IUnionSubclassAttributesMapper : IEntityAttributesMapper, IEnti
void Catalog(string catalogName);
void Schema(string schemaName);
void Extends(System.Type baseType);
void Abstract(bool isAbstract);
}

public interface IUnionSubclassMapper : IUnionSubclassAttributesMapper, IPropertyContainerMapper {}
Expand All @@ -15,6 +16,8 @@ public interface IUnionSubclassAttributesMapper<TEntity> : IEntityAttributesMapp
void Table(string tableName);
void Catalog(string catalogName);
void Schema(string schemaName);
void Extends(System.Type baseType);
void Abstract(bool isAbstract);
}

public interface IUnionSubclassMapper<TEntity> : IUnionSubclassAttributesMapper<TEntity>, IPropertyContainerMapper<TEntity> where TEntity : class {}
Expand Down
6 changes: 6 additions & 0 deletions src/NHibernate/Mapping/ByCode/Impl/ClassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public Dictionary<string, IJoinMapper> JoinMappers
}

#region Implementation of IClassMapper
public void Abstract(bool isAbstract)
{
classMapping.@abstract = isAbstract;
classMapping.abstractSpecified = true;
}

public void OptimisticLock(OptimisticLockMode mode)
{
classMapping.optimisticlock = (HbmOptimisticLockMode)Enum.Parse(typeof(OptimisticLockMode), mode.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ private Dictionary<string, IJoinMapper<TEntity>> JoinCustomizers
}

#region Implementation of IClassAttributesMapper<TEntity>
public void Abstract(bool isAbstract)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.Abstract(isAbstract));
}

public void OptimisticLock(OptimisticLockMode mode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ public void Subselect(string sql)
#endregion

#region Implementation of IJoinedSubclassAttributesMapper<TEntity>
public void Extends(System.Type baseType)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IJoinedSubclassAttributesMapper m) => m.Extends(baseType));
}

public void Abstract(bool isAbstract)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IJoinedSubclassAttributesMapper m) => m.Abstract(isAbstract));
}

public void Table(string tableName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public SubclassCustomizer(IModelExplicitDeclarationsHolder explicitDeclarationsH
}

#region ISubclassMapper<TEntity> Members
public void Extends(System.Type baseType)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (ISubclassMapper m) => m.Extends(baseType));
}

public void Abstract(bool isAbstract)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (ISubclassMapper m) => m.Abstract(isAbstract));
}

public void DiscriminatorValue(object value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ public void Subselect(string sql)
#endregion

#region Implementation of IUnionSubclassAttributesMapper<TEntity>
public void Extends(System.Type baseType)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IUnionSubclassAttributesMapper m) => m.Extends(baseType));
}

public void Abstract(bool isAbstract)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IUnionSubclassAttributesMapper m) => m.Abstract(isAbstract));
}

public void Table(string tableName)
{
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/Mapping/ByCode/Impl/JoinedSubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public void Subselect(string sql)
#endregion

#region Implementation of IJoinedSubclassAttributesMapper
public void Abstract(bool isAbstract)
{
classMapping.@abstract = isAbstract;
classMapping.abstractSpecified = true;
}

public void Table(string tableName)
{
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/Mapping/ByCode/Impl/SubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public Dictionary<string, IJoinMapper> JoinMappers
}

#region ISubclassMapper Members
public void Abstract(bool isAbstract)
{
classMapping.@abstract = isAbstract;
classMapping.abstractSpecified = true;
}

public void DiscriminatorValue(object value)
{
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/Mapping/ByCode/Impl/UnionSubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ public void Subselect(string sql)
#endregion

#region Implementation of IUnionSubclassAttributesMapper
public void Abstract(bool isAbstract)
{
classMapping.@abstract = isAbstract;
classMapping.abstractSpecified = true;
}

public void Table(string tableName)
{
Expand Down