2

I am running into an nHibernate error while saving an object.

The classes involved are:

interface IHardwareSpecification
{
   //fields and methods
} 

public class CPUSpecification : IHardwareSpecification
{
    //fields and methods
}    

public class SystemTransaction 
{       
    //Bunch of other fields

    private IHardwareSpecification _specs;
    public virtual IHardwareSpecification Specification 
    { 
        get { return _specs; }
        set { _specs = value;} 
    }
 }

Mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ...>
  <class name="SystemTransaction" table="SystemTransactions" lazy="false">
    <component access="field.camelcase-underscore" name="Specification"
               class="HardwareMarketplace.Model.CPUSpecification">
      <property access="field.camelcase-underscore" column="Specification_Rate"
                name="Rate"/>
         ...
    </component>
  </class>
</hibernate-mapping >

While persisting the object to database via Save, I get the following error:

Exception: Unable to cast object of type 'Castle.Proxies.IHardwareSpecificationProxy' to type 'Hardwaremarketplace.Model.SystemTransactions.CPUSpecification'.

I am trying to figure out how to resolve this so any help will be appreciated. f

abatishchev
  • 98,240
  • 88
  • 296
  • 433
fjxx
  • 945
  • 10
  • 20
  • @Diego CPUSpecification is a value object and thus has no mapping. I don't have a mapping for IHardwareSpecification. – fjxx Feb 04 '11 at 20:53
  • 1
    The only error I noticed so far is that the field is named "_specs", but the name in the mapping is "Specification". The field should be named "_specification" according to your access setting. Otherwise I cannot reproduce your problem. More info might help. – AlexD Feb 04 '11 at 21:06
  • Somehow "Specification" property is assigned an object of type "IHardwareSpecificationProxy" before you attempt to save the entity. This is the source of the problem. I don't see a reason for nHiberante to create that proxy. How do you initialize your entity before saving it? – AlexD Feb 04 '11 at 21:20
  • @AlexD A WCF webservice SystemTransactionDTO data contract is mapped to the SystemTransaction model entity using AutoMapper. – fjxx Feb 04 '11 at 21:28

1 Answers1

0

Based on your comment I understand that AutoMapper creates proxy type for interface property Specification. Thus you have:

public class CPUSpecification : IHardwareSpecification { }

and

public class IHardwareSpecificationProxy : IHardwareSpecification{ }

These are two incompatible types and IHardwareSpecificationProxy object cannot be converted to CPUSpecification.

What you need to do is to tell AutoMapper to use CPUSpecification class instead of dynamic proxy.

Edit: Considering you have CPUSpecificationDTO inside SystemTransactionDTO, you can achieve what you need with the following code:

Mapper.CreateMap<SystemTransactionDTO, SystemTransaction>();
Mapper.CreateMap<CPUSpecificationDTO, CPUSpecification>();
Mapper.CreateMap<CPUSpecificationDTO, IHardwareSpecification>()
    .ConvertUsing(dto => Mapper.Map<CPUSpecificationDTO, CPUSpecification>(dto));

And no need to change Specification property type to CPUSpecification:).

AlexD
  • 5,011
  • 2
  • 23
  • 34
  • Thanks - I wasn't aware of that. Are you suggesting that I change the automapper mapping Mapper.Map"CPUSpecificationDTO, IHardwareSpecification" to Mapper.Map"CPUSpecificationDTO, CPUSpecification" (which would also imply changing the Specification property of SystemTransaction class to type CPUSpecification) ? – fjxx Feb 04 '11 at 22:15
  • As you can see from my edited answer, you don't have to change Specification property to type CPUSpecification. Just tell AutoMapper to use conversion to CPUSpecification when converting to IHardwareSpecification. Did this advice help you to solve the problem? – AlexD Feb 05 '11 at 13:35
  • Yes I tried this method for mapping for converting from SystemTransaction to SystemTransactionDTO - I am getting an error saying that argument type IHardwareSpecification is not assignable to type CPUSpecification – fjxx Feb 07 '11 at 05:10