Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Architectural question

As a result of a previous post (Architecture: simple CQS) I've been thinking how I could build a simple system that is flexible enough to be extended later.

In other words: I don't see the need for a full-blown CQRS now, but I want it to be easy to evolve to it later, if needed.

So I was thinking to separate commanding from querying, but both based on the same database.

The query part would be easy: a WCF data service based on views to that it's easy to query for data. Nothing special there.

The command part is something more difficult, and here's an idea: commands are of course executed in an asynchronous way, so they don't return a result. But, my ASP.NET MVC site's controllers often need feedback from a command (for example if a registration of a member succeeded or not). So if the controller sends a command, it also generates a transaction ID (a guid) that is passed together with the command properties. The command service receives this command, puts it into a transactions table in the database with state 'processing', and is executed (using DDD principles). After execution, the transactions table is updated, so that state becomes 'completed' or 'failed', and other more detailed information like the primary key that was generated.

Meanwhile the site is using the QueryService to poll for the state of this transaction, until it receives 'completed' or 'failed', and then it can continue its work based on this result. If the transactions table is polled and the result was 'completed' or 'failed', the entry is deleted.

A side effect is that I don't need guid's as keys for my entities, which is a good thing for performance and size.

In most cases this polling mechanism is probably not needed, but is possible if needed. And the interfaces are designed with CQS in mind, so open for the future.

Do you think of any flaws in this approach? Other ideas or suggestions?

Thanks!

Lud

like image 453
L-Four Avatar asked Aug 11 '11 19:08

L-Four


People also ask

What questions does an architect ask a client?

Do you have any sustainability goals for the project? Are there any views on the site that are particularly important to you? What kind of ideas do you have for design or materials? Do you have any images from magazines or Pinterest that show us a style that you like?

What is special about architecture?

Architecture connects to economics and the sciences, and the people that practice it can both be detail-oriented technicians (solving equations that push buildings higher into the sky, or conserving every possible electron of electricity pumped into its walls), and poets of space and form.


1 Answers

I think you are very close to a full CQRS system with your approach.

I have a site that I used to do something similar to what you are describing. My site, braincredits.com, is architected using CQRS, and all commands are async in nature. So, as a result, when I create an entry, there is really no feedback to the user other than the command was successfully submitted for processing (not that it processed).

But I have a user score on the site (a count of their "credits") that should change as the user submits more items. But I don't want the user to keep hitting F5 to refresh the browser. So I am doing what you are proposing -- I have an AJAX call that fires off every second or two to see if the user's credit count has changed. If it has, the new amount is brought back and the UI is updated (with a little bit of animation to catch the user's attention -- but not too flashy).

What you're talking about is eventual consistency -- that the state of the application that the user is seeing will eventually be consistent with the system data (the system of record). That concept is pretty key to CQRS, and, in my opinion, makes a lot of sense. As soon as you retrieve data in a system (whether it's a CQRS-based one or not), the data is old. But if you assume that and assume that the client will eventually be consistent, then your approach makes sense and you can also design your UI to account for that AND take advantage of that.

As far as suggestions, I would watch how much polling you do and how much data you're sending up and back. Do go overboard with polling, which is sounds like you're not. But target what should be updated on a regular basis on your site and I think you'll be good.

The WCF Data Service layer for the query side is a good idea - just make sure it's only read-enabled (which I'm sure you've done).

Other than that, it sounds like you're off to a good start.

I hope this helps. Good luck!

like image 140
David Hoerster Avatar answered Sep 27 '22 23:09

David Hoerster