CQRS - whats all the fuss about?

Sun Aug 21 2011 by MarkP

If you haven't heard of CQRS - where have you been for the last year to six months? In the .NET world this pattern has been the lastest buzzword that will solve a multitude of issues (apparently) in relation to architectural and software design that has grown out of n-tier architecture. Its premise is to simplify the complexity inherent now in most systems based on this architecture especially when changes are required that tend to impact code in all the various layers. It also faciliates best practice (Single Responsibility Principle) and should lead to better performing systems.

CQRS - is an acronym for Command Query Responsibility Segregation or in laymens terms splitting the database reads (querying) from the CUD (Create/Update/Delete) commands using a messaging system. This is an architecture based around CQS (Command Query Seperation) where methods should either be a command that performs an action or query that returns data not both. The beauty of this architecture is to simplify the querying, as this is based on direct access to unnormalised data that maps directly to the view model objects used in the UI. This in turn simplifies the complexity of the query language to return the data as in most cases you are just returning direct data, so "SELECT * FROM ViewModel WHERE .." would suffice, no more need for complex object graphs or mapping relational data objects to DTO objects via a layered architecture.

The Command aspect of the architecture is slightly more difficult to grasp, but in general is relates to sending commands to a data store for Inserts/Deletes and Updates. When these commands are sent a messaging system picks up these changes and these changes are published to the Read only database or any other subscribers that require updating based on the event.

So thats the basic theory out of the way (see additional reading for further analysis) but how does this relate to real world applications or when trying to implement the architecture where migrating legacy applications with complex relational data structures? To see how this fits in I have decided to embark on a proof of concept application. The architecture to be used will be built around ASP.NET MVC 3.0 using the Razor view engine, MongoDb for the read only data store and SQL Server 2008 RC 2 for the Command data store. The issue is that we are dealing with a legacy application that has complex relational data which requires migrating so it would be unwise to amemd this structure at this stage. The Commands will be handled using Enitity Framework 4.1, due my knowledge in this area. In relation to the subscriber/publisher messaging mechanism I am still open to ideas on this. I am currently reviewing nServiceBus and will more than likely follow this route.

I am using mongoDb for the read only database as this will allow me to review a noSQL database and also see how scaleable this type of solution is. I looked at other noSQL databases (such as RavenDb and CouchDb) but mongoDb seemed better suited to my purposes. There are many discussions comparing these applications so it worth reading these before making a decision. It is also worth noting that you could just use a relational database or even XML data - it depends on your business case and what type of system you are developing.

My main concerns intially are bulk inserting data to mongoDb from SQL Server 2008. As mongoDb data is formatted as BSON (an extended JSON format to support binary data) I assume that I could easily create some form of routine in SSIS to be able to create this and then insert. From their documentation this is feasible using mongoimport. I am interested to see how the project progresses and to see how it all fits together but I feel its always worth reviewing new patterns (or old patterns revitalised) as soon as you can. The majority of new projects I see coming available require MVC skills now which was not so apparent over a year ago.The same I feel will happen to CQRS as it matures , more and more businesses with see the benefits developing applications over standard n-tier architecture.

Update
After reviewing Udi Dahans' blog post When to avoid CQRS, I may need to review if I require CQRS but theoretically seems to be suited to my business requirements as this stage. As this is only a proof of concept application and is defined to see how this pattern can work in my scenario I still feel it is worthwhile pursuing due to the experience that can be gained and the learning involved. I do think there has been confusion relating to CQRS, and there is concern it has reached "best practice" status. Maybe the reasoning behind this is the fact the n-tier architecture had led to complexity in software and developers/architects are now looking at alternatives to this approach based on the many issues that have arisen over the years when developing this way. My own opinion is that all projects need a specific architectural roadmap and all avenues need to be considered based on the business requirements - i.e. one size does not fit all.

Further Reading:
CQRS - Introduction for Beginners
mongoDb
Clarified CQRS

Using Quartz.Net in Windows Services

Mon Jul 04 2011 by MarkP

I was recently tasked to schedule an application using Windows Services for the current project I am working on. After researching the options available to me I decided to implement this using an open source project called Quartz.Net.

As with many .NET open source projects this is a port from the popular Java based scheduler. The basic premise of Quartz is based around the concept of scheduling jobs, which are fired, based on triggers assigned to the job. One of the most noticable things, was the use of CRON Unix expressions to be able to express schedules. Cron Expressions are used to define schedules in a clear and concise manner, as one line of text. e.g. to define the schedule to run every hour in office hours (9am-5pm) the following notation would be used "0 0 9-17 MON-FRI ? *". Further examples and tutorials can be found at Quartz.net

Testing and deploying Windows services is always a bit of a challenge espcially if the underlying service requires database or file access. I always ensure that I have sufficient logging in place to be able trace what the issue is. In this scenario, where the job ran quite a long running process I used both EntLib 5.0 to log the errors and custom logging which detailed the stages of the process once completed.

Text logs were created for the actual scheduling of the job and XML logs were created when the actual jobs were run. These were required in the UI so the user would be able to view if a job was successful or not and also display any relevant errors.

Another requirement was allow the user to manage the scheduling throught the UI .This was basically a CRON expression builder based on inputs entered by the user. One consideration that has to be accounted for, is that if the scheduler is amended, the windows service would have to be restarted for the new schedule to be implemented. This can be undertaken in C# code my accessing the ServiceController object and passing in the service name (see example).

Quartz is a very useful tool when scheduling jobs under window services and gived you the programmer a lot of flexibility and full control, in relation to scheduling jobs, rather than having to rely on the Timer object or scheduling jobs using third party applications.

Tags: Quartz.Net   C#   Post Comment

Html Helper for sharing links to Facebook

Wed Jan 12 2011 by MarkP

Over the last few days I have been working on this site to try and improve it whilst I had some time and also to improve my knowledge of the MVC framework. I had a requirement to be able to share my articles with social networking sites such as Facebook. Facebook has a javascript library which simplifies the process of displaying and sharing specific urls or pages on Facebook. Further information and details can be found at the Facebook Share site.

ASP.NET MVC has many built in HtmlHelpers that makes the displaying Html controls simpler and cleaner on the View and reduces the code required especially when the code needs to be dynamically created based on the model or route values. With this in mind a HtmlHelper was created to display the "Share Facebook" button on the page:

  1. public static MvcHtmlString FacebookShare(this HtmlHelper html,string type, string controllerName,string actionName, object routeValues)
  2.         {
  3. var tag = new TagBuilder("a");
  4. tag.MergeAttribute("name", "fb_share");
  5.             
  6. if(!string.IsNullOrEmpty(type))
  7. tag.MergeAttribute("type", type);
  8.  
  9. RequestContext ctx = html.ViewContext.RequestContext;
  10. UrlHelper urlHelper = new UrlHelper(ctx);
  11. tag.MergeAttribute("share_url", urlHelper.Action(actionName, controllerName, new RouteValueDictionary(routeValues), ctx.HttpContext.Request.Url.Scheme, ctx.HttpContext.Request.Url.Host));
  12. return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
  13.         }

This is displayed on the View using the following syntax (using Razor View Engine):

  1. @Html.FacebookShare("Facebook button type", "Controller", "Action", new {RouteValues})

At present the HtmlHelper is limited to Route Values only but could easily be extended to use optional parameters, such as Html attributes or the direct page link if required. The key requirement was to be able to pass in Route Values only and to be able to automatically create the link using MVC's built in methods such as the UrlHelper object.

Update
For Asp.NET MVC 3.0 you would use HtmlString rather than MvcHtmlString and return a new HtmlString(string value) rather than the static Create method exposed in the MvcHtmlString object.

Tags: MVC   Facebook   Post Comment

ASP.NET Interview Questions

Tue Jan 11 2011 by MarkP

I recently completed a contract and have now been looking at the job market for the last week. Whilst being successful in getting interviews I have realised my interview technique needs improvement and also need to "brush up" on my basic ASP.NET concepts particulary in relation to Web forms development. I realised questions I could answer 2-3 years ago I have struggled with recently. The main reason is that I have spent a lot of time researching new technologies and techniques aswell as working on mainly core systems development and integration - ask me a question of Domain Driven Design or the differences between abstract classes and interfaces and this would be no problem - ask me about the ASP.NET page lifecycle or different caching techniques and I struggle even though I have been working with the technology for many years. With this in mind I have decided to create a list of key points in relation to ASP.NET to ensure I do not fall over on such questions in the future.

Define the ASP.NET Page Life Cycle?

This question comes up frequently and whilst being familar with the core concepts relating to intialisation, page loading and page rendering have forgotten some of the finer details relating to full page life cycle.

Details are as follows:

The page cycle commences when the ProcessRequest() Method is called:

  • Page Pre Initialisation -> Page.PreInit (Access to Master Pages and Themes)
  • Page Initialisation –> Page.Init
  • Initialisation Complete -> Page.InitComplete
  • Viewstate Loading -> Page.LoadViewState
  • Postback Data Processing -> Page.LoadPostData
  • Pre Load -> Page.PreLoad
  • Page Loading -> Page.Load
  • Postback Change Notification -> RaisePostDataChangedEvent
  • Postback Event Handling -> RaisePostBackEvent
  • Load Complete -> Page.LoadComplete
  • Page Pre Rendering Phase -> Page.PreRender
  • Pre Render Complete -> Page.PreRenderComplete
  • View State Saving -> Page.SaveViewState
  • Save Control State -> Page.SaveControlState
  • Page Rendering -> Page.Render
  • Page Unloading -> Page.Unload

Describe the caching techniques available in the ASP.NET Framework?

Key types of caching in a ASP.NET application are : Page Caching (page aspx), Fragment caching (user controls ascx) and Object caching (API) (in memory cache) All cached objects are stored in the AppDomain and are avialable to all users accessing the application. Page and Fragment caching is set using the directive at the top of the aspx page/ascx user control as follows:
@OutputCache Duration="60" VaryByParam="None"

Decribe the different modes Session state can be stored and how it is configured?

Session state is available to each user visiting the application, who can be uniquely identified by the SessionID. The following modes can set for session in the web.config file:

  • InProc mode, which stores session state in memory on the Web server. This is the default.
  • StateServer mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • Custom mode, which enables you to specify a custom storage provider.
  • Off mode, which disables session state.

I expect to be adding to this over the next few weeks, to ensure I have all areas covered and not caught out again in the future.

Upgrade to MVC 3 RC 2

Sun Dec 19 2010 by MarkP

Things move fast in the MVC world. After just recently upgrading to MVC 2.0, this site has now been upgraded to Release Candidate 2 of the ASP.NET MVC framework Version 3. One of the major additions to the framework is the option to use the new Razor View Engine when creating Views. It never felt right using Master Pages in MVC I suppose it was thought of as transitional phase from migrating from ASP.NET forms to MVC. The ASP.NET view engine can still be used if wanted but after upgrading from this to the Razor View Engine can see the benefits of switching. I know that a lot of development has been undertaken using the Spark View Engine due to it being more HTML friendly and improved readability. Razor has addressed a lot of these issues and will be interested as to how this will effect the number of users now implementing Spark.

One the first "gotchas" was distinguishing between encoded and non encoded HTML. Razor automatically encodes the text when using the @ prior to an HTML helper class. This caused me issues when trying the display the content of the blog text as this can include HTML markup. A way round this is to either use MvcHtmlString.Create method or as from RC 2 the Html.Raw helper method.
This also caused me an issue when trying the generate the Recaptcha control - this now returns a MvcHtmlString from a custom helper I created rather than a string.

In my previous version of the site I was using standard inline code to generate hyperlinks - this has now been updated to use Html.ActionLinks. This was an oversight in my part when initially developing the site but once implemented in Razor, implementing this way solved some of the compilation issues encountered.

Below is a brief overview of the steps used in upgrading the website application in Visual Studio 2010

  1. Added a new MVC 3 Web application to the main solution and selected Razor as the View Engine
  2. Removed any unwanted files created when the application was added.
  3. Copied all View, Controllers and associated directories from previous MVC 2 application to MVC 3 application.
  4. Copy any sections from my web.config to the web.config in the new MVC 3 application
  5. Renamed all Views to used the .cshtml extension.
  6. Renamed the Master Page to _Layout.cshtml.
  7. Removed all references to the Master Page content templates and use the @model directive to call the relevant layout pages in the Views
  8. Refactor all <%= %> and <% %> tags to use the correct Razor syntax (@) - this required quite a bit of work to ensure the syntax was correct.
  9. In the previous version I was using user controls (.ascx files) which I replaced using .cshtml files and Partial Views.
  10. Remove the previous MVC 2 website application and set the MVC 3 version as the website application

When deploying the site I had to ensure that all the new assemblies were included in the bin directory of the website. When running locally this is not an issue as the assemblies are automatically included in the GAC. My host provider Discountasp.net currently do not have these installed on their servers so these files had to be included for the website to work correctly.

After a few tweaks I got the site up and running and is working as expected. All I am hoping now there are not many changes when the full release version of MVC 3 is released.

Further Reading & Resources:
Haacked
Download MVC 3 RC 2
ASP.NET MVC 3: Layouts with Razor

Tags: MVC   Razor   Post Comment