Posted by Sébastien Lachance with Comments (0)
This is an interesting pattern. I was using it without giving it a name. I encountered this pattern while reading the Jimmy Nilsson's book : Applying Domain-Driven Design and Patterns - With Examples in C# and .NET. The Unit Of Work is a way to create a logical group of object. The most basic example I can think of is the example of an Order. An order contains OrderLines. The Order and the OrderLines are part of the same logical group. We say that this is an Unit Of Work. I tend to think of it at the way I'll save the Order. Do I call a service to insert the Order and another to save the OrderLines? Or do I call a single service that will persist it all at the same time (maybe using a transaction)? I will use a single service that will save the Order, the OrderLines and all other related information. Everything that must be persisted at the same time is said to be an Unit Of Work.
In the RecipeHelper project (the personnal project I am working on), I have a single Unit Or Work (at the moment). The Recipe is the root and contains a list of steps to follow and a list of ingredients. When I persist a recipe, I persist everything in the object graph.
Here is some resources from experts that can help you more with applying this pattern :
Sometimes, you come across real treasure! This is one of them, printed and with me for a long time.
I have just finished reading the "Foundations of Programming" series by Karl Seguin. This is a "Must Read". It's a really great series that cover all aspect of building an application. I can't recommend it enough!
Here is a link to the PDF file which has all seven posts.
<p>I was wondering when this day would come. When I'd start thinking in term of object. This change in paradigm was not easy. I started programming using the data-centric way and was submerged by it. I was struggling to get rid on this kind of thinking. I've read multiple book before : Agile Software Development, Object Thinking, Refactoring, etc. But this was hard to me. However, 6 months ago, I finally landed on an Agile team. I got a hard time changing the way I was thinking because I was sure that I was understanding object-oriented development.</p><p>For me, the revelation occurred in this team. There I was introduced to my first test-driven project. One of the team members and a consultant for the company was previously working for Object Mentor. I needed to be up to the task, and immerged myself completely into it. I was reading the code of the project regularly and tried to do the same thing. I made mistakes, lots of mistakes, but I learned from them (even if I was not being directed in the right way after making them, but this is another story). Unfortunately the project was put to an halt and I had to get back to the old project, the one that became so complicated that I had abandoned all hope of doing something right with it. I was wrong, this was a wonderful opportunity and this is where the mind shift occurred. I started doing some unit tests and when I was comfortable with the coverage of a certain part, refactor to reflect a more object-oriented and easier to understand ways. I learned about Inversion Of Control (IoC), Dependency Injection, Mocking, Domain-Driven Design and Design Pattern. I also started to read my books back. I re-read Code Complete, Refactor and Object-Design Heuristics, and I am planning to read a lot more. I also adopted many of the Agile practices. This was not hard, since I had worked on project that had big upfront design, and after every release, we were deploying every week a new versions because of the bugs we had found or enhancements the clients wanted. This was just the natural thing to do and I firmly believe this is a good road to follow.</p><p>Now, If I could only get a new challenge to push my limits and learn more!</p>
Following the definition of a nightmare platform, I officially declare Microsoft Dynamics GP to be one of them. Sorry Dynamics GP guys, but I need an explanation for why I have spent half a day trying to figure out how this can be possible.
Consider this example :
salesItem.StandardCost = new MoneyAmount();salesItem.StandardCost.Value = 1;salesItem.StandardCost.DecimalDigits = 2;salesItem.UofMScheduleKey = new UofMScheduleKey();salesItem.UofMScheduleKey.Id = "EACH";Policy policy = DynamicsWebService.GetPolicyByOperation("CreateSalesItem", DynamicsWebServiceContext);DynamicsWebService.CreateSalesItem(salesItem, DynamicsWebServiceContext, policy);
And here is the error message :
A validation exception has occurred.Validation Errors:- StandardCost cannot be less than 0.
I'm still trying to figure out what amount should I put when the item has a cost...