Well,
Actually I have n classes inherited from AuditableEntity<Obj>, each
one has it's mapping class inherited from AuditableEntityMap<Obj> (see
bellow) an each one will have a subclass from the generic
AudityRecord<Obj>. In my framework when I load the configuration I get
a delegate for a static method in each AuditableEntityMap<Obj> that
loads it's AuditableRecord<Obj> mapping (confusing, huh?!, see above)
// The delegate passed to the AudityRecordMap (parent class)
internal delegate void MapAudityRecordDelegate(DiscriminatorPart
discriminator);
// The base AuditableEntity<Obj> mapping class
public class AuditableEntityMap<Obj> : EntityMap<Obj, long>
where Obj : AuditableEntity<Obj>, new()
{
private const string HistoryFilterCondition = "Id =
IdCurrent";
private const string DeletedFilterCondition = "Deleted = 0";
protected AuditableEntityMap()
{
base.Map(x => x.Deleted, "Deleted");
base.References<Obj>(x => x.Current, "Current").LazyLoad
().FetchType.Select();
base.AddPart(new FilterPart
(ContextFactory.HistoryFilterName,
ContextFactory.HistoryFilterCondition));
base.AddPart(new FilterPart
(ContextFactory.DeletedFilterName,
ContextFactory.DeletedFilterCondition));
}
private static void MapAudityRecord(DiscriminatorPart
discriminator)
{
discriminator.SubClass<AudityRecord<Obj>>(typeof
(Obj).Name, x =>
{
x.References<Obj>(y => y.Current, "Current");
x.References<Obj>(y => y.New, "New");
x.References<Obj>(y => y.Old, "Old");
});
}
internal static MapAudityRecordDelegate AudityRecordDelegate
{
get { return new MapAudityRecordDelegate
(AuditableEntityMap<Obj>.MapAudityRecord); }
}
}
// Some code from my "framework" configuration loader:
foreach (Type entityMap in
dataCtxMap.EntityMaps)
{
if (entityMap.IsGenericFrom(typeof
(AuditableEntityMap<>)))
{
auditableMappings.Add
((MapAudityRecordDelegate)entityMap.
GetProperty
("AudityRecordDelegate").GetValue(null, new object[0]));
}
cfg.Mappings(x => x.FluentMappings.Add
(entityMap));
}
if (auditableMappings.Count > 0)
{
cfg.Mappings(x => x.FluentMappings.Add(new
AudityRecordMap(auditableMappings.ToArray())));
}
// The AudityRecordMap class
internal class AudityRecordMap : EntityMap<AudityRecord, long>
{
public AudityRecordMap(MapAudityRecordDelegate[]
subClassesMap)
{
base.WithTable("AudityRecords");
base.Map(x => x.Operation,
"Operation").CustomTypeIs<GenericEnum<AudityOperation, char>>();
DiscriminatorPart discriminator =
base.DiscriminateSubClassesOnColumn<string>("Class");
foreach (MapAudityRecordDelegate subClasseMap in
subClassesMap)
{
subClasseMap(discriminator);
}
}
}
Actully this line doesn't works because it's expect the Type (witch
implements IMappingProvider):
cfg.Mappings(x => x.FluentMappings.Add(new
AudityRecordMap(auditableMappings.ToArray())));
So I added the following overload:
/// <summary>
/// Adds a single <see cref="IClassMap" /> represented by it's
instance.
/// </summary>
/// <param name="mapping">The mapping obj.</param>
/// <returns>Fluent mappings configuration</returns>
public FluentMappingsContainer Add(IMappingProvider mapping)
{
if (mapping == null)
throw new ArgumentNullException("mapping");
model.Add(mapping);
WasUsed = true;
return this;
}
Best regards,
DM
On Jul 28, 10:41 am, James Gregory <[email protected]> wrote:
> +1
> I prefer it when people show us problems, then *we *can provide the
> solution. Providing solutions doesn't help us understand if there's a
> greater issue we need to consider.
>
>
>
> On Tue, Jul 28, 2009 at 2:36 PM, Paul Batum <[email protected]> wrote:
> > It would be easier to follow what you are suggesting if you could provide
> > some code.
>
> > On Mon, Jul 27, 2009 at 9:07 AM, Mira.D <[email protected]> wrote:
>
> >> I'm really new to Fluent NHibernate, but I'm really enjoying this.
> >> It took care of all my major problems with dynamic mappings.
> >> I'm not sure if here is the right place to post my issue but, let's
> >> go:
>
> >> I have some "core components" that take care of entities auditing,
> >> so I have an AudityRecord class and AudityRecord<T> subclass that
> >> identifies each auditable entity recordinfo. In my program loading I
> >> had to add the AudityRecordMap and put some parameters in the
> >> constructor (an array with all auditable entities types). I've seen
> >> that calling FluentMappings.Add I have to pass the class type so it
> >> will sent to the PersistentModel, witch will instantiate the object,
> >> etc... The PersistentModel already has exposed a method to receive the
> >> IMappingProvider object so I just exposed a new method in the
> >> FluentMappingsContainer to receive the already instantiated
> >> AudityRecordMap with the needed array of types to map all of it's
> >> dynamic subclasses.
>
> >> That would be nice if it becomes part of the project so I won't need
> >> to be "fixing" it every new version downloaded.
>
> >> Best regards,
> >> DM
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Fluent NHibernate" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---