Tuesday, 29 July 2014

I Need to Stop Misusing Namespaces

At the recent NSBCon one interesting question that came about was how to structure a project. The panel consisting of various speakers had no answer, after all this is dependant upon the project in question. Therefore there is no right or wrong answer.

However one point they were in unison about was splitting the domain and technical implementation of a project apart by the correct use of in namespaces.

This is not the first time I've come across this, but I find myself breaking this principle on a regular basis. For example a typical project I work on looks like the following.

Problems

  • The namespace reflects a technical implementation detail, and not the problem domain.
  • Using Foo as an example, here the namespace is duplicated within the name of the types, which in turn defeats the point of namespaces.
  • Another issue is that the types can be much longer than they need to be, which is often a criticism of enterprise software development, when the names of objects roll off the screen because they contain so many patterns or conventions.

Solution

Use namespaces for related domain responsibilities. In turn, group together the objects and types that are used together.

An example of a better solution therefore would be:

Benefits
  • Things that change at the same rate, would live logically next to things that also need changes. In other words if I update the FooViewModel, chances are I'll need to update views or controllers.
  • Less typing if you don't suffer a namespace clash!
  • You can still prefix the namespace where required, e.g. Foo.Controller if you have a clash or prefer the readability.
  • Shorter type names!

While this is the ideal way of structuring our applications it's not always possible. Some coding conventions actually encourage the first example, and depending on the configurability of certain frameworks this may prove difficult. That aside, I'll be making a strong push towards structuring my projects correctly going forwards.

Monday, 21 July 2014

SOA Done Badly vs SOA Done Right

I was under the assumption I had been doing SOA for over 3 years. Previously I have had services which did stuff, these talked to other services which did other stuff and so on. We would group services around functionality.

We would break these services down if they got too big to handle. Behind the scenes the services would talk to a single database. When it came to creating an application or system, these front end applications would invoke various services, which in turn invoked other services. This layered style of architecture worked for a while, so everything appeared to be fine.

The overall architecture looked like this:

Bad example of SOA

Over time I began to question the benefit of this style.

  • I spent more time coding than solving business problems
  • I spent more time debugging than solving problems
  • I spent more time fixing broken deploys than solving problems
  • I spent more time writing infrastructure (to glue services together) than solving problems

It turns out this is actually quite a common style to SOA. But there are some serious flaws with this layered approach.

Problems

  • If a service fails, the whole system is pretty much broken.
  • If the database breaks, the whole system is pretty much broken.
  • If a service is slow or breaks SLA, the whole system is pretty much broken.
  • In order to decouple between services, you need complex, costly abstractions.

Solution

A solution to solve these problems is to partition the domain boundaries, vertically. The overall architecture would look like this:

Good example of SOA
Details
  • Each domain could consist of one or more "services".
  • Each with their own choice of data store, e.g. SQL, NOSQL, file system etc...
  • No domain can directly communicate with another domain.
  • If they do need to communicate, then the pub/sub model would be used or an ansyc command could be issued. Think .NET events or the event design pattern but across processes, not objects.
Benefits
  • Each service could fail and the impact would be minimal, simply rollback the deploy or fix the problem. Once the service comes back to life any commands or events that have not yet been processed would be handled. Each service would have their own message queues to persist commands/events.
  • Services could be re-wrote and distributed easily, proving they are highly decoupled.
  • No complex infrastructure, e.g. no need to map between objects because the service owns the whole stack. The same model could be used across partitions for example.
  • Works really well with agile development methodologies, e.g. vertical slicing of stories.

There is more to this introduction. Microservices are a hot new topic. Being defined as "SOA done correctly". Using the second example, within each domain there could be multiple services (or autonomous components) that coexist. The likes of UI composition to enable applications to be created from services is another great benefit. This enables so called "mashups".

I'll expand on these topics over time, but I am certain this method of vertically slicing services based upon business capabilities will be my default approach going forward.