Using POCO and Fluent NHibernate, I would like to have the ability to take an object like User and be able to give it various properties. I would like these objects to be able to focus on their properties, not common properties. Currently, I have an AuditableEntity that I can derive from to give my entity the ability to have Audited properties like CreatedDateTime and ModifiedDateTime, but I would also like to have the ability to take out the need to implement my Id for every object, Id’s should be able to be part of some other base object, so I could say this object has a Guid Id and this one has an int id and this one has no Id. Currently, my User object looks like this:
User.cs
namespace ZeroBase.Domain.Entities
{
public class User : AuditableEntity<User>
{
public virtual Guid Id { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string EmailAddress { get; set; }
public virtual IEnumerable<Comment> Comments { get; set; }
}
}
and my UserMap looks like this:
UserMap.cs
namespace ZeroBase.Infrastructure.Data
{
public class UserMap : AuditMap<User>
{
public UserMap()
{
Id(x => x.Id)
.Column("Id")
.GeneratedBy.Guid();
Map(x => x.Username);
Map(x => x.Password);
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.EmailAddress);
HasMany(x => x.Comments);
Table("Users");
}
}
}
Is there anyway to handle the Id creation in some sort of a generic base class and still have a separate class that objects can inherit from to become auditable?
Are you using
AuditableEntityfor classes without an id?Entityimplies that the class has an identity. Based on this, I would say that you can add a Id property to AuditableEntity.If you are using it for classes without an identity, like value objects, I would create another base class. Would something like this make sense?
Note: Entity, value objects etc. are terms of Domain Driven Design (DDD).