No, NSK is not (about) Northwind

So it seems my pet project has been Ayende-ified; Oren has been so kind to let me have a preview at his posts and that urged me to talk a bit about what NSK is and what it is aimed for.

I started NSK in 2004 in order to have a “one size fits all” demo for the talks and classes I and my colleagues at Managed Designs were holding; all I wanted was a way to showcase topics such as:

  • How an O/RM works (Unit of Work, proxies, queries & fetch plan management, …)
  • How to implement common presentation design patterns (MVC, MVP, …)
  • How to unit test code

Other than having that demo, I also wanted to make everyone able to have it up & running as frictionless as possible, so I:

  • Made it open source (choosing IBM’s OSI-approved Common Public License) and downloadable from Sourgeforge
  • Had it use the “ubiquitous” Northwind database

I knew from the start that such a db would have impaired my ability to design an effective Domain Model, but I didn’t care ‘cause that was code that I was expected to show (and comment), and not targeted to the casual downloader.

Let’s just have an example: in order to allow me to explain how an O/RM works and avoid early adopters (remember, we are talking about year 2004/2005) fear for “O/RMs black magic”, I defined both a IUnitOfWork interface and my own query object, and then implemented both delegating the real work to NHibernate. I even implemented proxies for a couple of entities in order to show how O/RMs manage concurrency. Frankly, I did not pay much attention to my implementation of UoW/query object/proxies, because that implementation wasn’t neither expected to go live in a “production” system nor looked at as an advice to encapsulate an O/RM. That “poor man’s” O/RM, let me stress about that, was only meant to allow me to talk about how an O/RMs works without having to resort to NHibernate source, which IMHO would have been overkill. And when DLINQ Linq 2 SQL came to us, I renamed the IUnitOfWork interface to IDataContext in order to better adhere to .NET’s emerging jargon: again, I was only trying to have a “companion demo” which fitted my own presenting needs so, when the “pioneering era of O/RM” relating to the .NET community ended (which, IMHO, happened with Microsoft releasing L2SQL and then EF) I considered my “introduce O/RMs by explaining the inner workings” strategy obsolete, made a branch for those still interested in having a look to that code and had NSK switched to the LINQ side of the Force.

And then came the book (to which, BTW, both me and Dino refer to as “the brick” <g>): we needed a companion demo, and ultimately chose to use NSK so I had to fit into the codebase a bunch of samples covering nearly all the book’s topics, such as:

  • The book talks about IoC and unit testing, so I ended implementing custom factories for both ASP.NET MVC (which at the time was in the “v1 beta” timeframe) and WCF in order to inject dependencies and/or mock objects
  • the book talks about validation and we wanted to show the capabilities of Enterprise Library’s Validation Application Block (which I still think is pretty gorgeous), so I did put in demos using both the custom attributes and the xml rulesets to validate the domain model

Again, the codebase was intended to be looked at while being guided by the book and not as a fully fledged application (please note that NSK still does not sport a release) or as an example of how to implement a Domain Model. If you look to NSK this way, you will find a lot of thing you would do very differently in a “real” application. Just to make an example: you have a look at the CalculateTotalIncome() method of the Customer class and find the following, pretty sub-optimal, code:

   1: public virtual decimal CalculateTotalIncome()
   2: {
   3:     Contract.Ensures(Contract.Result<decimal>()>=0);
   5:     decimal income = this.Orders.Sum(o => o.CalculatePrice());
   6:     return income;
   7: }

In a “real world” application, should the evaluation of the total income be a mere sum, I’d have the O/RM generating a proper query. But, for the sake of the project, I only wanted both a mockable customer repository and the above shown method in order to set up a unit testing demo for the following service:

   1: public virtual decimal CalculateSuggestedDiscountRate(string customerId)
   2: {
   3:     Contract.Requires<ArgumentNullException>(customerId != null, "customerId");
   4:     Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(customerId), "customerId");
   6:     Customer customer = customerRepository.FindById(customerId);
   7:     decimal income = customer.CalculateTotalIncome();
   8:     decimal discount = 0;
   9:     if (income > 5000)
  10:     {
  11:         //Suggests a 6% discount if income>5000USD
  12:         discount = 0.06M;
  13:     }
  14:     else
  15:     {
  16:         //Suggests a 1% discount for every 1000USD of income
  17:         discount = income / 100000;
  18:     }
  19:     return discount;
  20: }

This way, I could write down the following test:

   1: [TestMethod]
   2: public void Test_Calculation_Of_Suggested_Discount_Rate_For_Customers_With_Total_Income_Of_Less_Than_5000_Dollars()
   3: {
   4:     decimal generatedIncome = 3500;
   5:     string customerId = "FAKE1";
   6:     var custMockBuilder = new Mock<Customer>();
   7:     custMockBuilder.Setup(c => c.CalculateTotalIncome()).Returns(generatedIncome);
   8:     var repoMockBuilder = new Mock<ICustomerRepository>();
   9:     repoMockBuilder.Setup(r => r.FindById(customerId)).Returns(custMockBuilder.Object);
  11:     MarketingServices svc = new MarketingServices(repoMockBuilder.Object);
  12:     Assert.AreEqual<decimal>(0.035M, svc.CalculateSuggestedDiscountRate(customerId));
  13: }

To cut a long story short: the reader should look neither at the CalculateTotalIncome nor at the CalculateSuggestedDiscountRate service per se, but at the unit test. Think about it as a MSTest + Moq demo, and maybe you’ll get the picture I wanted to give to the reader. Being the code targeted at someone who has attended a class/talk or read the book, I thought there was no way to have someone misunderstanding the scope of the project.

The same goes for the domain model at large: if you take a look at it, you’ll notice that aggregates encapsulation is pretty low; I just managed to:

  • define an aggregate root abstraction (the IAggregateRoot interface), and built an eco-system aware of it (i.e. the repositories which uses IAggregateRoot explicit implementation and code contracts to enforce domain logic)
  • have the aggregate roots implement factories in order to prevent bad instantiation to happen (builder pattern anyone?)
  • encapsulate some navigation properties having the domain model “users” forced to use domain logic (i.e. the, AddProduct method of the Order entity which is the only way to add a product to the order… or avoiding that, from a domain logic perspective)
  • added a bunch of domain services (i.e.: GetRelatedProducts, CalculateSuggestedDiscountRate, …) in order to show the differences lying in implementing domain logic within the model and/or by means of services

Again, that was enough for my own needings; demo after demo, technology after technology (code contracts, entity framework POCO mapping, jquery, MVVM) NSK grew up and nearly became the “one size fits all” demo I needed in my talks. Up to late 2010/early 2011, I only managed to update NSK in order to remain useful for my own needings.

All of a sudden, though, “developing” NSK wasn’t fun anymore so I settled down and asked myself what I wanted to do with it; my answer was: “make it a real application under the form of an Amazon-like e-commerce web site”. That would have justified the complexity DDD kicks in and also (hopefully) make the project a foundation for solutions we build at Managed Designs. I opted to go for the CQRS way and started focusing of the front-end: so I quickly implemented a read model and started implementing a couple of user stories only to define a structure for the project (architecture, file system, NuGet-based pre build actions, …), only to discover that such a project is too much of an effort for a single person which is going to develop it in his spare time, so I contacted a bunch of friends in order to ask for their support. Then we stopped committing code and started writing down the user stories that will lead our effort from now on.

That’s why the read model and the domain model appear so similar, being the former a mere database reverse engineering and the latter the model that fitted my own “demoing” needings, but we’ll remove this model (and other code, such as the repositories) as soon as we’ll have the new ones, which will emerge from the user stories. Remember that I *still* need demoes for my talks, so I won’t be able to remove “demoable” code until when I have a replacement for it. The nice part is that we think that the new model will be composed by “real” aggregates and that we’ll switch from SQL server to a NoSQL database, so the “R” and “C” parts are going to be pretty different.

At this very moment, being the “code writing” on a hiatus, the only code I would recommend a casual downloader to look at is the stack that shows the recommended projects onto the home page, which picks the products following the following strategy: “Given all products->Choose the ones we are selling (which, of course, aren’t all the products we have in the db)->Then select only the available ones->Let’s pick up the projection we need within this view”. That happens picking them (the products) by means of an expression tree that is composed while goin’ up the application stack: that’s an idiom we at Managed Designs nicknamed LET (which stands for Layered Expression Trees) and we think that it’s a pretty powerful idiomatic way to express logic in a DDD context. I’ll talk about it at the next local event, and dedicate soon a blog post to it.

To sum it up: if you’re looking for a sample application, you’d better search for a project sporting a release version (which is something NSK still doesn’t) or, at least, wait for we to implement some more user stories in order to allow the whole design to consolidate. In the meantime, the project can still be looked at in order to take advantage of some “pills” you could be interested in for your projects, such as:

  • JQueryUI integration with ASP.NET MVC (i.e.: the DateTime partial view implemented for both WebFormsEngine and Razor engine)
  • MVC and WCF custom factories
  • NuGet-based pre build actions in order to easen you first “compile&run” (for those using the recently released v1.6 of NuGet, I’d recommend to have a look at the “package restore” feature)
  • Code Contracts aware IRepository interface (don’t focus on actual designs: the real meat lies in having IAggregateRoot + code contracts enforcing your validation even when application code “forgets” to do domain object validation)
  • Rss and Atom custom ActionResults for ASP.NET MVC, taking advantage of framework’s built-in serializers

That’s all code you can use (and refactor to satisfy your own needings) due to the license chosen. Enjoy NSK!

P.S.: I know, project’s description on Codeplex sucks bad. Mea culpa: I’ll fix it ASAP.

posted @ Tuesday, January 10, 2012 12:17 PM


Comments on this entry:

# re: No, NSK is not (about) Northwind

Left by Luca Minudel at 1/10/2012 1:51 PM
The post from Ayende is based on wrong assumptions and some misunderstandings.
This is common between we developers, because communication skills are not our main strength and because sometime our ability with the code/machines make us arrogant with people...

Still someone for free spent time to look at the code and comment it.
While the conclusion are biased by the wrong assumptions and the misunderstandings, maybe some mentioned facts can be used to inspire some potential improvements. Let see if we can take something good out of it.

- You already pointed out the description, it can be improved to avoid misunderstanding, that are possible with the current description as the post of Ayende show.

- The other thing that could be improved is the number of projects: all those projects are released together so there are no need to split them in different project.
To define the logical grouping of classes, namespaces should be used.

- Not sure about the comments, usually they can take away the focus from the readability of the code. Maybe class/method/arguments names could be reviewed to check that they explain in an easy way what the code is doing. This is an hypothesis from me, don't know the code to say if this really apply.

P.S. in practice NSK SDK has proven to be useful to many people to start to look into architecture and is an example useful for the book you wrote. NSK SDK seems to be very useful , keep up with the good work guys !

# re: No, NSK is not (about) Northwind

Left by Maurizio Tammacco at 1/10/2012 2:08 PM
Ayende's post is definitely the result of misunderstandings, but on one thing he's, however, absolutely right, or the implementation of ICollection by IRepository violates the contract and provide no implementation for some methods.
LSP and ISP should make you think.

# re: No, NSK is not (about) Northwind

Left by raffaeu at 1/10/2012 2:10 PM
I started to follow this project in late 2006 and I still remember how much I struggled to learn that complicated architecture (Domain, DAL, BLL, UI).
After the first trial I moved back to NSK in 2007 with a new application and this time the effort was well spent; the customer I built that application for is still using it, so the architecture is not so overcomplicated and not understandable.
Then I moved to BM and Dino and Andrea published their book that I have exhaustively evangelized in the company I working now.
Our code base is very close to NSK and I (we) personally believe that this can be a succesfull and reusable architecture.
Of course you have to be *forward-looking*

# re: No, NSK is not (about) Northwind

Left by Danimar Ribeiro at 1/10/2012 6:22 PM
I understand your point. But also Ayende is right.
When I first looked this example, I got really confused to understand, and I thought this example was completely right. But after reading some reviews from Ayende, somethings got cleared to me. He shows the wrong points, it doesn't matter if the intention wasn't supposed to be that, to the people who are starting this advice about what is right, and what's wrong is good.

# re: No, NSK is not (about) Northwind

Left by Nazareno at 1/10/2012 10:29 PM
NSK is not an easy project. I start to “read” it some years ago and I found really difficult to understand it, but I never think that it was a problem of the project (or of its developers). My feeling was that I was not ready for it.
Some years after I have the possibility (and the luck) to study better the underling architectures and I start to understand how to “use it”. At the moment this is not a sample app that you can reuse “ready, steady, go” on you projects. I use it to search suggestion about design or implementation, knowing that I have to write my own code to use it in my solution.
It, also, try to investigate new topics (like CQRS) when there is no so much implementation reference project around. I think this is another good thing.
Even if I do not think that you can measure a solution using the number of project, the large number of project is due to the fact that this solution tries to embrace the most common frameworks/tools.
A real and complete implementation of a MES project (layered and with test project) made using lot of pattern inspired by this project count “only” 11 projects (obviously we have chosen only one ORM and one presentation design pattern).

# re: No, NSK is not (about) Northwind

Left by Daniel at 1/11/2012 1:28 PM
I thought that demos and books are meant to present the *best* way to write an application, not some so-so, average way (I won't call names).

# re: No, NSK is not (about) Northwind

Left by Jason Meckley at 1/11/2012 2:57 PM
So the project has evolved over the years. What i'm reading between the lines is you have 1 general purpose project as opposed to many context/demo specific projects.

simply put, context specific projects will always be better as the reviewer doesn't need inherent knowledge about what was intended vs. what should be ignored.

not to mention there is some level of accountability with public code and publishing a book. whether you explicitly say it or not, it says "this is how you do it".

# re: No, NSK is not (about) Northwind

Left by trustee at 1/14/2012 12:14 AM
please delete this project from internet. it does more harm than good

# re: No, NSK is not (about) Northwind

Left by Riccardo at 1/20/2012 11:41 AM
Hi all, I read all posts from Ayende around NSK and also commented out some of them. It used some implementation optimization in order to justify his point of view. Is like if one say... I have an hole in my pants then I have to change all my dress. He is trying to communicate the concept that the repository abstraction is not useful. His blog code source, prove exactly the opposite :)

# re: No, NSK is not (about) Northwind

Left by haroon at 1/30/2012 8:22 PM
I think Ayende needs to provide some good examples other than NSK to learn from instead of just critisizing.

# re: No, NSK is not (about) Northwind

Left by Jakub at 10/3/2013 2:10 PM
Very interesting project. I'm not a specialist in this type of work, but i can see the advetages of your ways.

# re: No, NSK is not (about) Northwind

Left by undergraduate thesis at 1/8/2014 3:44 PM
School people regularly gravel at whatever point they think about their proposition or are going to take a shot at it. This article offers proposal written work tips for both undergrad and graduate scholars with the trust of making the knowledge of picking and working with an examination supervisor/mentor a charming one.

# funy image

Left by funy image at 2/11/2014 10:20 AM
The funny images have most important part in our lifes beacuse its keep our charmfull and our heart filled with pleasure so the funny images play a diffrent role in our life for fun.
funy image

# re: No, NSK is not (about) Northwind

Left by tensioner at 4/3/2014 11:00 AM

# NO NSk

Left by Stanford at 4/13/2014 12:00 AM
I think so the author is right.. But can't pick up the Admin SO far.

# castle clash hack android

Left by castle clash hack android at 11/15/2014 11:29 AM
Blogs @ UGIdotNET

# re: No, NSK is not (about) Northwind

Left by Rd - Enhancing Video Streaming at 11/23/2014 10:05 PM
Very grateful for the writeup, some interesting information I was able to glean from it. As someone new to NSK, I think I'm in for a very long and tiresome journey getting to grips with this API.

# re: No, NSK is not (about) Northwind

Left by sitedesign at 2/17/2016 7:54 AM

# re: No, NSK is not (about) Northwind

Left by sitedesign at 2/21/2016 1:24 PM

# re: No, NSK is not (about) Northwind

Left by آهک هیدارته at 4/13/2017 12:56 PM

# re: No, NSK is not (about) Northwind

Left by آهک هیدارته at 4/13/2017 12:59 PM
Gravatar جوش شیرین اسید سولفوریک آهک بنتونیت پرلیت

# web design

Left by ساخت سایت at 8/8/2017 4:49 PM
I do not know much about what is posted
ساخت سايت
طراحی سايت تهران

# re: No, NSK is not (about) Northwind

Left by adderall at 11/8/2017 4:24 PM
I appreciate and thank you for sharing this article

# matlab programer

Left by ali fadaie at 11/18/2017 12:14 PM
I'm not exactly aware of the writer's desire. I'm sorry about this

انجام پروژه متلب

# re: No, NSK is not (about) Northwind

Left by ساخت مهر at 12/3/2017 3:13 PM
مهر شرکت
مهر لیزری

# Sega Sports Shoes

Left by Sega Shoes at 12/17/2017 11:07 AM
This blog embraces delightful material, every post is enticing, thanks for the info.

# re: No, NSK is not (about) Northwind

Left by عمل بینی at 1/31/2018 8:19 PM
I thought that demos and books are meant to present the *best* way to write an application

# re: No, NSK is not (about) Northwind

Left by <a href=" at 2/7/2018 5:43 PM
I was very thankful for the discussion

# re: No, NSK is not (about) Northwind

Left by [url=]انج at 2/7/2018 5:44 PM
thanks for sharing

# dayiran

Left by meys at 2/10/2018 10:28 AM
دي ايران فروش اينترنتي مبلمان خانگي و اداري ميز ناهارخوري بوفه استند ساعت ايستاده ميزکامپيوتر انواع تشک سرويس خواب کودک و نوجوان و بزرگسال
با بهترين کيفيت و قيمت
ارسال رايگان به سراسر ايران

# dayfarsh

Left by parham at 2/10/2018 10:29 AM

فروشگاه آنلاين فروش انواع فرش هاي ماشيني گليم فرش فرش شگي فرش هاي تزئيني فرش شنل فرش کودک
با بهترين کيفيت و قيمت
ارسال رايگان به سراسر ايران

# decormod

Left by dorsa at 2/10/2018 10:30 AM
مجله تخصصي دکوراسيون و مد

# behtarha

Left by elnaz at 2/10/2018 10:30 AM
مجله اينترنتي بهترها

# re: No, NSK is not (about) Northwind

Left by آموزش موسیقی at 3/9/2018 12:58 PM
Thanks for sharing good information

# re: No, NSK is not (about) Northwind

Left by اسکوتر برقی at 3/9/2018 12:59 PM
Fantastic blog

# re: No, NSK is not (about) Northwind

Left by آموزش پیانو at 3/9/2018 1:00 PM
Good websitw

# re: No, NSK is not (about) Northwind

Left by اسکوتر برقی at 3/9/2018 1:01 PM
Thanks for sharing good information

# re: No, NSK is not (about) Northwind

Left by تابلو برجسته at 4/14/2018 2:00 PM
It was a very good post
people regularly gravel at whatever point they think about their proposition or are going to take a shot at it.
تابلو چلنيوم
تابلو برجسته
تابلو سازي
تابلو استيل

# re: No, NSK is not (about) Northwind

Left by خرید کفش at 5/8/2018 1:04 PM
thank you for good post
[URL=""]خرید کفش[/URL]

Your comment:

 (will not be displayed)

Please add 5 and 6 and type the answer here:

Live Comment Preview: