Archive

Posts Tagged ‘DDD’

DDD Immersion Course review

March 8th, 2010 stiiifff No comments

I recently had the chance to follow a 4-day Domain-Driven Design Hands-on immersion course in Paris, with Eric Evans.

It was very inspirational, probably the most interesting course I’ve followed to date. The amount of knowledge contained in the DDD book can be daunting to master, and the course succeeds in clarifying the most complex aspects of DDD as well as delivering the latest evolutions of the approach.

If you are interested in this course, Paul Rayner has written an amazing series of posts about it:

  • Part One – Introduction. What is DDD? Ubiquituous language? a Model?
  • Part Two – Building-Block patterns (eg. Aggregate. Domain Event. Creative Collaboration etc)
  • Part Three – Strategic design – Bounded Context and context mapping.
  • Part Four – Strategic design (continued) – Core Domain
  • Part Five – More on supple design (Specification pattern). Implementation concerns. Discussion.
  • Part Six – Design and agile.
  • Part Seven – Final. Course takeaways and other thoughts.

Happy modeling !

Categories: DDD Tags:

Looking back at 2009, and forward to 2010

December 24th, 2009 stiiifff 4 comments

There will probably be thousands of posts like this one but anyway, here I go with my own … I wanna take a little time looking back at 2009 and then set my personal expectations for 2010.

Overall, 2009 was a great year :-)

  • I finally created my own company ! This gives me a certain degree of freedom in choosing the types of project I want to work on, the knowledge I want to acquire and even more opportunities to meet a lot of very talented people.
  • I learned a lot … especially on how to have a more business-centered focus on software development. Thinking about business value, weighting different solutions benefits vs costs, and also, listening & talking more to the business guys.
  • I read a lot, although I still haven’t finished all the books I started. Functional & concurrent programming, domain-specific languages, Scrum, Business/IT Alignment are in my continuous learning bag.
  • I followed a great course with Udi Dahan and it helped me gain some new perspectives on how to rethink about software design in general. Messaging, SOA & DDD are fascinating topics I want to further learn from.
  • Articles & presentations of Eric Evans on DDD and Udi on CQRS had a tremendous impact on my view of software building. We, software developers tend to focus too much on creating cool frameworks or libraries, and solving complex technical problems, while forgetting about our main goal : providing business value. Sometimes, solving a problem from a business point of view is far easier than at a technical level. Also, we tend to be maniac about every line of code we write … but as E. Evans is saying “Precision design are fragile” & “Not all of a system will be well designed“. Focus your efforts on writing good code in parts of the system that provide most of the business value. It is perfectly fine to write not so good code elsewhere (the duck-tape programmer has its role to play!), just make sure you make everything explicit.
  • Finally, with some friends, I created a new community about the .Net platform & the Agile methodologies, DotNetHub. It’s a very exciting project :-)

So, 2009 was so amazing that I’m eagerly awaiting 2010 !

  • I already booked a DDD immersion workshop with E. Evans in Paris, mid-February, and I’m sure it’s gonna be a great course. Maybe others will follow as I find myself learning a lot during those intensive courses.
  • DotNetHub will keep me busy, organizing great conferences with, well-known and well-known-to-be speakers.
  • I absolutely have to finish all the books I started before I buy any new one !
  • I will restart working on my app based on Northwind and demonstrating DDD coupled with NServiceBus. I have to admit, I got a bit side-tracked by DotNetHub and another major personal event (read further lol).
  • Probably learn some new technical stuff like C# 4.0, VS2010/TFS2010, Dublin & some more F# but honestly, that’s less important and most probably easier than the previous stuff (although F# is quite funky). Using your brain and training your critical sense to filter marketing / wrong piece of information / silver bullets is actually the hardest part of our job.

So, that’s about it folks ! This is the list of my expectations for 2010 … but that’s actually all secondary compared to the main challenge I will have to face : becoming a father end of May 2010 ! ;-) Indeed, my wife is pregnant of my first child. I’m really excited about it … now, on top of my to-do list : read the baby books or get killed :-) lol

Merry Christmas & Happy New Year to all !

Getting a better view from the roof of the Bus

November 3rd, 2009 stiiifff No comments

Last time, I had a first spike with NServiceBus in combination with Castle Windsor, NHibernate & FluentNHibernate, and highlighted the fact that it was quite easy to get those guys up and running together in a clean way, and from there, to process your first messages.

This time, I would like to take a step back, off the Bus, to lay down the foundations of a more concrete & complete example. To do so, I will use the well-known used & abused database sample from Microsoft : Northwind !

I can already hear you all “Northwind, WTF ?!” … except that this time, it’s gonna be different, trust me !

Let’s start by having a look at our veteran database diagram …

NorthwindDiagram

As we can see, Northwind is a traditional sales company, with an online business, which is:

  • Receiving Orders from Customers, classified in different Demographic segments, and shipping those orders via third-party Shippers.
  • Provisioning adequate Stock quantities of Products, arranged in Categories, by buying from several Suppliers, to deliver ordered quantities.
  • Managing Salesmen organized in Territories & Regions.

(yeah, I can get all that from a simple database diagram … impressive imagination huh ? ;-) )

In an SOA world, we wil tend to breakdown the different facets of the company into several independent but cooperating Services.

A naive but all too common way to split our problem domain would probably be as follows:

  • Employee service: manage employees, territories & regions.
  • Customer service: manage customer & demographic segments.
  • Order service: manages customer orders & shippers.
  • Supplier service: manages suppliers & stock.
  • Product service: manages products & categories.

Unfortunately, this simplistic way of defining service boundaries around groups of related concepts most often leads to services that are not autonomous (e.g. call each others, use same database), which violates one of the 4 tenets of SOA (“Services are autonomous“). Plus the fact that it just smells too much like a CRUD API.

A better starting point for modeling services is to analyze the Business Capabilities of the company, that is to say, the distinctive aspects of the company that are contributing to achieve its main goals. Another possibility can also be to model your Services around Bounded Contexts that you would have previously identified, if you are a DDD practitioner. I’m still not yet entirely sure which solution you should prefer and in which conditions … good topic for another discussion.

But in any case, your Services will be better aligned with the Business rather than just being technical CRUD-services.

Here is an attempt at defining the Business Capabilities (BC) and their respective goals in our beloved fictive Northwind company:

  • Marketing BC: maintains a catalog of products, define pricing & promotional operations to maximize sales.
  • Sales BC: accepts customer orders and improves customer relationship.
  • Inventory BC: optimizes the provisioning of product stocks via suppliers to support sales.
  • Shipping BC: optimizes delivery times & costs through shippers.
  • HR BC: manages salesmen to cover sales territories & markets.

They might change as the company’s goals evolve, some being dropped, new ones being added but let’s focus on these ones for now. One service will be defined for each business capability.

Each of those services will eventually use it’s own independent datastore to persist information and will expose its features by accepting incoming Messages and triggering outgoing Events on the Messaging infrastructure.

Those services will be composed to form one or more application(s) that will deliver great value to the company and increase its sales by multiple orders of magnitude !!!

To break with the past, I’ve decided to name this sample Southwind! It sounds warmer and way cooler than Northwind, doesn’t it? :-)  Indeed, it will feature a first personal attempt at building a sample bringing together an Event-Driven Architecture with domain models built following the Domain-Driven Design approach, on top of NServiceBus. Might be a bit ambitious, but it’s so interesting right ?

That’s it for now, till next time where I will focus on the messages & events being exchanged by the different services.

Meanwhile, go read this great article about Event Driven SOA with NServiceBus.

As always, remarks, comments, suggestions are welcome. And if you wanna help building this great sample, you’re more than welcome! ;-)

Stepping onto the Bus

October 12th, 2009 stiiifff No comments

I recently had the chance to follow the Advanced Distributed Systems Design with SOA course with Udi Dahan in Brussels, a rejuvenating experience for various reasons : I had no real-world SOA or Messaging Bus experience, it helped me clear out some misunderstandings I had about DDD and also, although painful at first, Udi finger pointed all the bad habits & misconceptions I had accumulated over the years (I can still hear him squeaking “Northwind ! Northwind !“).

So here I am, entering the realm of SOA, EDA & DDD that might well be heaven on earth for both business people & software-makers, the place we all heard of but never saw. But let’s start slowly, and discover my first endeavor with NServiceBus, shall we? :)

For a good introduction on NServiceBus, I suggest you to read the post from Jan who has been doing research on the same aforementioned topics as well.

This post will focus on how well NServiceBus, NHibernate, FluentNHibernate & Castle Framework play together and how to make them fly in a DDD context. Ok, ok, enough talking, show me the code man !!! :D

To manage my NHibernate sessions (ISession), I like to use the Castle’s NHibernate Integration facility, as it gives me a nice abstraction in the form of the ISessionManager component:

ISessionManager

Next to that, a little more than a month ago, the 1.0 version of FluentNHibernate was released, and it’s really neat for both NHibernate configuration & mappings. So, I like to use it as well:

Fluently.Configure(config)
  .Database(MsSqlConfiguration.MsSql2008
    .DefaultSchema("dbo")
    .ConnectionString(c => c
      .FromConnectionStringWithKey("Northwind"))
    .QuerySubstitutions("true 1, false 0")
    .DoNot.UseOuterJoin()
    .ShowSql())
  .Mappings(m => m
    .FluentMappings.AddFromAssemblyOf<CatalogMap>()
    .ExportTo(System.Environment.CurrentDirectory))
  .BuildConfiguration();

One very nice thing about NServiceBus (and its Generic Host) is that it acknowledges from the start that our softwares have to operate in different environments and for that, it offers a very clean solution : Profiles.

Wrapping it all up, wouldn’t it be nice if I could use Castle’s NHibernate Integration facility for useful components like ISessionManager (and others, more on that later), FluentNHibernate for configuration & mappings and take advantage of NServiceBus Profiles ? Hell yeah !!! Well, that’s actually quite easy & clean. :)

First, configure the NH facility in your config file:

<castle>
  <facilities>
    <facility id="nhibernate.facility" type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration">
      <factory id="nhibernate.factory"/>
    </facility>
  </facilities>
</castle>

Notice that the configuration for the factory is empty … indeed, we want to configure it fluently in the code.

Create a Profile Handler class (implements a IHandleProfile role interface) which simply registers an object into the container (NSB is container-agnostic) :

public class IntegrationProfileHandler : IHandleProfile<Integration>
{
  public void ProfileActivated()
  {
    Configure.Instance.Configurer
      .RegisterSingleton<IConfigurationContributor>(
        new IntegrationNHibernateConfig());
  }
}

… and finally, an implementation of the IConfigurationContributor interface … which is part of Castle’s NHibernate Integration facility, and will be called before NH’s SessionFactory is built in order to, well, contribute to the configuration. :)

public class IntegrationNHibernateConfig : IConfigurationContributor
{
  public void Process(string name, Configuration config)
  {
    Fluently.Configure(config)
      .Database(MsSqlConfiguration.MsSql2008
        .DefaultSchema("dbo")
        .ConnectionString(c => c
          .FromConnectionStringWithKey("Northwind"))
        .QuerySubstitutions("true 1, false 0")
        .DoNot.UseOuterJoin()
        .ShowSql())
      .Mappings(m => m
        .FluentMappings.AddFromAssemblyOf<CatalogMap>()
        .ExportTo(System.Environment.CurrentDirectory))
      .BuildConfiguration();
  }
}

Ok, very nice … but I’m not done yet.

A nice concept that we want to use when querying in a true DDD fashion is the notion of Fetching Strategy (read more about it here & there). We need Fetching Strategies for a very simple reason:

Make sure that once we call into the Domain Model to perform a certain action, it has everything it needs to do its job, without causing lazy-loading to trigger and possibly N+1 select problems.

For that matter, I define a simple interface IFetchingStrategy (IEntityRole is just a marker interface):

public interface IFetchingStrategy<TEntityRole>
    where TEntityRole : IEntityRole
{
  string[] FetchList { get; }
}

… that I can then implement to define a fetching strategy per entity role:

public interface IRegisterProductInCatalog : IEntityRole
{
  void RegisterProduct(string category,
         string productName, string productDescription);
}

public class RegisterProductInCatalogStrategy : IFetchingStrategy<IRegisterProductInCatalog>
{
  public string[] FetchList
  {
    get { return new[] { "Categories.Products" }; }
  }
}

Now, it would be nice if I could get an easy access to those fetching strategies … well, that’s what you use an IoC container for ;) Here is the configuration of Castle’s Windsor container in NServiceBus endpoint’s configuration class:

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
  public void Init()
  {
    var container = new WindsorContainer(new XmlInterpreter());

    Configure.With()
      .CastleWindsorBuilder(container)
      .XmlSerializer();

    container.Register(
      AllTypes
        .Of(typeof(IFetchingStrategy<>))
        .Pick(Configure.TypesToScan)
        .WithService.FirstInterface()
    );
  }
}

Fluent configuration for the Bus & the Container ! Woohoo, my head is turning ! :D

Now, for the final part, the MessageHandler that makes use of all those cuties & with the help some little extension methods:

public class ProductManager : IHandleMessages<RegisterProductInCatalogRequest>
{
  public virtual IBus Bus { get; set; }
  public virtual ISessionManager SessionManager { get; set; }
  public virtual IFetchingStrategy<IRegisterProductInCatalog> Strategy { get; set; }

  public virtual void Handle(RegisterProductInCatalogRequest message)
  {
    using (var session = SessionManager.OpenSession())
    {
      var catalog =
            session.For<Catalog>(message.CatalogId)
                   .Apply(Strategy)
                   .UniqueResult<IRegisterProductInCatalog>();

      catalog.RegisterProduct(message.CategoryName,
                              message.ProductName,
                              message.ProductDescription);
    }
  }
}

I don’t know you, but that’s the kind of code that makes me happy (like a hippo). :)

Comments, feedback, suggestions are all welcome ! ;)

Coarse-grained Lock & ORM : Followup

May 31st, 2009 stiiifff No comments

Ayende has a followup post concerning support for Coarse-grained locks in NHibernate.

All I want to be able to do is to define my coarse-grained locks (=> optimistic-lock on aggregate root) in NHibernate’s mapping files … and then forget about it when I know that NHibernate can handle it for me. :)

I will then be confident that changes performed within an aggregate are done in a consistent manner and that concurrency problems are detected without using expensive database locks but using optimistic locking on just 1 table for the whole aggregate.

Categories: DDD, ORM, Patterns Tags: , ,

Entity Framework => Entity Base Class Library

November 23rd, 2008 stiiifff 5 comments

A few days ago the Entity Framework Team released a proposed solution for N-Tiers change tracking on entities … or should I call it a no-solution?

Basically, they will provide a low-level API that will allow YOU to do change-tracking the way YOU prefer.

5 months ago, I was writing a big NO to the Vote of no confidence. Well, now that I had to use Entity Framework on a real project, I must say that I was wrong. I really tried my best using it, thinking that it would be a safe bet for future evolution … and I know what I'm talking about when using ORMs, I'm a very advanced user of NHibernate (multi-tenancy, dynamic filters, dynamic mappings, partial domain models anyone?) … but my only conclusion to the Entity Framework is : WTF are they doing ?! Is that all you could come up with after months of research, brainstormings, experts meetings ?!

If someone give me a set of classes that doesn't bring a A to Z solution to a problem, sorry but I don't call it a Framework … I call it a Base Class Library. I've been vey supportive to the Entity Framework Team (I gave design feedback through multiple channels) but now I think I'm done. And it seems that others have the same feeling : Frans Bouma, author of the LLBGenPro ORM tool, goes in that direction too with its answer to the original post, and a full post on his blog, wich I both totally agree with.

I mean, come on, all those abstractions and discussions are very interesting but after some extensive periods of time trying to make this thing become useable, you have to ask yourself some serious questions. Even the now defunct Linq2Sql is more pleasant & easier to use.

Anyway, I will now go back using something that is customizable, extensible, that allow me to work the way I want (Domain-Driven Design) and most importantly that actually works : NHibernate. I recently discovered that it's moving full speed to version 2.1 with great new features such as Entity Modes : see here and there to know what I'm talking about. Also, a great workshop video on advanced use cases of NHibernate by Ayende.

Update: Ayende's reaction to the Entity Framework Team's design post.

Distributed Domain-Driven Design

July 15th, 2008 stiiifff No comments

Yves Goeleven, a fellow Belgian .Net architect, has a series of very interesting posts on Distributed Domain-Driven Design (DDDD).

If you're researching about how to design your applications with DDD in mind while still being highly scalable, this paper from Pat Helland (Amazon) is a great source of inspiration (thanks Yves for the link). But don't expect to grasp the full potential of it if you don't have a good understanding of both DDD & Distributed Systems (Entities, Transactions, Workflows, Messaging & Scalability in a SOA world).

Coarse-grained Lock & ORM : Not Supported ?

July 2nd, 2008 stiiifff No comments

A while ago, I tried to apply a coarse-grained lock using NHibernate. I didn’t succeed, although I tried several options. This is a pity as I consider this pattern essential to be able to develop real-world applications (not just pet projects) using DDD. Indeed, how do you handle concurrency on your aggregates if you cannot use a coarse-grained lock ?.

More practically described, your ORM must be able to mark the aggregate root as dirty if one of its (direct or indirect) child is dirty, even though the root itself is not dirty … which implies that the ORM, or at least its change-tracking algorithm, must understand the concept of Aggregate (or let you manually configure all the ‘cascades‘).

By the way, I’m not the only one who tried: check here and there. Unfortunately, up to now, I wasn’t able to find an ORM that natively such a locking pattern … I would be very happy if someone could prove me wrong. :)

Note: I actually had a very good implementation of it working on a project I was working on, more than 1 year ago … It used a custom abstract persistence layer sitting on top of NHibernate. To get this to work I had to write the following:

  • Custom Change-Tracking algorithm.
  • NHibernate Interceptor to plug in the custom Change Tracking algorithm (bypass the default one from NHibernate).

Digressing a bit from the main subject of the post, they are other advanced functionalities of NHibernate that can easily be customized:

Categories: DDD, ORM, Patterns Tags: , ,

DDD Specifications

June 24th, 2008 stiiifff 4 comments

Specifications is an interesting pattern as it allows reuse of expressions on entities for filtering, querying, validation … going further, it could also be used to present users with smart composite filters in the UI (instead of the usual 'type a filter value for this field').

I was thinking about writing a lenghty post on specifications but others have done it, and probably much better than I could, so … I'm just going to gather some links for you to dig deeper in what specifications are and what they can be used for.

Ok, the Specification pattern looks good … but is it really usefull ? Well, yes ;) If you combine it with a Linq-friendly Repository, it is indeed quite usefull:

SpecificationModel

SpecificationModel

Let me describe the various components that appear on this diagram:

  • ISpecification<TEntity>: This is the base interface for Specification. It has one method 'IsSatisfiedBy' that takes an entity (a class without any other specific constraint) as argument and return a bool indicating if the entity matches the specification or not.
  • ILambdaSpecification<TEntity>: A specialized base interface for Specifications expressed as a Lambda expression. It has members returning the Lambda expression, an equivalent Predicate and a String representation.
  • LambdaSpecification<TEntity>: The implementation class of ILambdaSpecification<TEntity>. Contains implicit conversion operators from & to an untyped Lambda expression.
  • IRepository<TEntity>: The base interface for a Linq-friendly repository … and as Specifications can be expressed as Lambda expressios (= ILambdaSpecification<TEntity>), they can also be used as query filters on the Repository.
  • LambdaSpecificationExtensions: Extension method that allows conversion of a Lambda expression to a ILambdaSpecification<TEntity>.

That's just the base model for Specifications and the link with Repositories. Other aspects such as specification compositions must be taken into account, Linq supporting natively some of the necessary capabilities. Advanced specification concepts such as Subsumtion & Partially satisfied specifications can be implemented if needed only (don't make things more complex than they need to be for your project).

I hope this little introduction on specifications gave you an idea of the level of expressivenes your domain model can reach when using named specifications & specifications-friendly repositories !!! ;)

Categories: DDD, Patterns Tags: , , , ,

Ado.Net Entity Framework – Vote of no confidence – NO !

June 24th, 2008 stiiifff No comments

Just stumbled on the new soap opera of summer 2008: the so-called Ado.Net Entity Framework Vote of No Confidence (that I will nicely abbreviate ANEFVONC). Come on, we have Euro 2008 Cup, the Olympic Games in China … isn't that enough ? ;)

The open letter describes the main problems that, following the Alt.Net community, currently prevent the Entity Framework from being a great DDD Tool:

  • INORDINATE FOCUS THE DATA ASPECT OF ENTITIES LEADS TO DEGRADED ENTITY ARCHITECTURES: Yes, EF is currently mainly about data access … let's hope it will evolve and introduce more & more 'Entity services'. Concerning Business Rules, is there a standard way of expressing Business Rules that EF could include ? Not sure about that but anyway I don't believe that EF currently prevents you to define your BR … because in a system with numerous & complex BR, your BR logic wouldn't be coded right inside your Entities. ;)
  • EXCESS CODE NEEDED TO DEAL WITH LACK OF LAZY LOADING: I remember EntitySet<T> has Lazy Loading built-in … but yeah, then you're not 100% PI compliant anymore. And no, I don't need Lazy Loading to keep my business logic free of Data Access concerns. I can use specific Repositories that will contain custom Linq queries if my base Linq-generic Repository is not enough. Lazy Loading is very appeling at first, but frankly, is only applicable to simple cases. 'Transparent data access' is a myth … you need to express a query in some form. Defining relations in order to benefit from Lazy Loading is a very simplistic view of the task of Querying on a Domain Model.
  • SHARED, CANONICAL MODEL CONTRADICTS SOFTWARE BEST PRACTICES: Haven't tested EF on that point … but indeed, support for Partial Models (see here, and here) is needed for complex Domain Models (especially when split in many Modules).
  • LACK OF PERSISTENCE IGNORANCE CAUSES BUSINESS LOGIC TO BE HARDER TO
    READ, WRITE, AND MODIFY, CAUSING DEVELOPMENT AND MAINTENANCE COSTS TO
    INCREASE AT AN EXAGGERATED RATE
    : Last time I checked, the EF Team had written a post on its blog to express its commitment to making EF as close to PI as possible … did they changed their mind ? Or is the Alt.Net community not patient enough? lol :)
  • EXCESSIVE MERGE CONFLICTS WITH SOURCE CONTROL IN TEAM ENVIRONMENTS: Well, let's hope it can be fixed.

Although, I think I understand most of the concerns expressed in the letter, I still feel that it focus too much on the data access part of Domain Driven Design … and DDD is so much more than that, right? IMHO, here are some key problems that must be adressed for DDD to hit mainstream:

  • Bridge the gap between DDD & MDD: I want to do DDD, but I don't want to know 100 patterns and tricks and tips in order to work efficiently … Gotta focus on defining the Ubiquitous Language right ? ;) What we need is a Meta-Model for DDD that allow us to create our Domain Models more easily than with Code, Unit Tests, Refactoring … I want to work at a higher level of abstraction, please. Give me DDD domain-specific languages !!! (a textual one for purists and a graphical one for realists, both being SCM-compliants).
  • DDD Best Practices Validation Rules: Create Generic DDD-specific rules that can be verified on the Domain Model created using the DDD DSL. Allow to define Domain Model-specific rules (= Business Rules) directly at the Model level, using a Business Rules DSL.
  • ORM-free DDD Code Generation: Well, if my first wish is realized (DDD DSL), then generate the code for me based on what I have described using the DSL. One way code generation would be fine (if they are enough extension points otherwise, we are back to square one with open letters and blah blah). Bi-Directional DSL would be great, but much much harder. Also, make the code generator pluggable … so that, any ORM-specific artefacts can be generated (keep the core DDD DSL free of any ORM-specifics). Also, generate Business Rules code.
  • Relationship as first-class elements of the language: let's get rid of those horrible Add / Remove methods hiding the fact that we are still not able to manage bi-directional relationships correctly & consistently. Make the concept of relationship an integral part of the language (Linq 2.0 ?). A great article on the subject (in french, sorry).

Other interesting (old !) posts that also shed light on the fact that DDD still has a long way to go: here and there.

Personnally, I think that EF is the first building block for more DDD goodness to come … so, give feedback, but be patient. I believe an annoucement like this one can only bring good things. Like NHibernate, Rome wasn't built in just one day.

Or did I miss something ? :)

Reminder: Never a sign a paper just because a lot of other people did it before you. ;)