Skip to content

Commit

Permalink
fixing inheritance when doing fluent configuration (now implicit)
Browse files Browse the repository at this point in the history
git-svn-id: https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk@1655 d7b3437e-3345-0410-94a8-cbd290e69f67
  • Loading branch information
rogerkratz committed Feb 4, 2011
1 parent c70f626 commit 32c0e4c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using NHibernate.Envers.Configuration.Fluent;
using NHibernate.Envers.Configuration.Store;
Expand All @@ -7,37 +8,45 @@

namespace NHibernate.Envers.Tests.NetSpecific.UnitTests.Fluent
{
[TestFixture]
public class InheritanceTest
{
private IDictionary<System.Type, IEntityMeta> metas;
[TestFixture]
public class InheritanceTest
{
private IDictionary<System.Type, IEntityMeta> metas;

[SetUp]
public void Setup()
{
var cfg = new FluentConfiguration();
cfg.Audit<Dog>();
metas = cfg.CreateMetaData(null);
}
[SetUp]
public void Setup()
{
var cfg = new FluentConfiguration();
cfg.Audit<Dog>();
cfg.Audit<Cat>();
metas = cfg.CreateMetaData(null);
}

[Test]
public void NumberOfEntityMetas()
{
Assert.AreEqual(2, metas.Count);
}
[Test]
public void NumberOfEntityMetas()
{
Assert.AreEqual(3, metas.Count);
}

[Test]
public void AnimalShouldBeAudited()
{
var entMeta = metas[typeof(Animal)];
entMeta.ClassMetas.OnlyContains<AuditedAttribute>();
}
[Test]
public void AnimalShouldBeAudited()
{
var entMeta = metas[typeof(Animal)];
entMeta.ClassMetas.OnlyContains<AuditedAttribute>();
}

[Test]
public void DogShouldBeAudited()
{
var entMeta = metas[typeof(Dog)];
entMeta.ClassMetas.OnlyContains<AuditedAttribute>();
}
}
[Test]
public void DogShouldBeAudited()
{
var entMeta = metas[typeof(Dog)];
entMeta.ClassMetas.OnlyContains<AuditedAttribute>();
}

[Test]
public void CatShouldBeAudited()
{
var entMeta = metas[typeof(Cat)];
entMeta.ClassMetas.OnlyContains<AuditedAttribute>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace NHibernate.Envers.Tests.NetSpecific.UnitTests.Fluent.Model
{
public abstract class Animal
public class Animal
{
private int weight;
}

public class Dog : Animal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public IFluentAudit<T> Exclude(Expression<Func<T, object>> func)
return this;
}

public IDictionary<MemberInfo, IEnumerable<Attribute>> Create()
public IDictionary<MemberInfo, IEnumerable<Attribute>> Create()
{
var ret = new Dictionary<MemberInfo, IEnumerable<Attribute>>();
addType(ret, typeof(T));
foreach (var ex in excluded)
{
Expand All @@ -33,13 +33,9 @@ public IDictionary<MemberInfo, IEnumerable<Attribute>> Create()
return ret;
}

private void addType(IDictionary<MemberInfo, IEnumerable<Attribute>> ret, System.Type type)
{
ret[type] = new List<Attribute> {new AuditedAttribute()};
if(!type.BaseType.Equals(typeof(object)))
{
addType(ret, type.BaseType);
}
}
private static void addType(IDictionary<MemberInfo, IEnumerable<Attribute>> ret, System.Type type)
{
ret[type] = new List<Attribute> {new AuditedAttribute()};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace NHibernate.Envers.Configuration.Fluent
{
public class FluentConfiguration : IMetaDataProvider
{
private static ILog log = LogManager.GetLogger(typeof (FluentConfiguration));
private readonly IDictionary<System.Type, IAttributeFactory> audits;
private static ILog log = LogManager.GetLogger(typeof (FluentConfiguration));
private readonly IDictionary<System.Type, IAttributeFactory> audits;

public FluentConfiguration()
{
Expand All @@ -16,10 +16,10 @@ public FluentConfiguration()

public IFluentAudit<T> Audit<T>()
{
var type = typeof (T);
var ret = new FluentAudit<T>();
audits[type] = ret;
return ret;
var type = typeof (T);
var ret = new FluentAudit<T>();
audits[type] = ret;
return ret;
}

public IDictionary<System.Type, IEntityMeta> CreateMetaData(Cfg.Configuration nhConfiguration)
Expand All @@ -37,8 +37,8 @@ public IFluentAudit<T> Audit<T>()
var entMeta = createOrGetEntityMeta(ret, classType);
foreach (var attribute in membAndAttrs.Value)
{
if(log.IsDebugEnabled)
log.Debug("Adding " + attribute.GetType().Name + " to type " + classType.FullName);
if(log.IsDebugEnabled)
log.Debug("Adding " + attribute.GetType().Name + " to type " + classType.FullName);
entMeta.AddClassMeta(attribute);
}
}
Expand All @@ -48,16 +48,37 @@ public IFluentAudit<T> Audit<T>()
var entMeta = createOrGetEntityMeta(ret, memberType);
foreach (var attribute in membAndAttrs.Value)
{
if (log.IsDebugEnabled)
log.Debug("Adding " + attribute.GetType().Name + " to type " + memberType.FullName);
if (log.IsDebugEnabled)
log.Debug("Adding " + attribute.GetType().Name + " to type " + memberType.FullName);
entMeta.AddMemberMeta(memberInfo, attribute);
}
}
}
}
addBaseTypesForAuditAttribute(ret);
return ret;
}

private void addBaseTypesForAuditAttribute(IDictionary<System.Type, IEntityMeta> ret)
{
foreach (var auditedType in audits.Keys)
{
setBaseTypeAsAudited(auditedType.BaseType, ret);
}
}

private static void setBaseTypeAsAudited(System.Type baseType, IDictionary<System.Type, IEntityMeta> ret)
{
if (!ret.ContainsKey(baseType) && !baseType.Equals(typeof(object)))
{
var eMeta = new EntityMeta();
eMeta.AddClassMeta(new AuditedAttribute());
ret[baseType] = eMeta;

setBaseTypeAsAudited(baseType.BaseType, ret);
}
}

private static EntityMeta createOrGetEntityMeta(IDictionary<System.Type, IEntityMeta> metas, System.Type type)
{
IEntityMeta ret;
Expand Down

0 comments on commit 32c0e4c

Please sign in to comment.