views:

786

answers:

4

With CSLA.net, all domain classes need to inherit from Businessbase, which contains non-virtual properties.

When using NHibernate, we need to implement virtual properties for lazy loading.

Some options to use CSLA/NHibernate together seem to be:

  • switch lazy loading off in NHibernate and implement lazy loading code in the domain classes (although this seems less flexible)
  • leave lazy loading on in NHibernate but use a DTO class to map to the database and then transfer the data to the CSLA domain classes

What other options might there be? Any pointers in the right direction will be much appreciated.

I suppose the above question is really applicable to using NHibernate with any framework.

+2  A: 

You can create interfaces for all of your mapped classes and specify that NHibernate should use that interface when creating the proxy. When you do this, your concrete domain class will not be used until the instance is initialized.

For example, you can do it in your hbm.xml like this:

<class name="DomainModel.Entity, DomainModel" table="Entities" proxy="DomainModel.Api.IEntity, DomainModel">
    ...
</class>

Note, however, that this puts a couple of limitations on how you can do your mappings. For example, you cannot use the access="field.*" access strategies. Check out this post on the two lazy loading strategies that can be used.

mookid8000
+1  A: 

You can try CSLA.Nhibernate out of box or can get some hints from the same.

CSLA.Nhibernate is part of CSLAContrib on codeplex. http://cslacontrib.codeplex.com/SourceControl/changeset/view/46985#302175

Not much activity since long. But whatever is implemented is working fine. SVN path : https://cslacontrib.svn.codeplex.com/svn/ProjectTrackerNHibernate

Sachin
A: 

You can specify 'lazy=false' in your NHibernate mapping at the class level. This will remove the need to have virtual properties, since NHibernate will then not use dynamic proxies. (this does not affect the laziness of collections).

<class name="SomeClass" lazy="false">
   <id .... />

   <set name="SomeSet" ... >
   </set>
</class>

In the above example, the class is mapped as 'lazy'. You will not need virtual properties, but the SomeSet collection can remain lazy loaded.

Frederik Gheysels
+1  A: 

I believe the only correct course of action is to create a DTO layer in between your business layer and your data access. I have done this on many projects and I have had a lot of success with it.

Please keep in mind that your business objects should not look/feel like your data layer. CSLA objects are your business layer and should be hydrated from your data access ORM layer within your DataPortal_XYZ methods.

Take for example, a simple example of a Users, Roles, and a UserRoles data table structure where UserRoles is a link table to link users with roles. This is your data schema and it is very good at normalizing your data.

Your business objects should NOT look like this as that is not normalizing the bahavior. When thinking about a User business object, that should have a property of RoleList which is a list of Role objects. There should NOT be a UserRole business object in existance. This would occur if you were trying to go directly from a database schema and CSLA objects.

Jamie Wright