CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

Spicing up our NHibernate Fluent Interface with pluggable conventions

I'm adding pluggable type conventions to our NHibernate mapping Fluent Interface today:

 

    public interface ITypeConvention
{
bool CanHandle(Type type);
void AlterMap(IProperty property);

}

public interface IProperty
{
void AddAlteration(Action action);
void UseThisType() where T : IUserType;
void SetAttributeOnPropertyElement(string name, string key);
}
 
Anyway, one of the design ideas we can steal from Ruby on Rails is the convention over configuration philosophy.  Part of the efficiency gain I think we make by moving from hbm.xml configuration to the FI is taking advantage of conventions tied to object types.  We've made the FI smart enough to respond to some class and property types.  The code above is going to make these conventions pluggable.  Today, I'm adding a custom IUserType for our Value Objects.  I'm creating the hook above to plug in a rule that says that any property subclassing type ValueObject will be mapped using this new IUserType.  
 
Here's some examples of conventions: 
  • You're trying to create a mapping for a class that subclasses DomainEntity?  Why don't I just automatically set up the identifier node for you pointing to the Id property.  I'll add property mappings for all those common audit trail columns too.
  • You got an enumeration type?  I'll just stick in an EntityStringType for you.
 
 
More results, less code, and fewer lines of XmHell. 

 




Comments

Will Shaver said:

I think you're missing some kind of end-code tag, the bottom half of your post is not wrapping correctly.

# June 25, 2008 5:23 PM

Jeremy D. Miller said:

@Will,

It's just too much effort to stop CS from eating my code.  It's trying to be helpful and reformat my C# code inside a <pre> node for me.

Sorry about the Courier New exposure.

# June 25, 2008 5:28 PM

Ayende Rahien said:

Jeremy,

Use an external tool, like WLW

# June 25, 2008 6:03 PM

Jeremy D. Miller said:

@Ayende,

I'm on Windows 2008 at work.  My normal PC with WLW is ill.

# June 25, 2008 6:07 PM

Mike Moore said:

FWIW, Brad Wilson had a nice post about installing the Live apps on Win2K8.

bradwilson.typepad.com/.../live-apps-on-wi.html

# June 25, 2008 7:06 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# June 26, 2008 8:51 AM

Morten Lyhr said:

So why dont you install WLW on work Win2008, thats what I did?

# June 26, 2008 9:09 AM

Tim Scott said:

This is really cool.  I am starting a new Nhib project and would love to escape XmlHell.  I took a quick look at the code.  I am trying to figure out what all ShadeTree.DomainModel does.  It looks like it also has some repository functionality.  I think I plan to use RhinoCommons as well.  Any idea if the two would conflict?  Is there any way to break the mapping tool into it's own assembly?  I'm happy to help with that it that's the way to go.

# June 26, 2008 11:54 AM

Jeremy D. Miller said:

@Tim,

The Repository in there is very naive at the moment, but we'll get it real soon.  Note the complete absence of the words "try" and "catch" in that code.

I just wanted a Repository off the bat that could be swapped in and out with an "In Memory" model for whitebox testing.

I don't think the two things would collide, and I'm not enthusiastic about creating more assemblies.  I am willing to change the code to decouple the FI from everything else if that's necessary.

And I'd love to have a volunteer for the NHib FI coding;)

# June 26, 2008 11:59 AM

Eduardo said:

I got curious about the IUserType and ValueOjbect thing. Is it kind of reusable domain difined type? I was thinking about that the other day.

What do your user type does? Label, validation, mask ...

# June 28, 2008 7:10 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP