Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,125 | Comments: 45,492

filter by tags archive

ReviewMicrosoft N Layer App Sample, part VIII–CRUD is so 90s

time to read 2 min | 236 words

Continuing my review of http://microsoftnlayerapp.codeplex.com/, an Official Guidance (shows up at: http://msdn.microsoft.com/es-es/architecture/en) which people are expected to read and follow.

Take a look at the following:


I was curious about that, I didn’t really understand what is going on here, so I tracked it down a bit…


Skipping over the part where there is a cast of null, this seems to indicate that this is a call to modify the entity to simulate concurrency, digging deeper:


Seems like I was right, it is intended to force the save of this item to the database. That seemed very strange, so I checked upward, where this is used, and I found:


So, ChangeBankAccount is just another way of saying Save(), it seems, in a very round about way.

More posts in "Review" series:

  1. (03 Dec 2013) Getting started with LevelDB
  2. (20 Jul 2011) Microsoft N Layer App Sample, part X–Architecture for the Space Age
  3. (15 Jul 2011) Microsoft N Layer App Sample, part IX–Not Invented Here is FUN
  4. (13 Jul 2011) Microsoft N Layer App Sample, part VIII–CRUD is so 90s
  5. (11 Jul 2011) Microsoft N Layer App Sample, part VII–Data Access Layer is GOOD for you
  6. (08 Jul 2011) Microsoft N Layer App Sample, part VI–Single responsibility principle is for idiots and morons
  7. (06 Jul 2011) Microsoft N Layer App Sample, Part V–Cross Cutting is a fine line
  8. (04 Jul 2011) Microsoft N Layer App Sample, Part IV-IoC FTW
  9. (01 Jul 2011) Microsoft N Layer App Sample, Part III–Abstraction is as abstraction does
  10. (30 Jun 2011) Microsoft N Layer App Sample, Part II–getting lost in the architecture
  11. (29 Jun 2011) Microsoft N Layer App Sample, Part I
  12. (12 Oct 2009) GoGrid vs.Amazon EC2
  13. (12 May 2009) C# in Depth
  14. (02 Sep 2008) Hibernate Search in Action
  15. (04 Jun 2008) Umbrella project
  16. (04 Jun 2008) Mass Transit Samples
  17. (23 Aug 2005) iRiver H340



ceremony, ceremony ceremony... why make patterns easy to work with? I mean, if you're MS - if your customers are learning patterns that are used in other languages - that might mean they have transferable knowledge.

This is what this is all about.

Take the EntLib pile of old shit

policy injection anyone?!

and the EntLib Data Access Application Block!?!? A lump of code that actually makes the task at hand 10000% harder than it would have been without it - while also completely screwing with the whole layout of your system....


Honestly - what the hell were they thinking?! http://msdn.microsoft.com/en-us/magazine/cc163766.aspx


Better questions are why LockAccount is always negating the lock status, allowing LOCKaccount also to unlock the account if it was already locked.

And why is a entity only marked 'modified' if changeTracker is set? Does this mean that if account.StartTrackingAll() isn't called that entity is never marked modified, but it is actually saved to the database??

And most importantly. If you lock an account, why on earth would you want to redirect the user to a transfer money page?

Peter Morlion

With all respect to the people who coded this, but it looks like it was coded by an intern. Very sloppy stuff.


Want to learn all the presented internals, how they should be implemented, in terms of locking, tracking, states? Read NHibernate code and learn. The wrap around wrap around ORM. Gosh, are they paid for each line of code (additional bonus for ctrl+c, ctrl+v?

Jeff Sternal

If I'm reading this right, the LockAccount method also unlock locked accounts.

Frank Quednau

Sigh, and then customers frown because the argumentation and opinions expressed to them are so detached from Microsoft.

The null-cast alone shows blatant ignorance to what the developer is actually doing.

I have been wondering lately, did you find anything remotely positive about the whole "official guidance"?


I don't quite understand the casting of null part??

Alex Simkin


This is what Ayende ment as casting of null:

if (bankAccount == (BankAccount)null) {


Haha this is just embarrassing.


Well, a cast of null can be done in order to remind you what kind of type class you are dealing with. It does not heart. It can be a matter of readability. For instance, I saw similar 'null casting' in PEX & MOLES code.

In any case, I beleive that most of that code is outdated as they are working on V2.0 code. That code review is part of V1.0 Sample.


Who every wrote/reviewed/verified this code should not be allowed next to an IDE


Is this really how you should be performing this operation in an "N-Layer" application? Why not just encapsulate that saving logic (i.e. business logic!) into your service itself. What if someone provided an invalid account to the method (etc etc)?:

public void LockAccount(string acctNum) {
    ServiceResult result = BankingService.LockAccount(acctNum);

    if (result.WasSuccessful) {
        // do something (display message, redirect, etc.)

        return RedirectToAction("Index");
    } else {
        // set errors/warnings/alerts/whatever
        // return view model or some alert mechanism
        base.Alert(result.Errors, "Sorry, we could not lock the account due to some errors:");

        return View();

Or something to that effect (i.e. encapsulating this saving stuff into the service itself) so that consumers of your service layer don't need to manually save entities? All of that code smells wrong to me.

Alex Simkin

Casting null may be used in two cases:

  1. For generic type inference when compiler cannot figure out type, and
  2. When trying to use null as a result for the ternary operator to avoid "no implicit conversion" error.

However, this is so 90's. Cool guys should use default(...) in such cases.


As I see it, one reason to cast a null to some object would be if we had a function that expected a certain object type as a parameter. The compiler would then need the cast to decide for the correct function. But in the Microsoft example, I really don't understand it.

Duncan Smart

For in thing, I suspect the developer's first language isn't English (nor their 2nd or 3rd perhaps... :) ). It seems that "Change" is a typo for "Charge"... but still.

Duncan Smart

Should have said "For one thing..."

Comment preview

Comments have been closed on this topic.


  1. RavenDB 3.5 whirl wind tour: I'll have the 3+1 goodies to go, please - 3 days from now
  2. The design of RavenDB 4.0: Voron has a one track mind - 4 days from now
  3. RavenDB 3.5 whirl wind tour: Digging deep into the internals - 5 days from now
  4. The design of RavenDB 4.0: Separation of indexes and documents - 6 days from now
  5. RavenDB 3.5 whirl wind tour: Deeper insights to indexing - 7 days from now

And 10 more posts are pending...

There are posts all the way to May 30, 2016


  1. The design of RavenDB 4.0 (14):
    05 May 2016 - Physically segregating collections
  2. RavenDB 3.5 whirl wind tour (14):
    04 May 2016 - I’ll find who is taking my I/O bandwidth and they SHALL pay
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats