Humble Blog

Monday, August 29, 2011

First of all, we will introduce a repository interface to implement the merge functionality that is glaringly missing from Entity Framework. I decided to name this repository ToTheEfnhX.

What ToTheEfnhX is not:

It's not a Unit of Work component

It doesn't compete with S#arp, S#arp is tied to NHibernate

What ToTheEfnhX is:

It's a repository interface

It's a repository interface that supports both Entity Framework and NHibernate

On this topic, we will discuss how to save to whole object graph using Entity Framework via ToTheEfnhX component. The API is patterned after NHibernate session.Merge, this function can save and update the whole object graph to database.

PM> install-package EntityFramework
You are downloading EntityFramework from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=224682. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'EntityFramework 4.1.10715.0'.
Successfully added 'EntityFramework 4.1.10715.0' to TheService.

PM> install-package FluentNHibernate
'NHibernate (≥ 3.2.0.4000)' not installed. Attempting to retrieve dependency from source...
Done.
'Iesi.Collections (≥ 3.2.0.4000)' not installed. Attempting to retrieve dependency from source...
Done.
Successfully installed 'Iesi.Collections 3.2.0.4000'.
Successfully installed 'NHibernate 3.2.0.4000'.
You are downloading FluentNHibernate from James Gregory and contributors, the license agreement to which is available at http://github.com/jagregory/fluent-nhibernate/raw/master/LICENSE.txt. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'FluentNHibernate 1.3.0.717'.
Successfully added 'Iesi.Collections 3.2.0.4000' to TheService.
Successfully added 'NHibernate 3.2.0.4000' to TheService.
Successfully added 'FluentNHibernate 1.3.0.717' to TheService.

On your front-end(Winforms app), add Service Reference to TheService, name it UseService

Then configure your Service Reference:

Add BindingSource to your form, then name it to bdsQuestion, on your bdsQuestion Properties, click DataSource, then point it to UseService.Question:

Your bdsQuestion's DataSource should look like this:

Add another binding source to your form, name it bdsQuestionComment, point its DataSource to bdsQuestion, and its DataMember to Comments.

It should look like this:

Add another binding source to your form, name it bdsAnswer, point its DataSource to bdsQuestion, and its DataMember to Answers.

It should look like this:

Add another binding source to your form, name it bdsAnswerComment, point its DataSource to bdsAnswer, and its DataMember to Comments.

It should look like this:

Add these controls, and set the controls' property's BindingSource accordingly

Question's Text

Question's Poster

Question's Comments

Question's Answers' Text

Question's Answers' Poster

Question's Answers' Comments

Answer's BindingNavigator

Copy the following to your Form1.cs, and bind the events to their corresponding controls(I think this is where VB.NET trumps C#, VB.NET has declarative way of binding events to objects(via Handles keyword), VB.NET event has more one-stop-shop feel into it, anyway I still love C# :-)

Now let's try to delete the first answer(its corresponding comments will be deleted too), and let's try to modify the second's answer's text, and add another comment to second's answer. This is where ToTheEfnhX dutifully do its job, this Merge functionality is glaringly missing in Entity Framework. All changes (delete,update,insert) to the entities can be merged back to database.