Good afternoon.
Before I begin my explanation, I have had a look at other similar questions but the subtle differences (mainly in purpose of design) mean that the solutions provided in these answers to not apply to me.
I am attempting to create a 'Base data access library' for use with future projects so that I do not have to spend my time coding the same logic across multiple projects. The intention is to simply import this library and instantly have the ability to inherit from a IBaseDao and instantly have standard 'CRUD' abilities from the get-go.
Because (when coding this base library) I have no way of knowing exactly what my business objects will be (Customer, FileStructure, Cat, Dog, Order ect) I have defined an interface within my base library which all business objects must implement before they can be used with this library. Such an interface is defined as follows:
public interface IEntity
{
/// Indicates weather this entity is used as test
/// data or if it is a real-world entity.
bool IsMockObject { get; set; }
/// This is not intended for use in a 'real' application
/// and is only used in testing.
string ReadableName { get; set; }
/// The unique identifier for this object.
Guid Id { get; set; }
}
So now in the 'real' application I intend to have something similar to the following:
public class Customer : IEntity
{
public Customer()
{
}
string name = "";
public virtual String Name
{
get
{
return name;
}
set
{
name = value;
}
}
private DateTime birthday = DateTime.Now;
public virtual DateTime Birthday
{
get;
set;
}
private List<Order> orders = new List<Order>();
public virtual List<Order> Orders
{
get
{
return orders;
}
set
{
orders = value;
}
}
#region IEntity Members
private bool isMockObject;
public virtual bool IsMockObject
{
get
{
return true;
}
set
{
isMockObject = value;
}
}
private string readableName;
public virtual string ReadableName
{
get
{
return readableName;
}
set
{
readableName = value;
}
}
private Guid id;
public virtual Guid Id
{
get
{
return id;
}
set
{
id = value;
}
}
#endregion
}
Now here is where the issue arises. During my unit tests, when i try and save a 'Customer' I get a parameter count mismatch exception. I suspect the cause of this issue is that the Customer table within my database does not contain columns for all the IEntity properties. It does however contain an Id column. So far, my CustomerMap looks like this:
public class CustomerMap : ClassMap<Customer>
{
public CustomerMap()
{
Table("Customer");
// From IEntity
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Birthday);
// From IEntity
Map(x => x.IsMockObject);
// From IEntity
Map(x => x.ReadableName);
HasMany(x => x.Orders);
}
}
What I actually want NHibernate to do when saving is save in the Customer table, all the properties unique to Customer
as well as the IEntity.Id
property then save in a seperate table called (if IsMockEntity is true) MockEntitiy the Id of the mock object and its readable name.
I find ClassMap's pretty difficult to understand at the moment as I have done very little with NHibernate past basic persistence. Any help or links to relevant material greatly appreciated.
Thank you for your time.