Sunday, July 29, 2007

My Presentation at OKCodeCamp - DDD with NHibernate

I presented a talk on Domain Driven Design with NHibernate at the OKC code camp yesterday.

It started out a bit rough because I had some missing references and some confusing exceptions, however once I got the ball rolling I think it went pretty well.

I ran way short on time, however, and I promised the crowd that I would post the completed files on my blog tonight .

So, here is the powerpoint, demo applications, and all of the referenced dlls from my presentation.  Let me hear your feedback in the comments!

File Attachment: ddd with nhibernate.zip (7694 KB)

(the file is large because I have the complete references to specific versions of NHibernate and NUnit, so you shouldn’t have to worry about references at all.)

To get the sample running, change the app.config to point to an actual database.  The database should be empty.

Sunday, July 29, 2007 11:31:44 PM (Central Standard Time, UTC-06:00)
cheers!
Monday, July 30, 2007 2:28:09 PM (Central Standard Time, UTC-06:00)
I thought it was a great presentation. What was the software that you mentioned to help with creating the NHibernate mapping files?
Monday, July 30, 2007 8:59:25 PM (Central Standard Time, UTC-06:00)
It's called ActiveWriter and it generates the ActiveRecord model objects for use with that pattern. There are probably some more tools out there, but I recommend just digging in so that you really understand what's going on when you choose different settings in the mapping. Generating the schema directly from the mapping is a great way to do this.

Here's the link to the ActiveWriter project:
http://www.altinoren.com/activewriter/
Tuesday, September 25, 2007 1:17:08 AM (Central Standard Time, UTC-06:00)
Hi Ben, can you share the database files that are use in your demo? It will be handy. I am picking up nhibernate.
Chua Wen Ching
Tuesday, September 25, 2007 6:00:34 AM (Central Standard Time, UTC-06:00)
The database will get created automatically. Read the last line of the post.
Tuesday, September 25, 2007 11:41:54 PM (Central Standard Time, UTC-06:00)
Oh that simple. Alright will give it a try. Thanks.
Chua Wen Ching
Wednesday, September 26, 2007 2:21:38 AM (Central Standard Time, UTC-06:00)
Hi, I had a problem here.

I changed the app.config file as below:

<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.connection.connection_string" value="Server=WENCHINGPC\SQL2005;initial catalog=NHibernateDemo;Integrated Security=SSPI" />

So I created the NHibernateDemo database in SQL Server 2005 Developer Edition.

Then I try to run the HelloWorld application, I got this error.

could not load an entity: [HelloWorld.Message#7][SQL: SELECT message0_.Id as Id0_1_, message0_.Text as Text0_1_, message0_.nextMessage_id as nextMess3_0_1_, message1_.Id as Id0_0_, message1_.Text as Text0_0_, message1_.nextMessage_id as nextMess3_0_0_ FROM Messages message0_ left outer join Messages message1_ on message0_.nextMessage_id=message1_.Id WHERE message0_.Id=?]

I also tried on removing that NHibernateDemo database. No difference though.

Any help :)

Thanks.
Chua Wen Ching
Wednesday, September 26, 2007 5:57:42 AM (Central Standard Time, UTC-06:00)
It looks like the database was built successfully, can you paste the inner exception? That should tell us what's going wrong.
Wednesday, September 26, 2007 6:54:16 PM (Central Standard Time, UTC-06:00)
InnerException (from debug) - {"Invalid object name 'Messages'."}

The full details of exception:

NHibernate.ADOException was unhandled
Message="could not load an entity: [HelloWorld.Message#7][SQL: SELECT message0_.Id as Id0_1_, message0_.Text as Text0_1_, message0_.nextMessage_id as nextMess3_0_1_, message1_.Id as Id0_0_, message1_.Text as Text0_0_, message1_.nextMessage_id as nextMess3_0_0_ FROM Messages message0_ left outer join Messages message1_ on message0_.nextMessage_id=message1_.Id WHERE message0_.Id=?]"
Source="NHibernate"
StackTrace:
at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, Type optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId)
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session)
at NHibernate.Impl.SessionImpl.DoLoad(Type theClass, Object id, Object optionalObject, LockMode lockMode, Boolean checkDeleted)
at NHibernate.Impl.SessionImpl.DoLoadByClass(Type clazz, Object id, Boolean checkDeleted, Boolean allowProxyCreation)
at NHibernate.Impl.SessionImpl.Get(Type clazz, Object id)
at NHibernate.Impl.SessionImpl.Get[T](Object id)
at HelloWorld.Program.Main(String[] args) in C:\Documents and Settings\Administrator\Desktop\NHiibernate\Sept 17\ddd_20with_20nhibernate\ddd with nhibernate\demos\01.hello.world\HelloWorld\Program.cs:line 32
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Chua Wen Ching
Wednesday, September 26, 2007 7:20:28 PM (Central Standard Time, UTC-06:00)
Can you look in the setup method of one of those tests... the one where it uses the SchemaExport object to build the database. Run that line using the debugger, then click stop.

You should be able to see the database if the connection string is pointing to a valid sql 2000/2005 server.

Do you have a database? if so, please list the tables and columns here.

Wednesday, September 26, 2007 7:50:49 PM (Central Standard Time, UTC-06:00)
Thanks for the response.

You mention earlier "The database will get created automatically. Read the last line of the post.". Yes I have a database called NHibernateDemo, but it is empty. Do I have to creates tables manually? Maybe I didn't get your meaning right. Can you clarify?

Take note, I am looking at the HelloWorld demo. I don't see any test? I cannot see any SchemaExport from the HelloWorld example. Hmm.. am i looking at the right file? Coz I downloaded it from the link attached in this email.

From this code from HelloWorld,

using(ISession session = _factory.OpenSession())
{
Message msg = session.Get<Message>(7);
DisplayMessage(msg);

msg.NextMessage = new Message("I'm attached.");


session.Flush();
}

I debug on the _factory.OpenSession(), and when i step in.. I can see that the session is on the right SQL Server 2005 database and the state is open.

Thanks :)
Chua Wen Ching
Wednesday, September 26, 2007 7:57:25 PM (Central Standard Time, UTC-06:00)
I thought I had built that database. I think you're right. I'm sorry!

Create a table called Messages with the following columns:

ID int (PK, Identity)
Text varchar(255) not null,
nextMessage_id int null

That should work.
Wednesday, September 26, 2007 8:09:56 PM (Central Standard Time, UTC-06:00)
Thanks for your reply. Appreciate this. Cool I will work on that Database for HelloWorld now.

How about the other examples like DDD, Blog and Restraurant Systems?

How do I create those database tables too? Is it alright if you can generate some script so I can download it? Easier :P

Any help?
Chua Wen Ching
Comments are closed.
Vegas Hotel - Loans - United Specialties - Credit Card Consolidation