Saturday, June 27, 2009
I've started work on a new project recently and the first thing for me to sort out is the Data Access Layer.  Because the Repository pattern served me so well in the past, I'm going to start there.

However, the previous solution used a bespoke QueryBuilder which converted an ICriteria into the appropriate SQL.  This SQL was then just passed through as CommandText on an ADO.NET connection to the appropriate database, Oracle or SQL Server. 

What we did was to mock the Repository using LINQ to Objects, and this allowed us to construct our application in a Domain-Driven fashion.

Great!

However, there was always room for improvement.  I've wanted to use LINQ as my ORM for quite some time now, and I know the purists out there will say that LINQ isn't an ORM, but for practical purposes in this project it's going to be.

Also, I've decided to use L2S rather than the Entity Framework.  My mantra is Design For Test, and right now L2E is just not persistent-ignorant in the slightest.  There are hacks as I've outlined in previous blog posts, but building hacks into the foundation of your application is a recipe for disaster.

While L2S may not be supported in future by MS, who cares?!  It works, plenty of good, solid apps have been built on it, AND IF WE GET THINGS RIGHT, there's no reason why we can't switch to L2E v2.0 next year.

We can do this because we are going to abstract out the actual DataContext, and access our data using POCO and Repository pattern.

In addition, if we can swap out the DataContext, then given that LINQ to Objects and LINQ to SQL queries are the same (or at least I hope they are, more on this later) then we can have a set of queries that we can run on both in-memory and database sources.  Truly Datasource-Agnostic.

I got the inspiration for this from http://compiledexperience.com/Blog/post/Domain-Driven-Design-Repositories-in-LINQ-to-SQL.aspx - and it works!





This diagram shows the LinqToSQL assembly, but we can also replace the SQLDataSource and SQLUnitOfWork with a MemoryDataSource and MemoryUnitOfWork.

For example, if we want to test an in-memory collection, we do the following:

            MemoryUnitOfWork context = new MemoryUnitOfWork();

            Repository<Customer> r = new Repository<Customer>(context);
            r.Save(new Customer(1, "Joe Bloggs"));
            r.Save(new Customer(2, "Joanna Bloggs"));

            IEnumerable<Customer> customers = from c in r.Fetch(null) where c.ID == 1 select c;

            List<Customer> results = customers.ToList<Customer>();

            Assert.AreEqual("Joe Bloggs", results[0].Name);


And if we want to test an actual connection to a database using L2S:

            DataContext db = new DataContext("Data Source=(local);Initial Catalog=DMGTest;Integrated Security=SSPI");

            SqlUnitOfWork context = new SqlUnitOfWork(db);

            Repository<Customer> r = new Repository<Customer>(context);

            IEnumerable<Customer> customers = from c in r.Fetch(null) where c.ID == 1 select c;

            List<Customer> results = customers.ToList<Customer>();

            Assert.AreEqual("Joe Bloggs", results[0].Name);

Note the line

IEnumerable<Customer> customers = from c in r.Fetch(null) where c.ID == 1 select c;

which is identical in both versions.

My next step is to encapsulate the queries using the Specification pattern, as Ritesh shows here.

So now we have completely isolated our domain from our DAL, and with the introduction of the Specification pattern we can look at our queries from a domain perspective also.

The code is just bashed out at the moment, but if you want it then just drop me a line and I'll send it over.



kick it on DotNetKicks.com
Saturday, June 27, 2009 9:59:11 AM (GMT Standard Time, UTC+00:00) | Comments [0] | c# | Database | Patterns | TDD#
Tuesday, May 26, 2009
Javascript, as I'm sure you are aware, has no in-built knowledge of classes, inheritance or interfaces as other OO programming languages do.  We have to engineer these using various constructs and work-arounds, in order that we maintain code order and good programming principles.  With more and more code on the client-side in order to provide rich user experiences, this is vital or you'll be condemned to snippet hell. 

Simple View-Controller pattern

Say you want to implement a bog-standard separation of View-Controller logic.  The View is a collection of controls, and directly handles the interaction of these controls before packaging them up and sending them on to the Controller.  In the Windows world, we would raise a custom event from the View which the Controller hooks into, but given Javascript's flexible duck-typing, we can bind the View directly to the Controller and inject a Stub in the future for testing if we should wish.

Anyway, our View has a button.  When the user clicks the button, we want the Controller to know what to do, not the View. 

The View will have a SetController method, which when set, will bind the button's click event (I've omitted the unbind to clear things up) to the appropriate method on the Controller.

Like so:

function RefineSearchView() {
    var _controller;
}

RefineSearchView.prototype.setRefineSearchController = function(newController) {
    this._controller = newController;
    $("#btnStreetRefineFilter").bind("click", createMethodReference(this._controller, "HandleFilter"));
}

NOTE: this is where it gets interesting.  createMethodReference() - what's that all about?  Why don't I just do:

$("#btnStreetRefineFilter").bind("click", this._controller.HandleFilter);

You could expect this to simply bind the click event on to this particular View's Controller's HandleFilter() method.  Non?

Non.  The reason this won't work is because when we bind the click event, we are not in the correct context.  This article - with the suitably majestic title of Object-Oriented Event Listening through Partial Application in JavaScript - will explain it in ways far better than I could!

This is to demonstrate in it's most basic format - we can go on as the article suggests, wave our magic refactoring wand until it transforms from the ugly duckling of createMethodReference into the elegant swan of

this.element.onclick = this.buttonClicked.bind(this);



Controller Handle method

Now we have this set up, it's just a case of implementing our HandleFilter method on the controller:


RefineSearchController.prototype.HandleFilter = function(e) {
    var searchParams = this._searchController.getSearchParams();
    var locality = this._view.getLocality();
    var town = this._view.getTown();

    searchParams._locality = locality;
    searchParams._town = town;

    this._searchController.setSearchParams(searchParams);
    this._searchController.search();

    this._view.updateFilterStatus(searchParams);
}

So now we have completely separated our code into View and Logic sections.  This example was a bit contrived, but you can imagine the situation where our View has to gather information from a number of controls before wrapping these up and sending them on to the Controller in a nice abstract package, similar to the searchParams outlined above.

Not only does this approach give benefits in terms of maintenance, but the stage is now set for unit-testing and programmatically driving your dynamic web page...

Tuesday, May 26, 2009 10:04:57 PM (GMT Standard Time, UTC+00:00) | Comments [0] | Javascript | Patterns | TDD#
Wednesday, May 20, 2009
Okay, so this is hardly a ground-breaking topic.  But I think it's a valid showcase as to how rich, UI experiences can be delivered via the browser without costing the earth.

Picture the scene - I have a web page with a jquery Grid (FlexiGrid - though I think I will be upgrading to jqGrid due to it's continued developer support - but more on this later) and a toolbar (a very cool Vista style toolbar).

One of the commands available on this toolbar is Print. 

When the user clicks this command, a jquery UI dialog pops up, displaying a selection of valid reports which the user may select.

This reports list is generated from the server.

So, if all of this was to be done as the page loads - well, you can imagine the performance would be unacceptable.  But I want to keep the user experience rich - I don't want them navigating from the grid to another page when they click Print.  So I need to do this using AJAX.

The process is as follows:

 




The Print Button Handler calls the PrintWebService via an AJAX call using some jquery magic as so:

 $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: "{}",
                dataType: "json",
                url: "http://localhost/PrintService/PrintService.asmx/RenderPrintDialog",

                success: function(data) {
                    $("#printdialoginner").html(data.d);
                    $("#printdialog").dialog("open");
                },

                error: function(xhr, status, errorThrown) {
                    alert(status);
                    alert(errorThrown);
                }
            });

Inside the service, we can get our reports from the server (from the database) at our leisure.  Ish.

Now comes the clever part.  To return the UI, all we're doing is churning back some HTML, right?  But what if we want to be able to visually SEE what we are going to return, at design-time? 

Thanks to Scott Gu, we can get full VS designer support when creating our dynamic UI - check it out - http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx

So we create our .ascx controls as normal, pass the path to the ViewManager, and let it get it's hands dirty.  It also keeps a nice, clean MVC separation.

Using this trick, I've been able to deliver a seamless, rich UI without having noticeable effects for the user.  Okay, so there is a slight delay when they bring up the Print dialog for the first time (I know, I've not mentioned caching here but do I need to?) but it's barely noticeable.










Wednesday, May 20, 2009 7:57:56 PM (GMT Standard Time, UTC+00:00) | Comments [0] | Javascript | Patterns | Web Design#
Wednesday, May 13, 2009
We had a review of some client-side code today, as it had started to smell a little "off".

As always, the indication I use when quickly reviewing a design is - how easy is this to unit test?


The problem

Logic all over the place.  Massive "if" statements.  More spaghetti than an episode of the Sopranos.

For our web app, we have a number of "tools"; when the user clicks one, it puts the application into that particular tool mode, which means that any subsequent mouse events have to check which mode we are currently in, and take the appropriate action.  Not only that, but we have introduced a vector view layer (SVG) so we have some tools that update the image layer, some that update the vector layer, and some that update both.

The solution

Stick to established patterns.  When dealing with UI, everything boils down to MVC in the end.


 

What we've done here is separate out each tool into an "Interactor" class.  The View provides the abstraction of each view type, and also the UI events.  The Controller provides the glue to stick everything together.

In Summary:

  • The View knows about the different view types, and knows about UI events.
  • The View Types know how to render themselves, but are still pretty "dumb".
  • The Controller knows about UI events, and things called Interactors, but it doesn't know what Interactors are or what they actually do.
  • The Interactors know exactly what is going on, they interact directly with the view type and the server.





This solution certainly isn't "pure" by any means - there is no M!  And I am in no doubt that we could refine this further. 

But the main thing is we have separated everything out into distinct, logical components that have a defined and related set of responsibilities.

What's more this design is geared for unit testing.  We can test the Interactors independently of the views (by dependency injection, which we will do later) and then add another layer of tests integrating the Controller, and so on.



Wednesday, May 13, 2009 8:16:28 PM (GMT Standard Time, UTC+00:00) | Comments [0] | Patterns | TDD | Javascript#
Saturday, April 11, 2009
Starting a new database project, the most important thing for me is to design it for test.  In order to do that effectively, we have to keep the business objects and the data access separate.

This concept is nothing new, but there's a lot of different ways to do that these days.

The first idea I had was to have the data access encapsulated within a DataProvider layer which would be interfaced directly by the application.   This would create and return basic DTO objects for the application to play with, as there is little to no business logic in the classes at this point.

Following discussions with my team, this morphed slightly into having the DTOs converted into fully-fledged Business Objects, which would have knowledge of the DAL and access the database in this way.

It seemed okay.... but then I got thinking about how to return a collection of objects, and how all that worked.  So that brought me to the Repository Pattern.

Simply put, the Repository Pattern abstracts the Data Access part from the Business Objects, which means that to fetch an Object, one has to go via the Repo.

There are a number of ways to implement this - doing a search will bring back a LOT of Linq-related implementations.

Right now, I'm going to keep it more in line with the original specification of this pattern.  My Repository is simply going to return the Business Objects themselves.  At the moment it is read only so there is no need for CUD - because of the way we have separated this we can easily come back to this at a later date.


Saturday, April 11, 2009 11:06:37 AM (GMT Standard Time, UTC+00:00) | Comments [0] | Database | Patterns#
Friday, February 06, 2009

Make sure you follow the pattern outlined here - http://msdn.microsoft.com/en-us/library/w369ty8x(VS.80).aspx

Friday, February 06, 2009 10:19:06 AM (GMT Standard Time, UTC+00:00) | Comments [0] | c# | Patterns#
Wednesday, February 04, 2009
I've been looking at the EF lately and while I've still not settled on how it can be tested (given the lack of persistence agnosticism), I do feel that MS has received enough stick from the community that this will be a feature of v2.

The following article by John Papa provides some good ideas as to how one would use EF as part of an n-tier architecture - http://msdn.microsoft.com/en-us/magazine/cc700340.aspx

Wednesday, February 04, 2009 7:53:45 PM (GMT Standard Time, UTC+00:00) | Comments [0] | c# | Database | Patterns | Entity Framework#
Tuesday, January 27, 2009

Making sure that your application is disposing of any unwanted resources has always been part of the ugly plumbing work of writing software.  Although .NET "helps" with this - by providing a non-deterministic Garbage Collector - it can actually muddy the waters a little.  Now, instead of having to implement a Destructor in each class, most of the time you can let the framework deal with it. 

The tricky part - and the reason I'm writing this blog - is remembering the specific scenarios when you need to, and when the framework will take over.

The general guidance is that you only need to implement IDisposable when dealing with unmanaged resources.  This is because the framework has no way of tracking this, and you need to do it yourself.  However, there are other scenarios where you might need more control.

A specific scenario would be if we had a class which had hooked into events provided by a global static or singleton class.  This singleton would likely not be disposed of until the application is shut-down (although obviously every case is different).  Because the delegate keeps a reference to our class, it would keep it alive until the singleton is disposed.  It is highly likely we want to free our class before this.  Whether we do this as part of implementing the IDisposable pattern, or use Init() and Unit() methods, is still up for debate.

In the following scenario, only ListForm and NavBarControl is required to implement IDisposable (because it is a Form):

Because this is a self-contained object graph, once we free our reference from ListForm, the GC will be able to free all the objects within this graph.

HOWEVER, if, say, ListFormController were to hook up an event handler to an object outwith this graph, then we would be in trouble - because it would not be freed and therefore keep it's referenced items alive.

You can find out more about the IDisposable pattern here.

 

Tuesday, January 27, 2009 12:24:23 PM (GMT Standard Time, UTC+00:00) | Comments [1] | c# | Patterns#
Tuesday, January 06, 2009

It takes a while to get going after the holidays, but once I had dusted the mince pie crumbs from the thinking cap it was business time.

As you may be aware from previous posts, I'm a regular user of the various flavours of the MVC pattern (MVC, MVP, Supervising Presenter etc.  I'll just refer to it as MVC from now on, even though the exact pattern I'm using may not be that).  The software might take a little longer to construct, but this will pay back in maintainability, testability, legibility and other -bilities.  

The task for today was to apply a user-defined 'mask' over a datasource that was bound to a grid.  In this specific case, we apply the mask to the grid instead of the datasource, but the principles would be the same if our requirements were to modify the datasource in some way.

We have 3 MVC-triads involved, and the associations between which (and classes) are illustrated below:

 

The 'Super' triad, shown in green, is where it will begin.  The Window will receive the click event to show the Top N dialog, which will notify the WindowController.  The WindowController will then create the concrete TopN View (the dialog), passing this into the TopNController.

When the user clicks the 'Apply' button on the Top N dialog, this will communicate the specified values to the TopNController, which will then modify the PivotViewDef.  Note: The PivotViewDef is stored by the Document, and bound to the GridView by the GridController on instantiation.  We use a simple Observer pattern for this, which means that whenever PivotViewDef is modified, we broadcast this change to any interested parties, which can then take action.

Therefore when the PivotViewDef is modified, it will automatically refresh the GridView.

This is a clean and elegant design, as it removes any logic from the forms.  The inter-dependencies between the various components are also limited, which reduces the complexity.  And because the TopNController only communicates to the view via an interface, it is inherently unit-testable.

 

 

 

 

Tuesday, January 06, 2009 8:46:42 PM (GMT Standard Time, UTC+00:00) | Comments [0] | c# | Patterns | TDD#
Monday, December 01, 2008

One of the most valuable lessons I ever learned during my time at university, was the methodology of Divide-And-Conquer.  This can be applied to anything, but in Computer Science is vital for dissecting those complex problems into their component parts.

The only problem was that, like a lot of things I did at uni, it wasn't really put into practice once the coursework and exams were over.  When you start work it's all about delivery - you don't have time to really think things through in this way, especially when you only have a couple of days until what you're working on is in the product, finished and looking shiny.

Or at least, you don't think you have the time.  But turns out the lecturers and academics are right after all.  Taking the time up-front to fully understand a component, whether it be for internal use or a UI control, saves a lot of hassle in the long-run.  Think incomplete specifications, unexpected surprises, and debugging particular scenarios during a defect cycle.

I've been thinking about this recently as I have to get to grips with a new control, and slot it into an existing application.  Whenever I start to work with a control that I'm unfamiliar with, I like to build myself a little sandbox and give it a good going overmaking sure that I know what's going on in an isolated environment, without having to worry about the rest of the codebase affecting it.   I might not get as much time on it as I'd like, but there's always that balance. 

What this lets me do is get to grips with what I'll be using the control for, how it binds to data, how it can be controlled via it's interface rather than using the 'Smart' Tags to configure it.   

I've been putting this technique through it's paces for a couple of years now, and it's clear when I look back at some older code how much of a difference it makes.  The solutions might take a little longer to develop, but the result is a cleaner implementation, for the reasons outlined above.

If I was President, I'd like to see a lot more of this sort of thing.  Whenever a new component was being developed, it should have an associated driver (prototype/sandbox - call it what you will) which configures and simulates the component in a real-world environment, but completely isolated of other parts. 

Of course, what this ultimately results in is a suite of Unit Tests for your newly developed component.  So this is hardly a revolutionary idea, but supports that we are on the right track.

 

 

 

Monday, December 01, 2008 5:59:29 PM (GMT Standard Time, UTC+00:00) | Comments [0] | c# | Patterns#
Sunday, March 02, 2008

I'm sure you'll be aware of the different MVC frameworks currently out there on the web.  RubyOnRails, MonoRail, and the new Microsoft ASP.NET MVC framework all claim to accelerate web development by removing a lot of the mundane, monkey-work plumbing involved in creating a web project.

They also provide a clear separation of concerns, by separating the presentation, logic and data-access into discrete layers.  This has the advantage that we can then introduce unit-testing into our web project, and mock any required components.

So far so good, but personally having looked at these frameworks, I can't fit them into any projects I'm working on at the moment.  It seems to me to be a trade-off between how much control I need and how much it conforms to a typical web layout.  But that's maybe because I haven't actually tried them out on anything other than a sample project. 

Having said that I do like what they offer - GETTING THE CODE OUT OF THE PAGE

Essentially, I believe we can achieve this and also maintain a lot of the control by using the framework outlined below.

As you can see, we have our MVC-triad, with the Controller at the heart.  The Controller interacts with the View via an interface, with each concrete WebPage having a reference to the Controller.  The WebPage also CREATES the Controller object(s) it uses, passing itself (it's interface) in to the Controller's constructor.   When the User selects something on a Page, the Page then calls the required method on the Controller.  The Controller can then carry out any logic and indirectly navigate to another URL.  It does this via the View interface that it has passed to it in it's constructor.

This example shows how the Controller can be used to control application flow.  The user initiates a command on MainPage, which calls Controller.Do_Command1.  In this method, the Controller would then carry out any logic for that Command, but then wants to move to another page.  The Controller can't do this directly, so calls the Show_AnotherPage() method on IMainPage (the MainPage's view interface).  The concrete MainPage class then redirects to AnotherPage.

This example shows the Controller again controlling the application flow, this time with conditions.  You can see how this could be easily tested via mocking.

Admittedly, this framework still relies on the developer putting in a lot of the plumbing, which has been nicely encapsulated by other MVC frameworks.  However, with this you get complete control over what is happening, and you also get to use the traditional WebForms approach, complete with Viewstate et. al. which is essential for 'legacy' apps.  It also means you can pick and choose the areas in the project where you implement it. 

If I was starting a web app from scratch I'd certainly look into MVC ASP.NET and see how it would work, but if you want the benefits of code separation in your existing applications now, then this framework could be adapted very easily.

EDIT: I'm not sure I really would look into MS' MVC ASP.NET framework any more - I can't see what benefits I would get from it that I can't with this approach.

Sunday, March 02, 2008 9:58:16 PM (GMT Standard Time, UTC+00:00) | Comments [0] | ASP.NET | Patterns#
Tuesday, February 26, 2008

I've been working on a particularly tricky problem recently, one that has me analysing the Form values collection that is passed in an ASP postback.  Not that difficult in itself, but I don't know what values I'm looking for until runtime!

I ended up encapsulating this parsing logic in a special FormValuesParser class, but once I had finished I couldn't eliminate the bad code smell that was hanging around, and coming from someone who buys the office air-freshener, I had to do something about it.

It was easy enough to see where it was coming from.  The main method in this class, ParseKeyType, was a huge switch statement.  Each case block did more or less the same thing, but had to do it in subtly different ways - for example, parse the formValueKey one way for a Browse type, and a different way for a MultiSelectCheck type.

The code looked like this:

private void ParseKeyType(string formValueKey, KeyType keyType)
        {
            string controlMapKey;
            FormKeyInfo keyInfo;
            switch (keyType)
            {
                case KeyType.Browse:
                  //Perform specific parsing to Get Control Map Key
                  //Get Key Info
                  //if KeyInfo != null
                  //Set KeyInfo.Value
                    //Get key look up in ControlMap
                    break;
                case KeyType.MultiSelectCheck:
                  //Perform specific parsing to Get Control Map Key
                  //Get Key Info
                  //if KeyInfo != null
                  //Set KeyInfo.Value
                    //Get key look up in ControlMap
                    break;
                case KeyType.TextEdit:
                case KeyType.Memo:
                case KeyType.SpinEdit:
                case KeyType.Browse_DropDown:
                case KeyType.DateEdit:
                case KeyType.DateTimeEdit:
                  //Perform specific parsing to Get Control Map Key
                  //Get Key Info
                  //if KeyInfo != null
                  //Set KeyInfo.Value
                    //Get key look up in ControlMap
                    break;
                case KeyType.CheckEdit:
                  //Perform specific parsing to Get Control Map Key
                  //Get Key Info
                  //if KeyInfo != null
                  //Set KeyInfo.Value
                    //Get key look up in ControlMap
                    break;
                case KeyType.TimeEdit:
                  //Perform specific parsing to Get Control Map Key
                  //Get Key Info
                  //if KeyInfo != null
                  //Set KeyInfo.Value
                    break;                    
            }

So trying to fathom what was going on when you have to add a new KeyType in 6 months time was going to be a hassle.

If we apply the Strategy pattern, where we encapsulate the code logic within distinct Strategy classes but provide a common interface, we end up with the following, MUCH more fragrant code:

        private void ParseKeyType(string formValueKey, KeyType keyType)
        {
            IKeyInfoStrategy keyInfoStrategy = null;
            switch (keyType)
            {
                case KeyType.Browse:
                    keyInfoStrategy = new BrowseKeyInfoStrategy();
                    break;
                case KeyType.MultiSelectCheck:
                    keyInfoStrategy = new MultiSelectCheckKeyInfoStrategy();
                    break;
                case KeyType.TextEdit:
                case KeyType.Memo:
                case KeyType.SpinEdit:
                case KeyType.Browse_DropDown:
                case KeyType.DateEdit:
                case KeyType.DateTimeEdit:
                    keyInfoStrategy = new SimpleKeyInfoStrategy();
                    break;
                case KeyType.CheckEdit:
                    keyInfoStrategy = new CheckEditKeyInfoStrategy();
                    break;
                case KeyType.TimeEdit:
                    keyInfoStrategy = new TimeEditKeyInfoStrategy();
                    break;        
            }


            if (keyInfoStrategy != null)
            {
                string controlMapKey = keyInfoStrategy.GetControlMapKey(formValueKey);
                FormKeyInfo keyInfo = GetKeyInfo(controlMapKey);
                keyInfoStrategy.SetKeyInfoValue(keyInfo, formValueKey,
                                                    mFormValueCollection);            
            }
        }

First off, we create an IKeyInfoStrategy interface which contains the common behaviour we have abstracted, and then a concrete Strategy class for each different 'class' of KeyType.

 

This is a very simple example, but you can see how it will scale and lead to much easier-to-read code.

 

Tuesday, February 26, 2008 8:48:16 PM (GMT Standard Time, UTC+00:00) | Comments [0] | Patterns#
Search
Archive
Links
Categories
Admin Login
Sign In
Blogroll