April 2009 - Posts

This post is political.

I am just glad that along with Israel, Canada and US did not take part in this pre-arranged "show". Boy oh boy, an international conference masqueraded with good intentions, and leading to the same old thing - hatred for Jews and state of Israel. Now ask the question yourself: do you trust UN and everything you hear? Cause I wouldn't.

Some countries representatives have left in order to boycott the speech, and this is where I am not sure if that was in solidarity to what was done to Israel, or because they felt attacked. Either way, I hope they will open their eyes, and stop participating ahead.

PS: The speech of the president of Iran had some brilliant timing - close to the Holocaust day. And the world applauded to the speech. Now will people wake up to see the truth?

I was exposed to Domain-Driven Design not that much time ago. Prior to that, I was struggling with the object-relational impedance mismatch while developing objects based applications. First attempt to get relational database close to the applications I was writing was an uneducated experiment to "wrap" the data into objects. The reason I think of this now as "uneducated" decision, is because I knew nothing about proper object relational mappers, neither had sufficient knowledge about patterns such as Identity, Unit of Work, Repository, etc.

The first wave ended up being a "home-grown" database access layer, trying to be an ORM. Quickly I felt the pain of that solution. Some good books helped to get the answer, such as Patterns of Enterprise Application Architecture.

The 2nd wave was the discovery of NHibernate. It was exciting and frustrating at the same time. Exciting, because it actually could link between the object oriented solution implemented with domain-first. Frustrating, since acceptance of an ORM such as NHibernate in a company that is a hardcore DB driven solutions is hard. To sell something that is new and "possibly is unreliable" is not a simple task, and it took time.

The 3rd wave was adopting FluentNHibernate as a development standard across the development team. Not only this has allowed us a better domain-driven development process, it actually has contributed to a fundamental change in development team - no more dumb developers coding down instructions, but developers that speak ubiquitous language with the business folks, developers that build applications based on the current domain understanding, changing and updating it to what business represents today.

Now I am moving towards the 4th wave - object oriented database. The idea is not original, but the thought is striking when you think - why should I have relational database for a domain/objects structured application? I have raised the question in my previous blog.

Today I have finished to read the book "The Definite Guide to db4o". Good reading. I recommend to look into it. I still cannot see how mainstream would accept it, since RDBMS are so much rooted in the minds of majority of developers and business people. There are some outstanding opportunities for OODMS, but those are not exposed widely.

db4o is mostly free, and available for both .NET and Java platforms. To sum this up I will bring an excerpt from the book to think about:

The key advantages are that developers can work with object structures as if they were in-memory structures, while there is no performance loss associated with processing taking place behind the scenes to fit those objects into a relational database. Little additional coding is required to manage object persistence. New application features can be added to products much more quickly to gain competitive advantages. Models can be updated more easily for debugging or refactoring, or in order to apply change requirements.

FluentNHibernate is an amazingly nice DSL to use for quick NHibernate mapping implementation. Today I had to refactor some of the code we did at work, in order to persist a state of an object. The original code was implemented using State pattern, which allows simple division of responsibility and easy decision making at each given step. I have decided to create a simple example of State pattern persisted with FluentNHibernate in order to demonstrate how powerful it is and what kind of freedom it can give to developers.

The domain for the sample will be some sort of simple message processing system. Listed below are all imagepossible message statuses we can have in the system. A message starts its life in the system when it’s  received the first time, and then its’ status is set to “Received”. When message is processed, in case processing has ended successfully, it’s being send to the destinator and marked as Processed. In case processing was not successful, it’s state is updated to the “FailedProcessing”. When a successfully processed message is sent out, its’ status is set to “Sent”, which is the last status possible in the given system.

First we want to implement the Status using State pattern.

   1: public abstract class MessageStatus : Entity
   2: {
   3:   protected MessageStatus(string status)
   4:   {
   5:     Status = status;
   6:   }
   7:  
   8:   public string Status { get; protected set; }
   9:  
  10:   public virtual void Complete()
  11:   {
  12:     throw new InvalidOperationException(string.Format("Status '{0}' cannot have a complete state.", Status));
  13:   }
  14:  
  15:   public virtual void Fail()
  16:   {
  17:     throw new InvalidOperationException(string.Format("Status '{0}' cannot have a failed state.", Status));
  18:   }
  19: }

I want to be able to store message (current) status as an entity on its’ own. We will use this to query system for messages with a particular status. MessageStatus is an Entity, entity is a base class for all entities (included in source code) to handle ID handling and equality.

Next step is to define behavior for the statuses we have. Let’s do it through tests.

   1: [Concern(typeof (MessageStatus))]
   2: [TestFixture]
   3: public abstract class ReceivedStatus_Specs : ContextSpecification<MessageStatus>
   4: {
   5:   protected override MessageStatus create_system_under_test()
   6:   {
   7:     return new ReceivedStatus();
   8:   }
   9:  
  10:   protected override void establish_context()
  11:   {
  12:     system_under_test = create_system_under_test();
  13:   }
  14: }
  15:  
  16: public class When_ReceivedStatus_is_asked_to_complete : ReceivedStatus_Specs
  17: {
  18:   protected override void because()
  19:   {
  20:     system_under_test.Complete();
  21:   }
  22:  
  23:   [Observation]
  24:   public void Should_become_Processed_status()
  25:   {
  26:     system_under_test.Status.Should_Be_Equal_To(ProcessedStatus.StatusName);
  27:   }
  28: }
  29:  
  30: public class When_ReceivedStatus_is_asked_to_fail : ReceivedStatus_Specs
  31: {
  32:   protected override void because() {}
  33:  
  34:   [Observation]
  35:   [ExpectedException(typeof(InvalidOperationException))]
  36:   public void Should_throw_an_exception()
  37:   {
  38:     system_under_test.Fail();
  39:   }
  40: }

Complete state transition specification will fail. Fixing the test will complete the implementation.

   1: public class ReceivedStatus : MessageStatus
   2: {
   3:   public const string StatusName = "Received";
   4:  
   5:   public ReceivedStatus() : base(StatusName) {}
   6:  
   7:   public override void Complete()
   8:   {
   9:     Status = ProcessedStatus.StatusName;
  10:   }
  11: }

The rest of the statuses are very much similar.

  • “Processed” status overrides both Complete() and Fail()
  • “Finished” and “FailedProcessing” statuses do not override anything as they are the final states
   1: public class ProcessedStatus : MessageStatus
   2: {
   3:   public const string StatusName = "Processed";
   4:  
   5:   public ProcessedStatus() : base(StatusName) { }
   6:  
   7:   public override void Complete()
   8:   {
   9:     Status = SentStatus.StatusName;
  10:   }
  11:  
  12:   public override void Fail()
  13:   {
  14:     Status = FailedProcessingStatus.StatusName;
  15:   }
  16:  
  17: public class SentStatus : MessageStatus
  18: {
  19:   public const string StatusName = "Sent";
  20:  
  21:   public SentStatus() : base(StatusName) { }
  22: }
  23:  
  24: public class FailedProcessingStatus : MessageStatus
  25: {
  26:   public const string StatusName = "FailedProcessing";
  27:  
  28:   public FailedProcessingStatus() : base(StatusName) { }
  29: }

Next step – mapping.

Mapping is done with FluentNHibernate, which is a great DSL to simplify and abstract XML mapping from domain world into relational database. Saying that, it is a good practice to understand what is done behind the scenes. There are plenty of resources out there. My recommendation is the XXX book.

Message mapping looks like this:

   1: public sealed class MessageMap : ClassMap<Message>
   2: {
   3:   public MessageMap()
   4:   {
   5:     Not.LazyLoad(); 
   6:     Id(x => x.Id);
   7:     Component(x => x.Content, part =>
   8:                               part.Map(y => y.Value)
   9:                                 .ColumnName("Content")
  10:                                 .CustomSqlTypeIs("text")
  11:                                 .Not.Nullable());
  12:  
  13:     References(x => x.Status)
  14:       .Not.Nullable()
  15:       .Not.LazyLoad()
  16:       .FetchType.Join();
  17:   }
  18:  
  19:   private XmlDocument Generate()
  20:   {
  21:     var mapping = CreateMapping(new MappingVisitor());
  22:     Trace.WriteLine(Regex.Replace(mapping.InnerXml, ">", ">\r"));
  23:     return mapping;
  24:   }
  25: }

Content is just a string at this moment, but it makes sense to wrap it with a value object and give it a meaning of Content, rather than just a string. Lately I see more an more benefit in this technique, especially when dealing with a new domain and understanding of domain concepts is rapidly changing.

Messages’ status is referenced by message. Cascade.All() will cause saving of the MessageStatus entity with messages’ ID. That is done intentionally, in order to allow querying later with no message data, just the ID. Mapping looks like this (and generated by the Visitor provided by FluentNHibernate):

   1: <?xml version="1.0" encoding="utf-8"?>
   2:     <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="StatePatternPersisted" namespace="StatePatternPersisted.Domain">
   3:         <class name="Message" table="`Message`" xmlns="urn:nhibernate-mapping-2.2" lazy="false"><id name="Id"><generator class="native"/></id>
   4:             <many-to-one cascade="all" not-null="true" lazy="proxy" fetch="join" name="Status" column="Status_id" />
   5:             <component name="Content" insert="true" update="true">
   6:                 <property name="Value" length="100" type="String">
   7:                     <column name="Content" not-null="true" sql-type="text" />
   8:                 </property>
   9:             </component>
  10:         </class>
  11: </hibernate-mapping>

Writing this by hand is possible, but too tedious.

Mapping MessageStatus has to be based on the fact that each entity will have a value (discriminator) that will cause NHibernate to instantiate this or another descendent of MessageStatus base abstract class.

   1: public MessageStatusMap()
   2: {
   3:   Not.LazyLoad();
   4:   Id(x => x.Id);
   5:   var column_name = "Status";
   6:  
   7:   DiscriminateSubClassesOnColumn<string>(column_name)
   8:     .SubClass<ReceivedStatus>(ReceivedStatus.StatusName, x => { })
   9:     .SubClass<ProcessedStatus>(ProcessedStatus.StatusName, x => { })
  10:     .SubClass<SentStatus>(SentStatus.StatusName, x => { })
  11:     .SubClass<FailedProcessingStatus>(FailedProcessingStatus.StatusName, x => { });
  12: }

The generated HBM shows the details:

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="StatePatternPersisted" namespace="StatePatternPersisted.Domain">
   3:     <class name="MessageStatus" table="`MessageStatus`" xmlns="urn:nhibernate-mapping-2.2" lazy="false"><id name="Id"><generator class="native"/></id>
   4:         <discriminator column="Status" type="String" />
   5:         <subclass name="StatePatternPersisted.Domain.MessageStatuses.SentStatus, StatePatternPersisted, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="Sent" />
   6:         <subclass name="StatePatternPersisted.Domain.MessageStatuses.FailedProcessingStatus, StatePatternPersisted, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="FailedProcessing" />
   7:         <subclass name="StatePatternPersisted.Domain.MessageStatuses.ReceivedStatus, StatePatternPersisted, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="Received" />
   8:         <subclass name="StatePatternPersisted.Domain.MessageStatuses.ProcessedStatus, StatePatternPersisted, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="Processed" />
   9:     </class>
  10: </hibernate-mapping>

NHibernate will use the column “Status” to discriminate the entities based on the value in that column. Each value is mapped to a particular MessageStatus implementer.

What’s next?

Next we should be doing configuration and persistence into the actual database, which I leave to your own choice. In order to allow code download, I have stripped the “heavy tools/libraries” in case those can be downloaded separately by just looking on the empty folder names in the Build project. I hope this example will help others to see the oportunities hidden behind FluentNHibernate DSL, to leverage an easy ORM such as NHibernate in order to create better code.

Code for download

Update: Seems like I completely forgot to implement the Entity class itself. public abstract class Entity { public int Id {get; private set;} ...}
Entity has also to implement the  overriden GenHashCode, ToString, and Equality. 

I have started to read a new book about OODB. The reason I decided to get the book and go through it is because I am SICK of the traditional RDBMS and the way it forces us to go through loops and hoops to create domain driven applications.

The very first question to popup - what's the practicality? Don't know, but I know the truth is out there. And it doesn't have a shape of conventional databases.

According to the book, db4o is exceptionally good for any kind of DB activity that operates on object hierarchies rather than any combination of RDB with proper OR Mapper (even such as N/Hibernate). And yes, it sucks when you work with it as query source for things like reporting, but wouldn't be that a re-use/abuse case? For reporting, use the relational representation of the data, not object one.

Why in the world one would like to deal with it? Well, actually a lot of reasons why would someone would like to deal with it. First of all, if you are doing DDD and implement your code with Domain-First approach, data has no meaning without objects. Your data is the state of the objects. Objects cannot keep going without retrieving it's state once they reloaded, but state without objects is meaningless, unless it has a different domain that utilizes it differently (again, reporting is a good example). Therefore, I am questioning the standard approach that "code comes and goes, data stays forever". No. Not in domain driven application.

Have comments? Shoot!

Just finished reading "The Productive Programmer" by Neal Ford. Mixed image emotions about the book. On one hand it's packed with good practices and decent tips to follow, on another hand haven't I watch the presentation on InfoQ, reading the book might be a bit more pleasant...

One good thought that I liked in the book especially - question the authorities. Exactly as the author of the book ran into the no acceptance of changing long-time structured naming conventions, same happened to myself recently in a team with a couple of "old school agile consultants" (no names :). And the most important lesson, don't turn into angry (code) monkey and beat other developers for fresh ideas. 

PS: I do recommend to watch the presentation, though. It's good.

Neal Ford had an interesting presentation at InfoQ. An interesting presentation. One part was funny, is to hear from a core Java guy calling Pocos by a funky name “Ponos” :) Well, besides the fact it’s not the name we (.NET folks) use, it’s also funny to hear if you know the similar Russian word.

In my previous blog about Hudson I was excited as a user how cool and great this CI engine was. This week I had to quickly setup a project and get it going on our build server. Boy oh boy, that was great task. Easy, clean, and fast. You should definitely check it out.

For those who want to install it, keep in mind the next:

  1. You will have to install Java Runtime
  2. Once you can run the command line version, you can through the browser install the windows service (a much better way of running CI server)
  3. You probably will need a few .NET related plugins to install prior to setting up any projects, such as
    1. MSBuild (if you use it)
    2. NAnt (if this is your build script of choice)
    3. Gallio report builder (if you use MbUnit 3.x)
    4. Green Balls (if you rather see red-green and not red-blue – why blue?!)

The nice part is that the information is all available at the Hudson site, and very easy to find.

Oh, and Terry, why won’t you blog about setting it up for the .NET community. Folks would love to read it :P

image

Update: this blog post is a nice detailed step-through guide how to setup and run Hudson. Uses NUnit, but not a big issue to convert to anything you need.

I was working a lot lately with regular expressions and XPath. As usual in scenario like this, you want to have a good tool to assist your tests writing. For regular expressions I used Rad Software Regular Expression Designer that does a good job of matching and replacements. For XPath, I used SketchPath 2.0 which is really friendly and great to use. Both are free for usage.

Feel free to comment on other useful tool you’ve encountered for XML and XPath.

Our team is moving away from Rhino.Mocks to Moq.

Rhino.Mocks was the first mocking framework I encountered in my life about a year and a half ago, while taking JP’s Nothing But .NET training course. It was great (the power of mocking), it was weird (record/playback concepts), it was good. But lately, the pain of legacy support and backwards compatibility has made the tool too fat and complex.

If you are familiar with AAA syntax, Moq will feel no big change. Just less fat and more to the business. It’s simple and expresses well the concepts you need for mocking. There’s no backwards compatibility, as it entirely based on C# 3.0 and AAA syntax.

At this point this is our mocking framework of choice. Saying this, it doesn’t mean we won’t change it if something better shows up, but for now this is the best we could find for our usage. Go grab it and try.

Testing is normally divided into unit testing and integration testing. Is it?

The current project I am involved in has gone through some very interesting turns. I was privileged to get into some really interesting debates and get exposed to different approaches that might not always can agree with, but definitely contribute to my better understanding of testing in general. Testing, one word so many meanings...

Do you write tests to guard against unexpected changes in your code? Or do you write the tests to validate assumptions? Maybe you do it to drive out the design and implementation? Or you testing how components work together when they actually put together? If you do one of those, you do testing. Then the question comes - is that enough or  should all of the mentioned be done plus more? Well, this is the reason I decided to blog about the subject.

My initial attempts to write tests were lack of satisfaction with the way I created code, the guts feeling that there must be a better way. I want to know that 2+2 is actually 4, and when it's replaced by x and y the result is what it should be. Later I liked the power of being able to validate interactions between the components without having the whole shebang up and running. Mocking it is called. Along combined with state based testing it felt good. It felt right, but still something was missing. The missing part was the clarity - I needed to understand what is happening with the tests, and that's why I loved the AAA syntax showed to me down the road and specification based testing. From that moment it became an infection that spread all over the brain and it was irreversible. And I am glad it was like that.

But a few project have showed that it still did not bring me the complete satisfaction and confidence in the code. This is were pain for integration was a clear sign. The last project has proved that the ration between so called "unit" testing (specifications) and automated integration testing should be about 50% to 50%. I know it sounds extreme, but imagine this: a component A that has a dependency on a component B. For specifications and design drive B can be mocked and everything is great. The key assumptions is that if B is tested and passes, there's no point of re-testing it along with A, as in A's tests we are only interested to exercise A's stuff. But are we? Well, partially true. For design and implementation purposes, yes. B should and will be mocked out. But right after that should come an integration test, where the real A and B both are used. That will ensure that the design secured by specification tests is implemented the way integration tests are using it. This is the total coverage.

So integration tests are required and team buys into that, but what will be an automated integration testing? This is what it is defined:

1. Touching DB

2. Going over a wire

3. Touching file system

4. Performing anything that is not in memory

Do I agree with the traditional definitions? Nope!

Doing anything that will cause more than a single component to execute it's production code with a purpose of getting the result anticipated from a production code - to me is an integration test. It can be all in memory, it can touch the DB, it can do whatever is necessary to make sure that production code will stand the expectations. And we can always optimize things, right? Locally cloned DB, emulating file system, instantiating multiple objects to work against. It is all good.

So if you do tests, keep in mind the importance of integration testing. Do not let it go invisible just because specifications are in place and make you feel comfy. Remember 50%/50%, or would it be more 100% to 100% - you own call, as long as you can live with it.

More Posts Next page »