I am running into the same issue and am trying to figure out how to actually cast the collection object.
All my classes Lazy Load. I retrieve an object that contains a collection from the database. I iterate through the collection and the objects are of INHibernateProxy of an Interface. I want to try to figure out what sub-class type they are of and cast accordingly.
Before, I was able to just check the type of the object:
if (persistedOption is FacilityFixedRateOption)
{
// do something
((FacilityFixedRateOption) persistedOption).FixedRate
}
but this never works. It doesn't match when I check the type, which is always INHibernateProxy.
I can get the correct type with this call:
Type type = NHibernateUtil.GetClass(persistedOption);
but I still have a problem casting. Below is how I figured out to cast. Is there a better way?
RateOptionGroup group = repository.RetrieveRateOptionGroup(groupId);
IFacilityRateOption persistedOption = group.Options[0] as IFacilityRateOption;
if (persistedOption is INHibernateProxy)
{
Type type = NHibernateUtil.GetClass(persistedOption);
persistedOption =
NHibernateProxyHelper.GetLazyInitializer((INHibernateProxy) persistedOption).GetImplementation() as
FacilityFixedRateOption;
}
Below are the mappings.
Thanks in advance,
Jamie
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Ais.Sentry.Domain.Securities.Loans.RateOptionGroup, Domain" table="rate_option_groups">
<id name="Id" column="id" type="Int32" unsaved-value="0" >
<generator class="identity" />
</id>
<property name="Name" column="name" type="String" not-null="true" />
<bag name="Options" table="rate_option_group_values" lazy="true" cascade="all">
<key column="group_id"/>
<many-to-many class="Ais.Sentry.Domain.Securities.Loans.IFacilityRateOption, Domain" column="rate_option_id" />
</bag>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Ais.Sentry.Domain.Securities.Loans.IFacilityRateOption, Domain" table="facility_rate_options">
<id name="Id" column="id" type="Int32" unsaved-value="0" >
<generator class="identity" />
</id>
<discriminator column="discriminator" type="String" />
<property name="Description" column="description" type="String"/>
<property name="PayConvention" column="pay_convention_id" type="Ais.Sentry.Domain.Calendars.PaymentConvention, Domain" not-null="true" />
<property name="IsAutoGenerated" column="is_auto_generated" type="Boolean" not-null="false"/>
<subclass name="Ais.Sentry.Domain.Securities.Loans.LoanRateOption, Domain" discriminator-value="LoanRateOption" >
<many-to-one name="facility" class="Ais.Sentry.Domain.Securities.Loans.CreditFacility, Domain" column="financial_security_id" access="field"/>
<subclass name="Ais.Sentry.Domain.Securities.Loans.FacilityFixedRateOption, Domain" discriminator-value="FixedRateOption" >
<property name="fixedRate" column="fixed_rate" type="Decimal(12,8)" access="field"/>
<many-to-one name="fixedRateSchedule" class="Ais.Sentry.Domain.Collections.RateSchedule, Domain" column="rate_schedule_id" cascade="all" access="field"/>
</subclass>
<subclass name="Ais.Sentry.Domain.Securities.Loans.FacilityFloatingRateOption, Domain" discriminator-value="FloatingRateOption" >
<property name="spread" column="spread" type="Decimal(12,8)" access="field"/>
<property name="initialCouponRate" column ="initial_coupon_rate" type="Decimal(12,8)" access="field"/>
</subclass>
</subclass>
</class>
</hibernate-mapping>