Jan 5 2012

Practicing ‘Agile’ Doesn’t Necessarily Make You ‘agile’

Greg Williams from Atomic Object offers some helpful insights regarding the "agile" movement.

Practicing ‘Agile’ Doesn’t Necessarily Make You ‘agile’

Tags: ,

Jun 8 2011

Whatever interests you naturally is the most important thing to work on

Brandon Durham explains why:

http://37signals.com/svn/posts/2922-whatever-interests-you-naturally-is-the-most-important-thing-to-work-on-

Tags: ,

Jun 8 2011

Process kills developer passion

James Turner explains the importance of being truly agile, rather than making your team beholden to process for its own sake.

http://radar.oreilly.com/2011/05/process-kills-developer-passion.html

Tags: , , , ,

Mar 20 2011

How CQRS can make it easier to discuss architecture

Category: Software DevelopmentJeff @ 11:33

My friend Scott has put down some great thoughts in his recent post, Why is discussing architecture so hard? What started as a simple response turned into a full blown blog post on CQRS. The following are my thoughts on how CQRS can provide some basic initial structure for a project.

~~~~~~~~~~~~~~~~~~~~~

An approach that's working well for us is adoption of CQRS (Command Query Responsibility Segregation) and EDA (Event Driven Architecture).  The basic separation of writes from reads leads to some interesting architectural options.  Also, the benefits of messaging based systems have been well documented. Pertaining to the present discussion, I want to emphasize two things: 1) how these concepts provide simple architectural structure from the get-go, and 2) how adopting this approach can help developers to leverage their existing expertise and experience with fewer derailing discussions about “over architecting.”  

We define contracts in three areas of the system:

  1. Commands
  2. Events
  3. Projections

*Commands* are messages which represent method calls on domain objects.  Client code (such as the UI) does not directly call or reference domain objects.  Instead, client code creates commands and submits them to a command processor.  This Command Processor validates the command, and then dispatches the command to a Command Handler.  A typical command handler retrieves a Domain object from a Domain Repository, and then maps the Command to a method call on the Domain object.  Note: we typically call these “Domain Commands” to avoid potential naming collisions.

*Events* are generated based on changes to objects in the Domain.  Our goal is a fully encapsulated Domain with well defined Bounded Contexts (Aggregate Roots).  The Aggregate Roots expose Domain Events which are persisted in an Event Source.  Then, a Message Publisher dispatches these Events to Event Handlers which typically create Projections (though they could also perform other tasks such as notification).  Note: we typically call these “Domain Events” to avoid potential naming collisions.

*Projections* are created by Event Handlers in reponse to Domain Events. These may be referred to as "reports" or "read models" or even "view models" (though not in the MVVM sense).  Projections are retrieved using a read-only reporting repository.  We typically generate one projection per UI screen element.  Though at times this may seem to lead to duplication when projections look similar to one another, remember that the Single Responsibility Principle is about a class having one reason to change.  Since separate UI screens may change for different reasons, we want to maintain this separation in our Projections as well.

With these three contracts in place, we have our “ridge beams” in place and things progress in the following fashion:

  • UI - Projections IN, Commands OUT
  • Domain - Commands IN, Events OUT
  • Message Publisher - Events IN, Projections OUT

UI developers use a Reporting Repository to query for Projections.  Screens are built up from these Projections for end user interaction.  User interaction leads to the creation of Commands which are submitted to the Command Processor.

Domain developers focus on modelling the business requirements in a truly Domain Driven fashion.  Business operations are methods on domain objects, dispatched to the Domain as Commands.  While state is fully encapsulated in the domain objects, they expose Domain Events which reveal their state after a mutating method call has completed successfully.  (And by fully encapsulated, I mean fully encapsulated - no property getters or setters).

I don’t have clever name for the developers who create the Event Handlers which take Domain Events and generate Projections.  This is a fairly trivial development effort, though critical of course since the UI will rely on the generated Projections in order to display system state to end users.

So we’ve come full circle in this very low detail, high altitude overview.  There is much more to discuss regarding things like asynchronous messaging, eventual consistency, event sourcing, relational vs. NoSql solutions, etc.  However, you can start building a system using these three simple contracts (“ridge beams”) without committing to major architectural choices too early in your development life cycle.  

Tags: , , , , , , ,

Mar 4 2011

CQRS Overview Episode 3 - Give Your Domain a Break

Category: Software DevelopmentJeff @ 05:32

In the last episode in this series, I delved into the importance of valuing behavior rather than data as of primary importance when modeling a business process in a software application. This imperative approach involves defining Commands which represent the behaviors which can be performed in the domain model. Outside the domain, commands are simple message objects which are dispatched to the domain for processing. In the domain itself, a command is simply a method on an Aggregate Root or Entity within the context of the domain. Keep in mind that commands represent an imperative - the client code tells a domain object to do something. While the domain object may say "yes" or "no", it does not answer any other questions since this would represent a query, violating the separation of commands and queries inherent to the CQRS approach.

Okay, so we're ready to move past a data-centric approach and laser focus on behavior. We'll make some classes called "Entities" and we'll put a bunch of properties and methods on these classes. Then we'll use a mapping tool such as an ORM to map these Entities onto a database schema for the sake of persistence. At first glance this appears to be a marked improvement over a procedural approach. However, it fails to take full advantage of some critical benefits of truly object oriented design. There are many issues which could be addressed regarding the burdens put on such a beast which so often passes for a "domain model." I'll focus on three in particular.

First, mapping entities onto a database schema invariably leads to leaky abstractions. Even when the database is built from the domain model, persistence specific concerns often leak into the domain objects. Some have made monumental attempts to prevent entities from having knowledge of the fact that they are to be persisted to a database. Others have embraced persistence concerns in the domain as with the Active Record pattern. But with domain objects so tightly related to database schemas, aren't we still focused on data? Isn't the proverbial database-tail still wagging the domain-dog? Must such compromises be made in the name of expediency or sticking with familiar tools and approaches?

Second, in the context of a domain model, the benefits of encapsulation can hardly be overstated. Put in simpler terms, property getters are evil. "But wait," you say, "how can a system even function if I have no exposed properties from which to determine the state of objects in my application?" To be clear, this requires a fairly drastic mind shift, especially for developers who are used to frameworks and tools full of property getters and setters (WinForms, WPF, Entity Framework, ASP.NET etc.). But such frameworks and tools by definition do not represent a business domain.  Yet we often use such code as an example of "good code" for any and all circumstances.  While such code may fit the context of a framework or toolkit, it has no place in a true, object-oriented domain model.

[Side bar on encapsulation for over achievers]

Developers who are used to exposing property getters on aggregates or entities often struggle to get their heads around such radical encapsulation. How can a class be useful when its innards are isolated from contact with the outside world? Aha! This is, in fact, a misnomer. It's not that such a class has no contact with the outside world, but that instead it maintains control over the way in which it communicates with the world outside its bounded context. In Episode 6 I will go into more detail regarding Domain Events, but for now the basic concept boils down to a simple idea - your domain classes should not allow direct manipulation of, or direct exposure to, their inner state. Instead, they will provide access to--or publish--events which represent the internal state changes which have occurred within their encapsulated boundary.

In case you missed it before, I did in fact say that getters are evil. While setters can be evil in their own right, many developers believe they are achieving encapsulation when they allow for public getters but use private setters. This simply does not count as encapsulation. Any time you directly expose the internal state of a domain object, you increase the fragility of the overall system. Exposed property getters make domain objects difficult to change. It also can cause rippling effects when such changes radiate out to all other classes which directly depend on the domain object. One more thing before we move on - the incessant need to check the state of objects through properties is symptomatic of the data focus creeping back into a place where we ought to be focused on behavior.

Finally, when we ask our domain to fulfill multiple roles, we violate the Single Responsibility Principle. What many developers refer to as a domain model fulfills multiple roles - enforcing referential constraints, performing data validation, invoking business logic, answering query requests, etc. These are not minor infractions of SRP but full blown felonies. In contrast, domain driven design principles allow us to create a true domain model which performs a singular function - representing the business domain in terms of behavior and messages. Domain objects perform commands, while keeping their internal state hidden from the client code. This allows for ideal levels of cohesion and greatly simplifies the domain objects, allowing them to focus on implementing the business specific behavior of the system.

How do you give your domain a break? You remove any and all direct or indirect reliance on the leaky abstractions of persistence from creeping into your domain model. You radically conform to the basic object oriented principle of encapsulation, avoiding all getters and setters, and only exposing command methods on your domain objects. Finally, you remember to adhere to the Single Responsibility Principle all along the way. Tempted to make that domain object fulfill multiple responsibilities? Something's not right, so take the time to think about what's wrong and what you can do about it besides exposing internal state to client code.

What if leaky abstractions could be completely removed from domain objects, not for the sake of some purist's notion of Persistence Ignorance, but for the sake of true OOP goodness, and all the benefits thereof? What if domain objects could focus on behavior and become freed from querying concerns? What if objects in your model fell within the parameters of the SRP? You'd have a far more maintainable and comprehensible system. In addition, you'd open yourself up to intriguing characterstics which would be extremely difficult to attain in a typical ORM-backed modelling approach. In the next episode, Beyond CAP, we'll dig deeper into one particularly interesting characteristic which becomes an option when working with a truly encapsulated domain - Eventual consistency. See you next time!

Further reading:

Eric Evans

Allen Holub

Greg Young

Jonathan Oliver

 

Tags: , , , ,

Jan 30 2011

Lots of Changes going from .NET to iPad development

Category: Software DevelopmentJeff @ 07:00

I've been away a lot lately, but have been exploring some fun new territory.  In particular, I've been working on Enterprise development for the iPad.  It's been quite a mental shift to move from

Windows -> Mac

C# -> Objective C

.NET -> unmanaged code

Desktop/Web/small-device dev -> large touch screen mobile dev

Visual Studio -> XCode

Code driven UI design -> Interface Builder

The pain points and frustrations have been numerous, though the resulting applications are beautiful and pleasant.  I miss the comfort of my original development experience, but appreciate the superior elegance that seems achievable in iOS.  There's a whole lot more to say on this, which may perhaps spur more blog posts in the near future.

In the mean time, I'm still a .NET developer and don't see that changing any time soon.  But adding iPad apps to my quiver of options has been an interesting experience so far. More to come soon.

Tags: , , , , ,

Nov 7 2010

CQRS Overview Episode 2 - It's Imperative!

Category: Software DevelopmentJeff @ 06:14

[This is the second episode in my CQRS Overview Series - Series Contents]

First, some full disclosure.  I started my career developing applications using Microsoft Access and VBA with SQL Server backend databases (yes, I know! but please keep reading, we all had to start somewhere...).  Such a stack (if this can even be called a "stack") naturally led to a completely data-centric approach to writing applications.  Object orientation wasn't even a distant thought in the mind of this business school graduate.  My world consisted of presenting data entry screens to users, possibly validating or manipulating the data, shoving the data into a table, and then reporting on the data.  Views and Stored Procedures were great ways to aggregate, filter, and slice and dice data.  Data, data, data.  You get the idea.  (Side note - I had clients who loved the fact that they could just directly manipulate data in tables, bypassing all constraints, logic, validation, etc.  Just imagine how hard it is to extend and maintain, let alone troubleshoot and debug such a system!).

"Neo, the matrix has you!"

Since I had never learned object oriented design principles, I was unaware of other approaches.  But like Neo in the Matrix, at some point I sensed a splinter in my mind, driving me mad.  One of my first interesting realizations came about when I noticed that many rules and processes of the business did not exist anywhere in the data-centric software. Instead, someone just had to know the process, or a document was created describing the process.  Rather than building such logic into the application, a data centric application seemed to lend itself to leaving gaping holes, left to be patched by super human feats of memory ("remind me, how do I do this again?"), copious amounts of ever-stale documentation ("is this standard operating procedure up to date?"), or just plain dumb luck.  Or things would just break and programmers could blame the users and call it a "data" problem ("the software isn't bad, it's a data entry problem.  Bad user!").

When the .NET framework was released in 2002, I jumped on that bandwagon and also began learning object oriented practices.  However, it proved hard to break the data-centric habit and it continued in force for a time.  This was due to many factors, not the least of which being Microsoft's propensity to use this approach in many of their code samples and help documentation.  For those who didn't know any better, the assumption was that the Microsoft way must be the "right" way. After all, they created the framework and the tools, so their code samples must represent the correct way to do things, right?

Wrong.  In so many ways, wrong.  Rather than talking about all the ways this is wrong, I want to delve into one particular area.  Get ready to make a mental shift. You see, it's not about data.  This was a stunning realization for me, and many still struggle to accept this as true.  Even many who realize that data should not be the focus continue to design systems, tools and frameworks that keep data at the core.  Even frameworks that focus on creating things like "business entities" with "business logic" continue to put data in the center of the universe.  Developers add a few "methods" to some "entities" and call it object oriented.  But, the Copernican revolution has already occurred.  Data is not the center of the universe.  Software development should not revolve around data.  It should instead focus on messages and behavior.

"Hang on now, Jeff.  Businesses really do care about data.  And a good software developer wants to make the business happy.  So you're just dead wrong.  It really is all about the data."

I'm not saying that data doesn't matter.  I am saying that data shouldn't be the center of our focus.  Things like Invoices and Purchase Orders and Ledgers certainly have characteristics which must be represented as data.  But there are far more interesting things going on in the business than just dates, currencies, strings and integers.  Think of all the things a business might do with something as simple as an invoice.  The business will create invoices.  They might submit them.  They might approve them or reject them. On occasion they might, perchance, adjust them or cancel them or combine them.

Let's step back to the data-centric systems approach I started with.  In those days, I would have created an "Invoice" screen and a few supporting tables, queries, and reports.  If the client wanted to be able to cancel an invoice, I probably would have just added a check box to the screen.  If there was logic around whether or not an invoice could be cancelled, I would have to find a way to enforce that logic before the data got saved to a database (most likely in a procedural fashion).  If the client wanted to adjust an invoice, they would just open the screen and do so, but no one would know specifically what was adjusted or why the adjustment was made.  Many, many business processes remained "offline" from the perspective of the software system.  It just provided a conduit for shoving data, with some forms of validation, and then regurgitating it later in some aggregated form like a report or query.

"It's Imperative!  Do This!"

Instead, imagine telling the software application what to do.  "Computer, create an invoice" or "computer, cancel this invoice" or "computer, approve this invoice" (no, I'm not a Trekkie, but I know a few).  Certain data points will of course be captured along the way, but the user's intent becomes quite clear now as he or she interacts with the application. Instead of asking the system for a pile of data and then manipulating it accordingly, the user takes more discreet, fine-grained steps that model the reality of the business. The system becomes task oriented, message based and coherently descriptive of real business processes.  Users submit commands to the software system, rather than plain data.

Consider just a few of the major improvements with such an approach.  I've already mentioned one - the software begins to represent the reality of the business. Another benefit comes in the fact that developers begin to speak the same language as the software users.  Instead of just gathering a bunch of data types with names like "InvoiceDate" or "Amount," developers learn the operational verbs used by the business.  This can provide a healthy improvement to communication.  So often developers want business people to speak their language, but high value systems can be built when developers learn to speak the language of the business instead.

While there are many other improvements due to this imperative approach, a final one worth mentioning comes from the fact that we are now sending meaningful messages between layers or components of our application and architecture, rather than piles of data. I will tease out multiple benefits of such a message based approach as this series continues. For now, just remember that it's not about the data.  It's about the command.  And the command is imperative.

See you next time when I'll tell you why you need to give your Domain a Break!

Tags: , ,

Sep 8 2010

CQRS Overview Episode 1 - Why Should I Care About CQRS?

Category: Software DevelopmentJeff @ 17:00

[This is the first episode in my CQRS Overview Series - Series Contents]

Is it worth my time even thinking about CQRS, much less dedicating precious time and energy to understanding it? In this post, I hope to demonstrate two things: first that CQRS consists of simple concepts, and second, that it is certainly worth your time to understand these basic concepts.  You may still choose to only adopt certain approaches and techniques made possible by CQRS.  However, by understanding CQRS you will discover new ways of thinking about requirements gathering, design, architecture and implementation.  These thinking processes provide benefits even if you don't implement every possible recommendation or option you read about here or in other sources of information about CQRS.

CQRS (Command Query Responsibility Segregation) grew out of a concept called CQS (Command Query Separation).  Bertrand Meyer coined the term CQS which is defined this way: "every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer" (Wikipedia/Command-query separation).  

CQS provides a basic separation of concerns in which any method will either modify state, or return data.  Methods following the CQS principle communicate context through the asking one simple question - is this method a Command or a Query?  Methods which try to fulfill both roles are often brittle, long, complex, difficult to change, or hard to test (or a combination of many of these factors).  By separating our commands and queries, we take a large step toward preventing these problems from entering our code base.

Stop for a moment to ponder the simplicity of CQS.  All we are doing is thinking about whether a method is going to do something, or return some data.  You can hardly get much more dirt simple than that.  This is a fundamental and simple principle, but with profound implications.  This becomes especially true when we extend the CQS concept beyond the method level and apply it generally to the design of the whole system.  

CQRS makes this leap from a method level to a system level.  A distinction is made between Commands which tell the system to take an action, and Queries which ask the system for information.  This approach helps at the conceptual level when thinking about the design of a system.  By separating these concerns, your mental process can take on a more fine grained approach to designing the system.  It helps you to begin speaking the language of the business. Commands represent actions taken in actual business processes. Queries represent data as it is presented in actual business scenarios. Developers start thinking about the application in business terms, rather than expecting business people to adopt the technical language of developers.

In summary, CQRS comes down to a simple concept, but with extensive implications. In the next article in this series, I'll dive more deeply into the specifics of how Commands bring power, elegance and simplicity to the design and development process.  Stay tuned.

Tags: , ,

Aug 31 2010

CQRS Overview - Series Introduction

Category: Software DevelopmentJeff @ 14:35

Command Query Responsibility Segregation (CQRS) has attracted a lot of attention recently.  This post kicks off a series of blog posts that will describe my experiences learning CQRS, including potential benefits and possible pitfalls.

In order to determine the potential of this approach, I have adopted CQRS for an in-house development project.  I do not currently have any production code that uses this pattern extensively, though I have long been a proponent of Domain Driven Design (DDD) and Agile programming practices.  I plan to use the lessons learned to guide future decisions about whether to fully adopt CQRS, implement portions of it, derive concepts from it, or run the other way screaming.  :)

I look forward to your questions, comments, suggestions and critical feedback. It should be a wild ride.

Tags: , ,

Aug 30 2010

CQRS Series from Mark Nijhof

Category: Software DevelopmentJeff @ 17:27

 

Learning CQRS?  A while back, Mark Nijhof posted a helpful series of blog posts along with an example project.  It's a great place to start for understanding the concepts and implementation details of Command Query Responsbility Separation.  If you've been on the CQRS bandwagon for a while, this is old news for you.  But for more recent adopters, you'll find this to be a helpful introduction.

 

Code Sample - http://github.com/MarkNijhof/Fohjin

Video Walkthrough of the Code Sample - http://vimeo.com/7838858

 

CQRS à la Greg Young - http://cre8ivethought.com/blog/2009/11/12/cqrs--la-greg-young/

CQRS Domain Events - http://cre8ivethought.com/blog/2009/11/20/cqrs-domain-events/

CQRS Domain State - http://cre8ivethought.com/blog/2009/12/08/cqrs-domain-state/

Specifications - http://cre8ivethought.com/blog/2009/12/22/specifications/

CQRS Event Sourcing - http://cre8ivethought.com/blog/2010/02/05/cqrs-event-sourcing/

CQRS Event Versioning - http://cre8ivethought.com/blog/2010/02/09/cqrs-event-versioning/

CQRS Scalability - http://cre8ivethought.com/blog/2010/02/09/cqrs-scalability/

Using conventions with Passive View - http://cre8ivethought.com/blog/2009/12/19/using-conventions-with-passive-view/

 

 

Tags: , , ,