In NHibernate there is a Save(entityObject) method, which creates a new row in the database with the given entity object, also, has an Update(entityObject) which updates the row corresponding to the entity object with the property values of this object. It also has a SaveOrUpdate(entityObject) method, which checks the whether the entity object corresponds to an existing row in the database, and chooses whether to call Save(…) or Update(…) based on that.

The way I usually do web applications across multiple tiers, when not using view models specifically, makes me encapsulate much code in Services layer that sometimes does not need to care about whether the given entity is persisted in database or not. Thus wanted to have similar method using Entity Framework as ORM.

Of course I have implemented the method number of times and the code evolved based on which version of Entity Framework I’m coding against, and my knowledge of the framework internals as well.  Actually, when you work with so many ORMs like I did, a new ORM or ORM version turns to only sound like “What’s new in the manual?” thing.

, , , , , ,

If you don’t know what eager loading is, Jump to “What’s eager loading?”.

Eager Loading Syntax

If you are eager loading Products for example in a typical (Categories 1<->* Products) relation, the standard syntax would like:

DbDataContext.Categories.Include(“Products”)

What is the problem with that?

The “Products” part. The word “Products” is a string. If I rename the Products table to ShopProducts or whatever or even remove it from this data diagram and have it elsewhere, or even something wrong happens and the relation is removed from DB/diagram by mistake, my code will still compile, but will fail when it runs. This is BAD BAD BAD.

How to solve this?

Since I always believe that if something exists somewhere you shouldn’t do it yourself unless its totally broken (and I mean REALLY REALLY BROKEN), I started searching inside the Entity Framework itself for something to get the entity name from.

At first it seemed super easy. Every entity class has a static property “EntityKeyPropertyName”, so, I thought I can write something like:

DbDataContext.Categories.Include(Product.EntityKeyPropertyName); // But this didn’t work

, , , , ,

Slideshare is quickly becoming the defacto standard for sharing presentation slides, just as YouTube for videos, and Flickr for images. I recently got into the habit to share my presentations there and use the embed feature to include it in my weblog, and this was the same for the "Design Patterns Via C# 3.0" session.

This morning I got this email from SlideShare

Hey Mohamed_Meligy!
Your slideshow
Design Patterns Via C# 3.0 has been featured on the SlideShare homepage by our editorial team.
Cheers,
- the SlideShare team

WOW .. I couldn’t believe it until I went to SlideShare.net and saw it myself …

Featured Homepage

 

Thank you SlideShare. I never expected the slides to be interesting to that extent :D :D :D.

, , ,
Design Patterns Via C# 3.0
View SlideShare presentation or Upload your own. (tags: design patterns)

Yesterday was my 2 part session about Design Patterns as part of dotNETwork 7th gathering. Thank you all guys for being there, There was so much interesting stuff about the audience. The conversation we all had even before the session starts, the interaction with all parts of the session, and the great questions.

Thank you all.

 

You can find the slides for the 2 parts combined in single downloadable file.

Download the Presentation Slides

For the code examples/demos, you can find them in single ZIP file as well.

Download the Code Samples

 

I hope you enjoyed the session.

 
Related Links
, , , , , ,

dotNETwork, the most active offline user group in Egypt is having its 7th gathering next Saturday, August 30, which will have two parallel tracks for the first time in the group gathering. BizTalk Introduction, and C# 3.0 Design Patterns, which I will be delivering!

Facebook Event

If I were you to attend the event, it would have been a hard choice too (unless you go simply for BizTalk) ! BizTalk beginner introduction as a topic was not delivered in public sessions before. The only BiTalk sessions I know of were either advanced ones or introducing newer version to those familiar with old version. If you are my friend or you are pretty much into patterns, you may want to give what I have to say a look.

The session parts were primarily intended to be in a couple of dotNETwork gatherings. Now that dotNETwork decided to try out the parallel tracks model having two sessions in the same topic in each track (which is a decision I really like, except that I’d love more distinction between the topics), it was logical to have them in the same gathering / track. I have been thinking whether this should go for the simplest level possible ever, then, decided to stick to the original plan, and even use the long time available for the presentation (1:30h for each part) to go say more about related topics, as I have so much interest in delivering this in certain way for long time now.

, , , , ,

· Everything You Wanted To Know About MVC and MVP But Were Afraid To Ask

· Functional Programming in C# 3.0: How Map/Reduce/Filter can Rock your World

· MbUnit 2.4 – Avoiding Crappy Data Resulted From The Test

· Managed Extensibility Framework

· IronRuby at Tech Ed 2008

· Velocity: A Distributed In-Memory Cache from Microsoft

· Must See WF/WCF Influencer MSDN Webcasts

· WCF Webcast Series (Old but good)

· Using CSS to Fix Anything: 20+ Common Bugs and Fixes (Found by Mohamed Tayseer)

· A jQuery Client Status Bar

· jQuery for Asp.net MVC preview 3

· Hands on Labs for Web Client and Smart Client (Web Client Software Factory, Smart Client Software Factory)

· Code Style Review Using Microsoft Source Analysis

· Did you know… You can use Tracepoints to log “printf” or “console.writeline” info without editing your code? – #237

· SOA: Making The Paradigm Shift Part 9 of N

· Functional C# – Learn from F# and LINQ

, , , ,

It all started with an email Mohamed Hossam (AKA, Bashmohandes) sent to SilverKey Tech. (the company I work for) local office here in Egypt, referring to the article "Foundations of Functional Programming – Part 1 – B# .NET Blog".

It inspired me to send few more language links:

I also referred to the latest version of DLR hosting spec., stating that it’s quickly changing and already not up to date still.

Besides, I included some more reading bonuses:

, , , ,

The "C# 3.0 in a nutshell" book has some neat free extras that are worth mentioning for those who haven’t already heard of (they have been released for long). Those are like must-have LINQ tools and helpers.

The homepage of LINQKit (the major part of the extras I’m going to cover here) provides great information and short code samples about the components:

  • LINQPad

    This is a snippet compiler (application to run/try small codes in separation than big VS projects, like this), that’s customized for LINQ queries. You can use the SQL Server Query Analyzer -like application to try your codes on the tables in the DB, and see the resultant output and SQL query. Very useful for learning LINQ and/for for making up complex LINQ Queries

,
Note:
This is ported from my old weblog. Originally published April 07, 2005

There was a question in Microsoft Forums regarding the new modifier in C# and what’s the difference between using it and using the virtual and override modifiers. I wanted to share that here as well.

The new modifier is mainly used for hiding the non virtual methods. Unlike override modifier, it’s used to hide all class members not only methods (i.e. variables and properties). If you create a Base class with a protected, internal or public int for example, and inherit that Base in another class called Child, placing another x in it and try to compile this, the code will compile successfully! However, if you look at the tasks window (assuming that you are using VC#) you’ll see a warning that you should use the new keyword with the second declaration. A simple code should tell you what I mean here:

using System;

namespace ConsoleView

{

      public class Base

      {

            ///

The original member

            public int x;

      }

      public class Child:Base

      {     
           
//

This declaration will compile well, but,

            //will generate the following warning :

            //%CodeFilePAth%(%LineNumber%):

            //The keyword new is required on ‘ConsoleView.Child.x’

            //because it hides inherited member ‘ConsoleView.Base.x’

            public int x;
            }
}

,