Hi,
I have an interface IRatingScale which is implemented by a concrete class RatingScale. My mapping file is defined in terms of RatingScale. I have a generic repository class GenericRepository<T> which implements IRepository<T>, which defines (among others) two methods as wrappers around ISession's Get<T> and CreateCriteria as follows:
Code:
public T GetById(IdT id)
{
return _sessionManager.OpenSession().Get<T>(id);
}
public ICollection<T> FindAll(params ICriterion[] criteria)
{
ICriteria crit = _sessionManager.OpenSession().CreateCriteria(typeof(T));
foreach (ICriterion criterion in criteria)
{
//allow some fancy antics like returning possible return
// or null to ignore the criteria
if (criterion == null)
continue;
crit.Add(criterion);
}
return crit.List<T>();
}
In a client class I have a IRepository defined as IRepository<IRatingScale> (mostly so I am able to unit test the client class) which is injected with a GenericReposity<IRatingScale>.
The issue I'm having is that the FindAll method is somehow able to resolve the fact that it needs to use the RatingScale mapping even tho T is defined as an IRatingScale, but Get<T> can't and throws a mapping exception.
My mapping file is below:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="OzoneHR.Domain"
namespace="OzoneHR.Domain.RatingScale">
<class name="RatingScale" table="RatingScale">
<id name="Id" access="nosetter.camelcase-underscore"
column="RatingScaleID" type="Int32">
<generator class="identity" />
</id>
<property name="RatingScaleName" column="RatingScaleName" type="String" not-null="true" access="nosetter.camelcase-underscore" />
<list name="Ratings"
table="Rating"
cascade="all"
access="nosetter.camelcase-underscore"
>
<key column="RatingScaleID" />
<index column="ScalePosition" type="Int32" />
<composite-element class="Rating">
<property name="Name" access="nosetter.camelcase-underscore" column="RatingName" not-null="true" />
<property name="ShortName" access="nosetter.camelcase-underscore" column="ShortName" not-null="true" />
<property name="Description" access="nosetter.camelcase-underscore" column="Description" not-null="true" />
</composite-element>
</list>
</class>
</hibernate-mapping>
I can't define my mapping in terms of class=IRating, as the Ratings list on RatingScale is defined as an internal protected property i.e. clients of the class shouldn't see it.
Any ideas on how I can workaround this problem?
Thanks,
Matt