Using LINQPad with NHibernate and S#arp Architecture

(Update: Added the code to initialize Castle Windsor as well)

I've known about LINQPad for a long time but for some reason the usefulness of it just hit me yesterday.  In a moment of clarity I plopped my money down for all the bells and whistles and started playing.  I watched the video on the LINQPad site (you should too) and began thinking of the different ways it would most benefit my project.  Of course the first step would be getting LINQPad to use the my actual code. 

My current project is built on S#arp Architecture with a few modifications.  One of those modifications is the implementation of NHibernate.Linq in the repositories.  Using LINQPad to write snippets and query your database is cool but using it to query your NHibernate repositories is other-side-of-the-pillow (or Billy D. Williams) cool.  It's also very easy to set up.

I'm using the Northwind example that is included with S#harp Architecture with the addition of NHibernate.Linq to the SupplierRepository.  With LINQPad open hit F4 to open the Advanced Query Properties window and add the following additional references:

  • NHibernate.dll
  • NHibernate.Linq.dll
  • SharpArch.Core.dll
  • SharpArch.Data.dll
  • SharpArch.Web.Castle.dll
  • SharpArch.Web.dll
  • SharpArch.Testing.dll
  • FluentNHibernate.dll
  • log4net.dll
  • Northwind.Core.dll
  • Northwind.Data.dll
  • Northwind.Web.dll
  • Northwind.ApplicationServices.dll
  • Castle.Core.dll
  • Castle.Windsor.dll
  • CommonServiceLocator.WindsorAdapter.dll
  • Microsoft.Practices.ServiceLocation.dll
  • Castle.MicroKernel.dll

These are for a basic setup, you might need to add additional references for your project.

Next add these Additional Namespace Imports

  • NHibernate.Cfg
  • NHibernate.Linq
  • SharpArch.Testing.NHibernate
  • SharpArch.Data.NHibernate
  • log4net
  • log4net.Config
  • Northwind.Data
  • Northwind.Data.NHibernateMaps
  • Castle.Windsor
  • Microsoft.Practices.ServiceLocation
  • CommonServiceLocator.WindsorAdapter

Now all that's left is to snag some code from the Global.ascx file to configure log4net, initialize NHibernate, Castle Windsor and adjust the paths to the web.config, NHibernate.config and the assembly that contains your mapped entities.  Also note the change from WebSessionStorage to SimpleSessionStorage.


var Log = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType );
XmlConfigurator.Configure(new FileInfo(@"C:\src\Sharp-Architecture\src\NorthwindSample\app\Northwind.Web\Web.config"));
Log.Info( "Application Start" );
	
IWindsorContainer container = new WindsorContainer();
ComponentRegistrar.AddComponentsTo( container );
ServiceLocator.SetLocatorProvider( () => new WindsorServiceLocator( container ) );

NHibernateSession.Init(
	new SimpleSessionStorage(),
	new[] {@"C:\src\Sharp-Architecture\src\NorthwindSample\app\Northwind.Web\bin\Northwind.Data.dll"},
	new AutoPersistenceModelGenerator().Generate(),
	@"C:\src\Sharp-Architecture\src\NorthwindSample\app\Northwind.Web\NHibernate.config" );
	
var repo = ( ISupplierRepository )ServiceLocator.Current.GetService( typeof( ISupplierRepository ) );

var supplier = from s in repo
				where s.CompanyName.StartsWith("Bigfoot")
				select s;
				
supplier.Dump();

 

Now you can use LINQPad to quickly try new code utilizing your custom assemblies.

 

 


Posted 12-17-2009 4:05 PM by anortham

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)