# Wednesday, May 27, 2009

In part 2 of the NerdDinnerAdvanced code I replaced the LinqToSql DAL with a DAL that uses NHibernate.  NHibernate is an open sourced object relational mapper.  If you are not familiar with ORM and/or NHibernate I highly suggest you check them out.  Because of the way we architected our project in Part 1 implementing an NHibernate DAL had little to no impact on any code above the DAL.

To start I re-structured the project a little.  I created a NerdDinnerAdvanced.Data.LinqToSql project and moved the LinqToSql DinnerRepository and the NerdDinner.dbml file into the new project.  This left just the IDinnerRepository in the NerdDinnerAdvanced.Data project.  You could certainly move the IDinnerRepository class to the NerdDinnerAdvanced.Common project and remove the NerdDinnerAdvanced.Data project if you wish.


Now it was time to create my NHibernate mappings.  If you are not familiar with how ORM’s work the basic concept is that the ORM framework maps data from a database to your entity/domain objects In order to accomplish this you need to tell the ORM what tables/columns map to what objects/fields and this is typically done via an XML file.  In this case we only have 2 objects to map, Dinner and Rsvp, so the mapping would not be complex.  Often, though, you are dealing with hundreds of objects with a lot of fields and the management of the XML mapping files can get very difficult.  For this reason, and because I wanted to have a chance to play with it, I decided to use Fluent NHibernate to do my mapping.  Fluent NHibernate allows you to do your mappings using strongly typed C# code.

In my NerdDinnerAdvanced.Domain project I created a new folder called Mappings and added two mapping classes, one for each of my two domain objects.



Because this is not a Fluent NHibernate tutorial I am not going to go into detail on how you create the mappings…and for the most part it is fairly intuitive.

 /// <summary>
 /// Fluent NHibernate mapping for the Dinner class
 /// </summary>
 public class DinnerMap : ClassMap<Dinner> {
     public DinnerMap() {
         SetAttribute("lazy", "false");
         Id(x => x.DinnerId);
         Map(x => x.Title);
         Map(x => x.Description);
         Map(x => x.EventDate);
         Map(x => x.HostedBy);
         Map(x => x.Address);
         Map(x => x.Country);
         Map(x => x.ContactPhone);
         Map(x => x.Latitude);
         Map(x => x.Longitude);
         HasMany(x => x.Rsvps).Inverse().KeyColumnNames.Add("DinnerId").Cascade.All().SetAttribute("lazy", "false");

Next it was time to create the NHibernate DAL so I added a NerdDinnerAdvanced.Data.NHibernate project to the solution.  The first thing you have to do when using NHibernate, after creating your mapping files, is create your NHibernate Session class.  Session to NHibernate is like DataContext to Linq.   The Session for the most part contains connection string and mapping info and it is the mediator between you and your data.

    /// <summary>
    /// NHibernate session factory
    /// </summary>
    public class SessionFactory {

        public static ISessionFactory CreateSessionFactory() {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.Is("Data Source=keith-166938fd4;Initial Catalog=NerdDinner;Integrated Security=True")))  
                .Mappings(m =>  m.FluentMappings.AddFromAssemblyOf<Dinner>())  

After creating the NHibernate Session class I can now implement my DinnerRepository using NHibernate.  Below is an example of the FindAllDinners method.

        /// <summary>
/// Returns a list of all dinners
/// </summary>
/// <returns>List of all dinners</returns>
public IQueryable<Dinner> FindAllDinners() {
var sessionFactory = SessionFactory.CreateSessionFactory();
using (var session = sessionFactory.OpenSession()) {
using (session.BeginTransaction()) {
return session.CreateCriteria(typeof(Dinner)).List<Dinner>().AsQueryable();

After completing the DinnerRepository we can tell our application to use the the new DAL by changing one line in our InversionOfControlHelper class.

        /// <summary>
        /// Initializes the inversion of control mappings
        /// </summary>
        public static void Initialize() {
//Tell Structure Map we are using Fluent mapping rather than onfiguration file based mapping! StructureMapConfiguration.UseDefaultStructureMapConfigFile = false;
//This is the only line of code outside of the DAL that needed to be modified to implement the NHibernate data access layer! //StructureMapConfiguration.BuildInstancesOf<IDinnerRepository>().TheDefaultIsConcreteType<Data.LinqToSql.DinnerRepository>();

Once again the complete source can be downloaded here.  Stay tuned for Part 3!

Wednesday, November 17, 2010 1:12:18 AM (Central Standard Time, UTC-06:00)
cheap designer clothes are the main operation on the website they are welcome as popular the cheap ugg boots in winter. for the warm feeling in the winter, people like to pick the ugg boots on the professional online ugg boots clearance, and as well as these products, you also can buy the cheap mbt shoes from these online website for your need and as you like.
Friday, December 17, 2010 5:18:34 AM (Central Standard Time, UTC-06:00)
Louis Vuitton. travel is the theme

of this season, each model is ready to go, as if about to begin a

gorgeous travel. As for the men's design director chanel handbags Paul Helbers described from the

African continent's ugg boots vast

hereditary king of inspiration, we probably only from the antelope

leather or crocodile skin and ugg

materials to the pursuit of luxury seek traces of modern art and

practicality to be cut Integration in the most traditional dress

Friday, December 31, 2010 2:35:34 AM (Central Standard Time, UTC-06:00)
I like every post in this blog. Really a nice work has done. I appreciate the blog owner.send flowers to morocco
Tuesday, January 11, 2011 6:18:32 AM (Central Standard Time, UTC-06:00)
It appears that you've put a good amount of effort into your article and I want a lot more of these on the World Wide Web these days. I sincerely got a kick out of your post. I do not have a bunch to to say in response, I only wanted to register to say remarkable work.
Tuesday, January 11, 2011 11:20:46 PM (Central Standard Time, UTC-06:00)
Your work is very good and I appreciate you and hopping for some more informative posts.send flowers to dubai | send flowers to india from uk
Stefanie Mathew
Thursday, January 13, 2011 12:42:35 AM (Central Standard Time, UTC-06:00)
Giving someone all your love cheap ugg boots sale is never an assurance that they will love discount ugg boots you back! Do not wait for love in return,just wait for cheap ugg boots it to grow in their ugg boots heart but if does not,be content it grows in ugg boots sale yours.It takes only a minute to get a crush on someone,but it takes a http://www.goodfurboots.com/ lifetime to forget someone.
Sunday, January 23, 2011 11:26:29 PM (Central Standard Time, UTC-06:00)
Barbour jackets are very <b>belstaff townmaster jacket</b> fashionable in appearance. <b>belstaff town master motorcycle jacket</b> They are available in a number <b>belstaff tourmaster trophy jacket</b> of attractive designs and <b>belstaff tourmaster summer jacket</b> patterns. The Barbour quilted <b>belstaff thruxton jacket</b> jacket has rocked the men's <b>belstaff the departed jacket</b> apparel market for its style <b>belstaff textile jacket</b> and comfort factor. They <b>belstaff stoke jacket</b> are designed with both <b>belstaff snetterton jacket</b> conventional and traditional styles <b>belstaff siskin jacket</b> to meet the requirements of <b>belstaff silverstone jacket</b> every customer. Clothing <b>belstaff sammy miller replica jacket</b> manufacturers are updating the <b>belstaff sammy miller jacket</b> style and design of these <b>belstaff roadmaster motorcycle jacket</b> fashionable jackets.<p>There <b>belstaff rider blouson jacket black</b> are a number of positive <b>belstaff rider blouson jacket</b> features of this fashionable <b>belstaff redford jacket</b> men's clothing. It not only <b>belstaff rebel jacket</b> provides protection against <b>belstaff rallymaster jacket</b> Storm and dust, but also adds <b>belstaff raf jacket</b> style and class to your <b>belstaff racing blouson leather jacket</b> appearance, thereby making you <b>belstaff racing blouson jacket</b> look more sophisticated and <b>belstaff racemaster jacket</b> elegant.<p>There are <b>belstaff r6 jacket</b> different varieties of Barbour <b>belstaff preston jacket</b> jackets available in the market. <b>belstaff panther timeless jacket</b> Among them the Barbour <b>belstaff panther jacket medium</b> Washed International jacket, the <b>belstaff outlaw jacket</b> Barbour Carbon Jacket, <b>belstaff original 50 jacket</b> Barbour Chelsea Quilt jacket are <b>belstaff omega jacket</b> quite popular. The Barbour <b>belstaff olivers mojave vintage leather blouson jacket</b> washed international jacket <b>belstaff nylon jacket</b> is very stylish in <b>belstaff new tourist trophy jacket</b> appearance. They are created with a <b>belstaff new delta force jacket</b> number of pockets. It is also <b>belstaff motorcycle jacket for sale</b> designed with a very stylish
Monday, January 24, 2011 2:29:44 AM (Central Standard Time, UTC-06:00)
Herve Leger is a great way to attract people’s attention if you wear one Herve Leger Dresses. Herve Leger Dresses store has many styles, such as round-neck dresses, V-neck dresses, and off-neck dresses and so on. Belstaff only on the basis of Belstaff sale people. Belstaff Jackets to be the men's great choice.Because of its good quality and stylish appearance, Belstaff outlet are very popular. There are many people who want to buy gucci handbags, because there are many online shop cheap bags of gucci outlet online.Here is a gucci handbags outlet you can buy bags with reasonable price, but high quality.gucci outlet bags are so lovely and perfect person wearing high heels and shoes look amazing.
Wednesday, May 04, 2011 10:01:55 PM (Central Daylight Time, UTC-05:00)
<p align="left">KXPOS system is all-in-one touch point of sale system,it used in restaurant pos system ,cafes,bars,pubs,clubs,supermarkets,vegetable and fruit restaurant pos pos system sydeny pos system<br />
pos sysem hardware <br>
point of sale hardware<br>
pos system manufacturer <br>
all in one touch<br>
touch pos<br>
touch screen point of sale<br>
<strong>epos</strong> <strong>point of sale</strong></p>
<p align="left">&nbsp;</p>
Monday, December 03, 2018 4:44:10 PM (Central Standard Time, UTC-06:00)
Доступ к переписке Мессенджеров Взлом приложений Whatsapp Viber Telegram - как взломать вайбер мессенджер онлайн, взлом переписки вайбер
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview