Hi,
I have what should be a relatively simple issue but I am struggling to resolve it.
There are 2 tables in my data model: Group and Category. There is a one-to-many relationship....one Group can have many Categories, and a Category can be in one group.
I've got the same 2 classes in my domain model, standard stuff.
Now, I need to write some unit tests for a controller class which uses instances of Group and Category. But the key here is that they are unit tests so I would use a mocking framework (NMock2 in this case) to create mock instances of the domain objects.
So standard practice is to have your domain objects implement an interface, and use the interface to create the mock instances.
So I created an IGroup and ICategory. ICategory has an accessor for it's group. Now I'm working with interfaces the ICategory.Group returns an IGroup rather than a group:
Code:
public interface ICategory
{
int Id { get; set; }
string Name { get; set; }
IGroup Group { get; set; }
}
This is all fine and my unit tests with the mock instances of ICategory and IGroup all work fine.
HOWEVER....now I am unable to do any data access with the Category and Group objects...I get the following NHibernate error:
Code:
NHibernate.MappingException: An association from the table CATEGORY refers to an unmapped class: CLEDOM.Business.Domain.IGroup.
Here's my mapping file for Category:
Code:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="CLEDOM.Business">
<class name="CLEDOM.Business.Domain.Category" table="CATEGORY">
<cache usage="read-only"/>
<id name="Id" type="int" column="CategoryId">
<generator class="native" />
</id>
<property name="Name" type="string" column="Name" length="128" />
<many-to-one name="Group" column="GroupId" />
</class>
</hibernate-mapping>
I tried to find some info on this but didn't really find a good example of a domain model implemented using standard interfaces. I did find some stuff but it had all sorts of IoC and Castle Windsor in there which was way over the top for what I want. I just want to use simple interfaces!
I did find another sample which included a "proxy" attribute on the class element. So I tried this:
Code:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="CLEDOM.Business">
<class name="CLEDOM.Business.Domain.Category" proxy="CLEDOM.Business.Domain.ICategory" table="CATEGORY">
etc...
etc...
but that made no difference.
Can anybody help? Can my domain model be coded against interfaces?