2

I've installed NHibernate.3.3.2.4000 via Nuget into my MVC3 project, configured it without a proxyfactory.factory_class:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>         
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect, NHibernate</property>
        <property name="connection.connection_string_name">db</property>
        <property name="adonet.batch_size">50</property>
        <property name="current_session_context_class">web</property>
        <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
        <mapping assembly="TheWorkshop.DomainModel" />
    </session-factory>
</hibernate-configuration>

but like this I'm getting the following error:

The following types may not be used as proxies: TheWorkshop.DomainModel.Contact: method set_DateAdded should be 'public/protected virtual' or 'protected internal virtual'

Where the DateAdded property privately sets the DateAdded value

 public virtual DateTime DateAdded
 {
     get { return _dateAdded; }
     private set { _dateAdded = DateTime.Now; }
 }

I don't particularly want to make the setter public, the idea is that the DateAdded is automatically set.

I have seen posts that suggest I could set the proxyfactory.factory_class to

<property name="proxyfactory.factory_class">
    NHibernate.ByteCode.Castle.ProxyFactoryFactory, 
    NHibernate.ByteCode.Castle
</property>

but the Nuget install didn't bring down any references to NHibernate.ByteCode.Castle, when I tried to Install-Package NHibernate.Castle it failed with

Install failed. Rolling back...
Install-Package : Already referencing a newer version of 'NHibernate'.
At line:1 char:16
+ Install-Package <<<< NHibernate.Castle
+ CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

So I guess I could down version NHibernate, but that seems wrong... What should I do, a similar question NuGet: NHibernate, Castle.Core 3.0 and where is ProxyFactoryFactory? says I don't need to configure the proxyfactory.factory_class in newer versions

The last versions of NHibernate do not require configuring a proxy factory.
An internal one is used by default and the old adapters are not part of the project anymore.

4

1 に答える 1

4
  1. You don't necessarily need Castle.Core unless you use the Castle.Core proxy factory like you did in the second part. NHibernate comes with it's own proxy factory by default which is what you had the first time, so I won't comment on the Castle.Core part (and I don't think it works with latest nhibernate anyway)
  2. You can solve your error by telling nhibernate that you don't want to lazy load the class (which is enabled by default). How you do this will vary based on how you did your mapping.
  3. You can get rid of the error by making the setter protected instead of private.
  4. I don't think your setter is actually working like you think it is. unless you are actually setting that property with a value, it will never get the current date/time, in which case you might as well just set it with DateTime.Now rather than what you have. Also, when nhibernate loads that, you'll be getting the current date rather than the actual date/time it was created. You should be setting the value in the constructor, not hijacking the setter like you are.
于 2013-02-25T21:30:28.230 に答える