Saturday, July 11, 2009

Taupo and Whakapapa Weekend

Some pics of our lovely weekend in Taupo and Whakapapa.

Saturday, July 11, 2009 9:35:09 PM (New Zealand Standard Time, UTC+12:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Wednesday, July 08, 2009

Foundations of Light

Not content with being 8 months pregnant and busily putting together loads of albums for clients and looking after tommy, Annabel has succeeded in putting together a brilliant exhibition of her work at the Coco Espresso cafe/restaurant in Thames.

Some quick pics of the setup and cafe are presented below:

003

002

004

 

001

Wednesday, July 08, 2009 11:09:29 PM (New Zealand Standard Time, UTC+12:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, June 29, 2009

KandaAlpha now on codeplex

kandaalpha a go go
Monday, June 29, 2009 11:34:59 AM (New Zealand Standard Time, UTC+12:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, June 25, 2009

db4o POCO Repository using Visual Studio 2010 .Net 4.0 Beta 1 and ASP.Net MVC 1.0

Note: This article applies to beta software and comes with no warranty or support, use at your own discretion. You must have Visual Studio.Net 2010 Beta 1 installed on your machine to make use of the code.

KandaAlpha now hosted on CodePlex, download from http://kandaalpha.codeplex.com/

This is an article which builds on a previous post on domain drive design architecture using asp.net web forms and Entity Framework v4.0. now, we leverage that core architecture to have a db40 object database as the back end and an asp.net mvc web application at the front end.

  • db40 is an object database which means there is no mapping files, attributes or other ‘impedance mismatch’ between your c# classes and the ‘database’ itself. It’s mostly used in embedded scenarios but appears to be getting used in websites. I’ve only just scratched the surface with it here, but i think it’s very cool and i hope it gains more and more traction.
  • asp.net mvc is the latest and greatest presentation architecture for asp.net and includes great separation of concerns between the various layers. In the architecture presented here, the Views and Controllers are within the asp.net mvc web application and the Model is essentially the domain model (e.g. KandaAlpha.Domain.Model)

My previous post discussed the architecture presented here solely within the context of the Entity Framework 4.0. It was also limited to usage of Web Forms on the front end. This release includes that working example but also includes into the solution an asp.net mvc 1.0 application which builds on the architecture and is backed by a db4op powered repository.

The first things i did were:

  • Added an MVC application (KandaAlpha.UI.Mvc) which is the front end side of things, with controllers that dependency inject the services and repositories as before.
  • Implement new repository for db4o (sorry, in the application i misspelt and used a zero, doh!) in KandaAlpha.Infrastructure.Repository.db40.
  • Update the web.config of the mvc application (and associated tests project) to wire the Unity dependency injection so that the db4o repositories are returned for repository interface requests

Changes to Dependency Injection Approach – Service Location vs Constructor Injection

I released this post last night and then got some good feedback from Will Beattie about design smells around my choice of Service Locator pattern approach to the dependency injection within the mvc controllers. One other approach as discussed in this post’s comments is to use constructor injection dynamically on the controllers. So, you can see i’ve added a custom controller factory.

In addition, Will had commented ton the controller’s dependency on the repository layer. I wasn’t too fussed about this given repository is within the domain and simply consists of interfaces but i did break it out for completeness. Notice that this does mean you get duplicate interfaces on the service layer in many cases. In practice, i would be quite happy to expose repository through the service layer but made the change anyway for completeness.

It’s worth noting i do still make of the ServiceLocator pattern within the mvc controller factory itself and also within global.ascx.cs Application_End which is where i ‘clean up’ the Repository Context (e.g. kill to db40 database ‘connection’).

So in general, the trend seems to be to favour constructor injection but there will sill be scenarios where a service locator pattern can prove useful, particular when mocking/testing may not be required.

Unit Test Isolation

If you take a look at the unit test for the controller now you can see how easy it is to mock up the controller without recourse to any dependency injection. In fact, the test is failing because i haven’t gotten round to implement a SaveChanges() for the in simply memory repository i built.

Before the unit test was admittedly an integration test, and it appears cleaner now, although it would be more work to maintain an in memory repository for a larger more complex application, but more advanced mocking tools may help with that.

How the mvc project runs

When you run the mvc project, it simply displays 2 customers on the screen. I’ve also added a little update on one of the customers as you refresh the page, just to show how to save things back to the db40 file.

This all took less than 2 hours and i was very pleased to get things up and running so quickly. It hopefully shows you some of the power and flexibility of dependency injection and the domain drive approach as well as introduction you to how use such an architecture with technologies like asp.net mvc, db4o and entity framework.

Thursday, June 25, 2009 10:51:27 PM (New Zealand Standard Time, UTC+12:00)  #    Disclaimer  |  Comments [3]  |  Trackback
 Sunday, June 21, 2009

glorious winter day

what a fantastic sunny day it was today.. Winter? bollocks to that!

DSC00088DSC00085DSC00084DSC00083

Sunday, June 21, 2009 6:26:59 PM (New Zealand Standard Time, UTC+12:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Saturday, June 20, 2009

Zoo Pics

DSC00054DSC00053DSC00051DSC00069DSC00068DSC00066DSC00065DSC00063DSC00060

Litter everywhere daddy! Tidy Kiwi Tommy sorts the ‘naughty people’ out.

DSC00059

DSC00056

The Thinker…

DSC00082

Apparenlty they are pink because of what they eat (shrimps etc.). When i asked Annabel why we don’t go green from eating cabbage, she said “because we aren’t flamingos”. Fair enough really.

DSC00081

Tommy after his first chocolate paddle pop

DSC00079

 

DSC00078DSC00077DSC00076DSC00074DSC00072

Saturday, June 20, 2009 5:17:33 PM (New Zealand Standard Time, UTC+12:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, June 18, 2009

Some recent pics

DSC00047DSC00043DSC00042DSC00041DSC00039DSC00036DSC00035DSC00034DSC00031DSC00030DSC00025DSC00022DSC00021DSC00019DSC00014DSC00013

Thursday, June 18, 2009 10:28:08 PM (New Zealand Standard Time, UTC+12:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, June 01, 2009

Kentico CMS 4.0 and Bounded Box Image Resizing

This article has code specifically for Kentico CMS 4.0 and can be used in a scenario where you have an image’s dimensions and want to ensure that the image fits into a known bounded box.

With Kentico CMS you retrieve images using a call to a handler at /CMSPages/GetFile.aspx. That page accepts parameters Height, Width and MaxSideSize. Height and Width can be fixed and specifying one and not the other will retain aspect ratio. MaxSideSize allows a single value to bound the height and width.

However, if you have a given bounded box on the page (e.g. say a 400 * 300 space) and you want to make sure an image fits inside that space whilst retaining aspect ratio there is no way to do this.

I’ve emailed this code to the Kentico support team to allow MaxHeight and MaxWidth properties so that we can do this. In the meantime this hack/fix within GetFile.aspx.cs code behind file will allow you to supply negative values for height and width which then act as a bounding box!

// HACK - Interpret MaxHeight and MaxWidth via negative Height and Width values (line 599)
if (this.Height < 0 || this.Width < 0)
    ApplyBoundingBox(atInfo.AttachmentImageHeight, atInfo.AttachmentImageWidth);

/// <summary>
   /// Custom Hack to interpret negative height and width values as setting bounding box
   /// </summary>
   private void ApplyBoundingBox(int imageHeight, int imageWidth)
   {

       if (this.Height >= 0 && this.Width >= 0)
           return;

       int maxHeight = 0;
       int maxWidth = 0;
       float ratio = 0;

       if (this.Height < 0)
       {
           maxHeight = this.Height * -1;
           this.Height = imageHeight;
       }

       if (this.Width < 0)
       {    
           maxWidth = this.Width * -1;
           this.Width = imageWidth;
       }

       if (maxHeight > 0 && this.Height > maxHeight)
       {
           ratio = (float)this.Height / (float)maxHeight;
           this.Height = maxHeight;          
           this.Width = (int)((float)this.Width / (float)ratio);
       }

       if (maxWidth > 0 && this.Width > maxWidth)
       { 
           ratio = (float)this.Width / (float)maxWidth;
           this.Width = maxWidth;
           this.Height = (int)((float)this.Height / (float)ratio);
       }

   }

Monday, June 01, 2009 11:43:47 AM (New Zealand Standard Time, UTC+12:00)  #    Disclaimer  |  Comments [0]  |  Trackback