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.
Read the full post ... (822 words, 2 images, estimated 3:17 mins reading time)
.NET, .NET FAQ, ADO.NET, C#, Code Snippets, Entity Framework, NHibernate
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
Read the full post ... (2487 words, 3 images, estimated 9:57 mins reading time)
.NET, .NET FAQ, ADO.NET, C#, CodeProject, LINQ
The Useless Introduction You Used To :)
This post has taken so long to be started in writing. I’ve been busy with many events in my life lately. Suffering from frequent limited internet access lately, and, all this moving between companies thing has been eating me. And yes, I admit, I’ have been as tired and more honestly lazy as you expected me to be!
Hey, there’s a little warning. This post is not exactly for my usual audience. I’m sorry, but introducing Domain Driven Design is not one of the goals for this post. There’re many interesting resources and books (even FREE: InfoQ, Domain Driven Design Quickly) on the topic. However, if you leave me a comment telling me to make a write-up on the topic, of course I will :) :).
One more thing. Another reason I’m working on this is that I’m preparing for an internal session here in Raya about Practical Lightweight Domain Driven Design. This session is truly internal yet. It should be recorded though but I’m not sure whether it’ll be possible to publish the videos (Yeah, I will see how we can have our public sessions of possible sure!). If you have a user group and would like me to give this session in a group meeting, I’ll be glad to do.
Read the full post ... (1517 words, 2 images, estimated 6:04 mins reading time)
.NET, ADO.NET, Agile, ALT.NET, Architecture, Domain Driven Design, Patterns, RAYA
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:
Read the full post ... (287 words, 2 images, estimated 1:09 mins reading time)
ADO.NET, ASP.NET, C#, F#, Link List