I want to create a base class for some of my entities, since they all share a Event
list property.
I want also to make the Event
list a readonly property.
So I created a base EventRelatedEntity
class and then derived it on each of my entity class that has to do with Events.
Please also note that EventRelatedEntity
class has no NHibernate mapping class since it's not linked to a table.
See code below.
Base class:
public class EventRelatedEntity
{
private readonly List<Event> events;
public virtual IReadOnlyCollection<Event> Events { get; protected set; }
public EventRelatedEntity()
{
events = new List<Event>();
Events = events.AsReadOnly();
}
protected virtual void AddEvent<T>(T entity, string message)
{
if (events == null)
events = new List<Event>();
Event newEvent = new Event();
if (typeof(T) == typeof(Company))
{
newEvent.CompanyId = (entity as Company).Id;
// ...and do some other stuff...
}
else if (typeof(T) == typeof(Document))
{
newEvent.DocumentId = (entity as Document).Id;
// ...and do some other stuff...
}
else if (typeof(T) == typeof(Typology))
{
newEvent.TypologyId = (entity as Typology).Id;
// ...and do some other stuff...
}
newEvent.Message = message;
events.Add(newEvent);
}
}
Entity classes
public class Company : EventRelatedEntity
{
[Key]
public virtual int Id { get; protected set; }
[Required]
public virtual string Alias { get; set; }
[Required]
public virtual string CompanyName { get; set; }
// ...and some other properties...
#region Actions
public virtual void AddEvent(string message)
{
base.AddEvent(this, message);
}
#endregion
}
public class Document : EventRelatedEntity
{
[Key]
public override int Id { get; protected set; }
[Required]
public virtual User User { get; protected set; }
// ...and some other properties...
#region Actions
public virtual void AddEvent(string message)
{
base.AddEvent(this, message);
}
#endregion
}
// ...and some other classes...
Fluent NHibernate mapping classes for entities
public class CompanyMap : ClassMap<Company>
{
public CompanyMap()
{
Table("Companies");
LazyLoad();
Id(x => x.Id).GeneratedBy.Identity().Column("Id");
Map(x => x.Alias).Column("Alias").Not.Nullable();
Map(x => x.CompanyName).Column("CompanyName").Not.Nullable();
// ...and some other mappings...
// Link with Events table
HasMany(x => x.Events) // Events is declared in the base class (EventRelatedEntity)
.KeyColumn("CompanyId")
.Access.LowerCaseField()
.Cascade.AllDeleteOrphan();
}
}
public class DocumentMap : ClassMap<Document>
{
public DocumentMap()
{
Table("Documents");
LazyLoad();
Id(x => x.Id).GeneratedBy.Identity().Column("Id");
References(x => x.User).Column("UserId");
// ...and some other mappings...
// Link with Events table
HasMany(x => x.Events) // Events is declared in the base class (EventRelatedEntity)
.KeyColumn("DocumentId")
.Access.LowerCaseField()
.Cascade.AllDeleteOrphan();
}
}
// ...and some other mapping classes...
Finally I want to avoid direct access to the List<>.Add()
method. I want a readonly collection. The only way to add a new Event
to the Event list of an entity must be the AddEvent
method of the corresponding entity class.
Example:
Document document = session.Get<Document>(1);
// ...the same for other derived classes...
// I WANT TO AVOID THIS!
document.Events.Add(new Event());
// WANTS TO BE THE ONLY PERMITTED WAY TO ADD NEW EVENTS
document.AddEvent("My new event message");
The problem is that when I do the:
Document document = session.Get<Document>(1);
I get an error from NHibernate:
Cannot cast objects of type 'NHibernate.Collection.Generic.PersistentGenericBag'1 [SolutionDOC_Interface.Entity.Event]' to the 'System.Collections.Generic.List'1 [SolutionDOC_Interface.Entity.Event]' type.
I think it is related with the fact the EventRelatedEntity
class has no NHibernate mapping, but I can't provide a map, since it has not to do with a table in DB.
Maybe if I declare the Event list inside each class (Company, Document, etc.) without using inheritance, NHibernate will work, but this approach would produce quite a bit of code duplication that I want to avoid.
UPDATE 2017/10/18
After changing the code like @ryan suggested, now it works.
Revised code:
public class EventRelatedEntity
{
private readonly IList<Event> events;
public virtual IReadOnlyCollection<Event> Events { get; protected set; }
public EventRelatedEntity()
{
events = new List<Event>();
Events = (events as List<Event>).AsReadOnly();
}
// ...
}
Events
is indeed declared as anIReadOnlyCollection
, thendocument.Events.Add(new Event());
should be producing an error:'IReadOnlyCollection<Event>' does not contain a definition for 'Add'...
– ryan