Friday, August 15, 2008
Yesterday afternoon I presented a quick 20 minute presentation to the Enterprise Architect user group - it was an interesting opportunity to talk with both an Enterprise Architect lead developer and analysts/BA's etc. who are using Enterprise Architect on a daily basis - there's nothing quite like doing something and then even more so presenting on what you've done to really firm up some conclusions / ideas around it (and as is often the case with me, spark 20 ideas for new products I could build to make it better/smarter).

For anyone who's interested here's the slide deck I presented - and also some links to things that were discussed:
Thanks to catch for hosting the group and providing the video conferencing facilities to link Auckland / Wellington (small tip, if you are presenting to two groups be sure to sit at the end of the table looking towards the camera, or your spend your whole time ping-ponging your head towards one group and the other ;) - and also thanks to everyone who came along and listened.

Also of interest was the discussion after the presentation around providing a number of example/sample MDA transformations through Sparx Systems New Zealand site - which should mean that before the end of the year you will be seeing both guidance and examples on developing MDA transformations targeting NHibernate, NHibernate search, ActiveRecord, Monorail and RhinoSecurity starting to emerge (what I tend to loosely call the "Castle stack" for want of a better term.) - which I'll post about it as it starts to happen.

posted @ Friday, August 15, 2008 2:19:21 AM (New Zealand Standard Time, UTC+12:00)    Comments [0] | Trackback |
 Monday, June 30, 2008
Jonathon Rossi over in Brisbane has been hard at work on the CVSI project (Castle Visual Studio Integration) and has released version 0.3 which now supports VS 2008 - for those not in the know CVSI provides nvelocity intellisense when writing views for monorail.

I did some testing of some earlier releases last week and it's looking good, hilights include:
  • New installer which supports 2005 and 2008.
  • Basic XHTML intelisense.
It also includes some fixes which means the intelisense now works when the templates are not in a web application project (useful for those of us writing applications where we have pluggable modules) and fixes for multi-level inheritence, so helpers and view components with multiple levels of base class i.e. FormHelper (which now inherits from AbstractFormRelatedHelper) will be included in the list of classes, something which bugged me with earlier releases on 2005.

Great work Jono!

posted @ Monday, June 30, 2008 8:31:14 AM (New Zealand Standard Time, UTC+12:00)    Comments [0] | Trackback |
 Thursday, June 19, 2008
If you recall many moons ago I posted a series of articles on the Castle Project's IOC Container "Windsor" teaching the fundamentals of IoC with a practical bent - lots of people liked them, and I still get feedback every now and then from people starting to use windsor and finding them useful.

At any rate Michael McGuire was once such person who read those tutorials a year or so ago and has now started a series of his own - mirroring my castle container tutorials but with the P&P Unity container instead - you can find it here.

As someone who has not given Unity much more then a brief skim it's a nice way to quickly get up to speed on some of the key differences.

So far after reading a couple of articles I've learnt.
  • You need to implement your own type converters for things like arrays or dictionaries in configuration.
  • Configuration syntax is not particularly human-friendly, obviously designed for management via a tool  - requiring the entry of full types all over the spot like "Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration" - just to register a component!
  • Default lifestyle is transient... hmmm.. personally I think singleton is more-often the norm for me when writing applications, but it really depends on how the container is being used/abused I guess.
  • Support for multiple configurations looks a little more baked in - but this is trivial stuff to implement in most containers.
I'll be interested to see how decorator chains etc. are implemented in Unity.

Good work Michael.

 |  |  | 
posted @ Thursday, June 19, 2008 12:45:04 AM (New Zealand Standard Time, UTC+12:00)    Comments [2] | Trackback |
 Sunday, December 02, 2007
So a quick observation on the alt.net conference list:

"You’ll be happy to know then that in the new WPF Composite work we are doing, the DI/IOC mechanism will be decoupled and pluggable. Actually the next version of Entlib will be the same. There’s a new Dependency Injection block that will allow you to plug-in the container of your choice."

Which was posted on the alt.net conference yahoo list by by Glen Block from Microsoft.

I think this is very good news, at least for the WPF composite work they're doing - it might make it a great deal more appealing to me then the CAB was (I was never a big fan of the CAB because it didn't integrate well with the other stacks of technologies that made me productive).

I'm fairly ambivalent about the Enterprise Libraries upcoming pluggable IoC support however - surely the overlap between it and the facilities and services already provided by the IoC container you'd be plugging into would be great ... and IoC is only part of that story... for a number of common services (i.e. logging, transactions etc.) you're going to have create appropriate wrappers and sandwich things into the containers "abstracted" view of the world, or face tightly coupling your application to the Enterprise Library, I'm not sure there is much of a value proposition there... though I'm open to being educated if any P&P Microsoft people happen to read this.

Interestingly enough has picked up on this slip from Glen and noted that it also implies Microsoft are writing yet another IoC container that will probably ship with EntLib & the WPF composite work (in whatever shape it eventually takes) and they ask the question "why oh why!" .

Personally I think it makes good sense to provide an out of the box IoC container, with it the product is far more accessible to new developers and people reviewing the technology - especially when you consider that:
  • If support was added into say the Castle Windsor container, it wouldn't see an official release until RC4 was made public - and until then would be in a state of flux as part of the castle trunk.
  • People would expect Microsoft to "suggest" an OSS container that works best with their offering - and playing favorites with the .Net OSS community could only do harm to the ecology I feel.
  • Microsoft retain consistency with their existing approach of providing an entire stack of technologies for you to get the job done, if that's what your like/want or are told you want. 
  • They can provide a set of consistent tutorials and training materials for developers to "get up to speed" with for the product, without having to provide umpteen alternative examples for different containers, or explain why the container terminology is slightly different to the product terminology.
So in short, I think it's a positive sign, not a negative one, even if another container is introduced into the mix - I'll be interested in seeing just how they do it (both the container, and the extension points to allow for plugging in a different container)... Though it could just be a hack using IServiceProvider, I wonder if perhaps they may need to go a bit further, so they can register services into the plugged in container with the appropriate lifestyle and configuration etc.

Edit: I just noticed Ayende posted about this as well...  he seems pretty well aligned with the "why oh why" viewpoint, perhaps I'm missing something - but I just don't see it being all that practical in some scenarios to provide a product that wont work without the developer going through a separate selection process to pick an IoC container, if they don't already use one from the OSS community.
posted @ Saturday, December 01, 2007 11:08:07 PM (New Zealand Daylight Time, UTC+13:00)    Comments [1] | Trackback |
 Tuesday, October 30, 2007
Just a quick note that I've put together a small guide on working with the Castle / Nhibernate / RhinoTools trunk here on my wiki, which goes well as a companion to this article on working with the Castle trunk which can be found on using.castleproject.org.

Hopefully it proves useful to someone other that myself.

posted @ Tuesday, October 30, 2007 8:30:36 AM (New Zealand Daylight Time, UTC+13:00)    Comments [7] | Trackback |
 Friday, September 21, 2007
Quite a good turnout, with well over a table full (I didn’t take an official count, but I’m guessing around 12 or more) including Alex James and Keith (who we hadn’t seen for a while) and two newcomers from Olympic Software, which was great - we always like to see fresh faces and ideas.

I talked very briefly about Castle Project RC3 – I suggested it was a week or two away from getting out the door, but in fact - It’s out as of about an hour ago! (11:30am) - the website and documentation is still in the process of being updated, but the installer and binaries/source are all here.

I was going to run through some of the changes I like in RC3 – but the conversation got steered away before I got warmed up - but it's well worth checking out, if you haven't been keeping an eye on the trunk and the changes that have been going in.

At any rate – this is the culmination of a huge amount of work from many of the Castle contributors, I think it even has a 2 line patch of my own in there somewhere :P ... great work guys!

PLINQ – talked about PLINQ getting closer, and the fact that parallelization is a very hot topic of late, with a few noting that the last MSDN mag was almost entirely focused on this subject.

On a tangent, Erlang cropped, not too many of us have had exposure to the language details, but Keith had done some spelunking and gave us a quick rundown of how it all hangs together, definitely something to keep an eye on - Andrew Peter's even called it his "Language of the year".

I brought up the MediaDefender debacle that’s been unravelling over the last week or so since their huge mail leak.  Not only are the inner workings of this organisation pretty unprofessional and certainly bordering on unethical for a corporation, but it highlights the risk most organisations run – the source of the leak being one employee who automatically forwarded all his work email to a gmail account, who then used the exact same credentials elsewhere.

Keith gave us some insight into LUA, including why it doesn’t make an attractive language to port to .Net, and why it’s so much easier to embed in a C++ based project as opposed to something like Ruby, which just seem to be designed for that purpose, especially with it's "out of the box" set of global variables.

I asked the question “What web load testing apps do you use” – and the answer was generally, we don’t!... I’ve been reviewing WebLOAD – which looks quite good, especially because the load machines can be deployed on a non Microsoft machine, such as Linux, making it easier to span a number of load machines out on something like Amazons elastic cloud (EC2), or low-cost machines without the hassle of O/S licensing.

We then moved onto talking about high-scale load testing, i.e. simulating a million simultaneous requests – somebody suggested that maybe you could hire whoever’s pulling the strings on the Storm BotNet – though I’m not sure they’re into self-promotion ;o)

We talked about social networking applications, and leveraging social networking platforms with API’s such as face book, this has been a popular topic over the last couple of months, including:
  • Finding the killer app(s) that could leverage an API like face book’s, and making money out of it.
  • Bad habits encouraged by these platforms, such as requesting your email account username and password to “email your friends”.
Alex James brought up the great series of posts that Rowan Simpson has been making lately – not everyone knew who Rowan was... so hopefully they should know now ;o) the posts walk through, the Trade me manifesto, and then drills into:
Over lunch we talked about many things, mostly non-IT related, but some things that did come up, including that the Mac OS isn’t going to have a NZ Daylight savings time update before Daylight savings actually kicks in, though it's not really a problem.

Some things that got mentioned which I didn’t quite catch the details of:

  • Metabase (I’m not sure I got the name right – couldn’t find a link, but apparently it’s a game / game development platform, in alpha development at the moment, where all game assets have an associated URL, and maybe an RSS Feed??) – Alex James suggested it might be interesting to combine something like that with a web based data storage platform, such as freebase, developed by Metaweb Tech.
  • Jason K (Knowles?) - who apparently supports the Idea of every user having there own domain, to cement their various web identities together, and where all their data is held by them in one place. – can anyone throw me the correct name and url?



posted @ Friday, September 21, 2007 12:22:34 AM (New Zealand Standard Time, UTC+12:00)    Comments [0] | Trackback |
 Thursday, July 26, 2007
About to run off to a meeting... so I'll make this quick.

Just read an interesting post by Donald Belcham on Refactoring Analysis with the help of nDepend... he's gone through the process of moving from .Net 1.1 to 2.0, then introducing interface based code, breaking classes apart to ensure they're enforcing the principle of single responsibility, moving to injecting dependencies, rolling his own primitive Inversion of Control container and finally employing the Castle Project's windsor container instead.

It pays to know a little bit about nDepend of course to understand the Abstractness vs Relational Cohesion figures... either take a look at the nDepend site, or maybe have a listen to the .Net Rocks podcast earlier this year which covered the same topic.

I'm not sure the figures actually answer questions so much as they ask them ... but it's interesting to see how much impact his first two rounds of refactoring had on the results.

I wonder how the figures would've come out had he executed his refactoring in a different order...  maybe DI first, then breaking the classes up based on responsibility, IoC and then finally introducing interfaces... hmmm

posted @ Thursday, July 26, 2007 1:57:28 AM (New Zealand Standard Time, UTC+12:00)    Comments [0] | Trackback |
 Saturday, July 14, 2007
Wasted 5 minutes figuring out what was going wrong, maybe this will save someone else the effort!

When using RhinoCommons and CastleProject both from trunk there's a version conflict with NHibernate.

RhinoCommons has NHibernate Version 2.0.0.1001 (Including support for things like Multi-criteria) ... and then the Castle Project is using NHibernate Version (1.2.0.4000) - this took me a little bit by surprise when I discovered that a monorail project I had was failing when attempting to databind properly when using the ARSmartDispatcherController... because I wasn't checking the binding errors collection, this was just failing silently... so a save would just load/save the existing record, without any binding.

Quick fix is just to use a assembly binding redirect in the web.config:

<runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

        <publisherPolicy apply="no"/>

        <dependentAssembly>

            <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />

            <bindingRedirect oldVersion="1.2.0.4000" newVersion="2.0.0.1001"/>

        </dependentAssembly>

    </assemblyBinding>

</runtime>




posted @ Saturday, July 14, 2007 1:13:35 AM (New Zealand Standard Time, UTC+12:00)    Comments [4] | Trackback |
 Wednesday, May 30, 2007
I've been getting a few questions about different aspects of the container tutorials series I posted a while back - while the series is not finished, neither is it forgotten, I've collated information regarding the series into a topic on my wiki and I'll keep this up to date until I've finished the series off.

I also notice Sam Gentile has posted that he's taking the plunge into learning about castle's Windsor container... good to hear :)


 |  | 
posted @ Wednesday, May 30, 2007 5:30:43 AM (New Zealand Standard Time, UTC+12:00)    Comments [0] | Trackback |
 Sunday, April 29, 2007
14_startable_facility.png

So this time we're going to have a look at the startable facility (you can find more info on it here in the wiki) - the concept of the startable facility is to ensure that components are started as soon as possible, which translates to as soon as all dependencies are available. 

Effectively what happens is you create a component, and implement a special interface called IStartable, which looks like this and is found in the Castle.Core assembly.

/// <summary>

/// Interface for components that wish to be started by the container

/// </summary>

public interface IStartable

{

    /// <summary>

    /// Starts this instance.

    /// </summary>

    void Start();

 

    /// <summary>

    /// Stops this instance.

    /// </summary>

    void Stop();

}


And then the startable facility detects any components registered in the container which implement the IStartable interface and determines if they have any dependencies which still need to be satisfied, if not, then the container will Resolve the component, and start it immediately (by invoking the IStartable.Start method on it) otherwise it's added to a list of services awaiting startup.

From then on, as components are registered in the container the startable facility will check the list of services awating startup, to see if any of them have all their dependencies available now, if so it will then Resolve the service and again invoke IStartable.Start on it.

Now at this point it's worth noting that Start is not executed asynchronously - if you block within the Start method then your container will halt loading the configuration etc. till it completes... be careful not to slow down your container startup too much with slow blocking operations with the Start() method.

So let's get into some code... first off I'm going to create a simple http listening service which is capable of serving up a single file... exciting stuff!

Here's the code:

public class LameHttpFileServer : IStartable

{

    private string _path;

    private HttpListener _listener;

    private Thread _listenerThread;

    private ILogger _logger;

 

    public LameHttpFileServer(string prefix, string path)

    {

        _path = path;

        _listener = new HttpListener();

        _listener.Prefixes.Add(prefix);

    }

 

    public ILogger Logger

    {

        get

        {

            if (_logger == null) _logger = NullLogger.Instance;

            return _logger;

        }

        set { _logger = value; }

    }

 

    public void Start()

    {

        Logger.Debug("Starting LameWebService...");

        _listener.Start();

        _listenerThread = new Thread(RequestWorker);

        _listenerThread.Start();

        Logger.Info("LameWebService Started.");

    }

 

    public void Stop()

    {

        Logger.Debug("Stopping LameWebService...");

        _listenerThread.Abort();

        _listener.Stop();

        Logger.Info("Stopped LameWebService.");

    }

 

    private void RequestWorker()

    {

        while (true)

        {

            HttpListenerContext context = null;

 

            try

            {

                context = _listener.GetContext();

            }

            catch (ObjectDisposedException)

            {

                return;

            }

 

            ServeContent(context.Response, _path);

        }

    }

 

    private void ServeContent(HttpListenerResponse response, string path)

    {

        string content = File.ReadAllText(path);

        byte[] buffer = Encoding.UTF8.GetBytes(content);

        response.ContentLength64 = buffer.Length;

 

        Stream output = response.OutputStream;

        output.Write(buffer, 0, buffer.Length);

        output.Close();

    }

}


Now, this class doesn't have any constructor dependencies (beyond a little configuration) but we do have a Setter dependency on an ILogger service - ILogger is actually a generic abstract of a logger for a logging framework (a logger being a named/scoped class supporting a number of generic message writing methods) out of the box Castle provides support for both nlog and log4net, however it also contains some lightweight loggers, such as a console logger, which means you can avoid having to write any logging configuration files... so we're going to use that this time... because the loggers are assigned by the container, there's not need to actually reference the type the logger is logging "for", because the container knows that implicitly.

Now let's have a look at the container configuration:

<configuration>

 

  <configSections>

    <section name="castle"

        type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />

  </configSections>

 

  <castle>

    <facilities>

      <facility id="startable.facility"

        type="Castle.Facilities.Startable.StartableFacility, Castle.MicroKernel" />

 

      <facility id="logging.facility"

        type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"

        loggingApi="console" />                         

 

  </facilities>

    <components>

 

      <component id="serves->lame.html"

                type="IoC.Tutorials.Part14.LameHttpFileServer, IoC.Tutorials.Part14">

        <parameters>

          <prefix>http://*:8089/</prefix>

          <path>lame.html</path>

        </parameters>

      </component>

 

    </components>

  </castle>

 

</configuration>


As you can see we have declared two facilities, one for handling the auto-wiring of Loggers into any components with ILogger constructor parameters or setter dependencies, and secondly the startable facility, which will take care of automatically starting our LameHttpFileServer for us.

Now, notice we're referencing a file called lame.html - heres what that looks like:

<html>

    <body>

        <h1>Lame.html</h1>

        <h2>Welcome to lame.html file</h2>

        <p>This is the contents of the lame.html file, neat huh?</p>

    </body>

</html>


Now done to business, let's look at the program that's using this server...

private static void Main(string[] args)

{

    WindsorContainer container = new WindsorContainer(new XmlInterpreter());

 

    GetPage();

 

    container.Dispose();

 

    GetPage();

 

    Console.Read();

}

 

private static void GetPage()

{           

    Console.WriteLine("\r\nClient: requesting http://localhost:8089/ ...");

    try

    {

        WebClient client = new WebClient();

        string content = client.DownloadString("http://localhost:8089/");

        Console.WriteLine("Client: success, content follows.\r\n\r\n {0}", content);

    }

    catch (WebException ex)

    {

        Console.WriteLine("Client: Exception occured, message: {0}", ex.Message);

    }

}


As you can see what we're doing is:

  1. Constructing the container using the configuration.
  2. Calling the GetPage method, which will attempt to download the lame.html file from our server (which the container should have started automatically for us)
  3. Disposing of the container (any guesses what that will do?)
  4. And finally calling the GetPage method again, to see if our server is still running.
So, running the program gives us these results:

[Debug] 'IoC.Tutorials.Part14.LameHttpFileServer' Starting LameWebService...
[Info] 'IoC.Tutorials.Part14.LameHttpFileServer' LameWebService Started.

Client: requesting http://localhost:8089/ ...
Client: success, content follows.

 <html>
    <body>
        <h1>Lame.html</h1>
        <h2>Welcome to lame.html file</h2>
        <p>This is the contents of the lame.html file, neat huh?</p>
    </body>
</html>

[Debug] 'IoC.Tutorials.Part14.LameHttpFileServer' Stopping LameWebService...
[Info] 'IoC.Tutorials.Part14.LameHttpFileServer' Stopped LameWebService.

Client: requesting http://localhost:8089/ ...
Client: Exception occurred, message: Unable to connect to the remote server

As you can see before we disposed of the container our request for the file worked just fine, but after the container had been disposed, it appears the file service has been stopped, and so our client was unable to connect... what caused this?

Well the way the startable service is implemented, it actually applies a couple of lifecycle concerns - a concern is effectively a bit of code that executes when what it's concerned in occurs, in these case the startable facility adds two concerns to a startable component's model.

The first "concern" is a commission concern, which invokes the Start method upon the component entering the commission step of it's lifecycle, which is immediately after the component instance has been constructed and all it's dependencies and configuration injected. 

The second concern is a "decommission" concern, which occurs upon the component is being decommissioned (ie. disposed of, released or purged from the container) this happens to all components the container is holding onto when it is disposed, or when an individual component is released - however in the case of components with a singleton lifestyle, you can't actually release the component prior to disposing of the container (because that would violate the lifestyle) ... so doing something like this:

LameHttpFileServer server = container.Resolve<LameHttpFileServer>();

container.Release(server);


In place of disposing the container won't stop the service if it's a singleton....

One last though - what would happen if we changed our components lifestyle to [Transient] instead of the default of singleton, let's create a simple class for testing this idea:

[Transient]

public class StartableExperiment : IStartable

{

    private static int _count;

    private int _number;

 

    public StartableExperiment()

    {

        _number = ++_count;

    }

 

    public void Start()

    {

        Console.WriteLine("Started #{0}", _number);

    }

 

    public void Stop()

    {

        Console.WriteLine("Stopped #{0}", _number);

    }

}


Now let's write some code to test it... we'll just register the component in code, to avoid having to change our config...

Here's the program itself...

WindsorContainer container = new WindsorContainer(new XmlInterpreter());

 

container.AddComponent("exp", typeof(StartableExperiment));

 

StartableExperiment exp1 = container.Resolve<StartableExperiment>();

container.Release(exp1);

StartableExperiment exp2 = container.Resolve<StartableExperiment>();

container.Release(exp2);

 

container.Dispose();


And the results:

Started #1
Started #2
Stopped #2
Started #3
Stopped #3
Stopped #1


So what's happened is that immediately upon registration a component is created... and can't be disposed (because we can't get a hold of the instance to release it) until the container is disposed... but for the couple of calls to Resolve and Release you can see the StartableExperiment instances are started & stopped during commission & decommission steps in the components life cycle as expected.

So that brings this look at the startable facility to an end... next time we might have another quick look at life cycles to see how else we can use this concept when build components, and maybe after that we'll have a look at some of other lifestyles which are available "out of the box" and how to write our own.
posted @ Sunday, April 29, 2007 4:51:25 AM (New Zealand Standard Time, UTC+12:00)    Comments [3] | Trackback |
 Saturday, April 28, 2007
13_injecting_service_arrays.png
So this time we're going to look at injecting service arrays ... if you haven't read part 12, it might pay to have a look at that first - this part follows on from the examples presented there.

So this should be reasonably quick, we're just going to rework the Part 12 solution so we don't need decorators... why would we do this, well in this case I feel that rewiring decorators (ie. changing orders, or disabling one of the decorators temporarily) isn't completely intuitive, especially when you have a long chain, it's easy to make mistakes.

So what's the plan, well first off let's implement an abstract class for our calculators:

public abstract class AbstractCalculator

{

    public abstract decimal Calculate(decimal currentTotal, Order order);

}


Now we can rework the existing decorators and default calculator to be implementations of the abstract caculator, so here goes:

First off the the logic in the DefaultCostCalculator will be moved into a class called TotalCalculator :

public class TotalCalculator : AbstractCalculator

{

    private decimal CalculateTotal(Order order)

    {

        decimal total = 0;

 

        foreach (OrderItem item in order.Items)

        {

            total += (item.Quantity*item.CostPerItem);

        }

 

        return total;

    }

 

    public override decimal Calculate(decimal currentTotal, Order order)

    {

        return currentTotal + CalculateTotal(order);

    }

}


And then for the gst calculator, we do much the same thing:

public class GstCalculator : AbstractCalculator

{

    private decimal _gstRate = 1.125m;

 

    public decimal GstRate

    {

        get { return _gstRate; }

        set { _gstRate = value; }

    }

 

    private bool IsNewZealand(Order order)

    {

        return (order.CountryCode == "NZ");

    }

 

    public override decimal Calculate(decimal currentTotal, Order order)

    {

        if (IsNewZealand(order))

        {

            return (currentTotal*_gstRate);

        }

 

        return currentTotal;

    }

}


And finally the shipping calculator:

public class ShippingCalculator : AbstractCalculator

{

    private decimal _shippingCost = 5.0m;

    private decimal _fragileShippingPremium = 1.5m;

 

    public decimal ShippingCost

    {

        get { return _shippingCost; }

        set { _shippingCost = value; }

    }

 

    public decimal FragileShippingPremium

    {

        get { return _fragileShippingPremium; }

        set { _fragileShippingPremium = value; }

    }

 

    private decimal GetShippingTotal(Order order)

    {

        decimal shippingTotal = 0;

 

        foreach (OrderItem item in order.Items)

        {

            decimal itemShippingCost = ShippingCost*item.Quantity;

            if (item.IsFragile) itemShippingCost *= FragileShippingPremium;

            shippingTotal += itemShippingCost;

        }

 

        return shippingTotal;

    }

 

    public override decimal Calculate(decimal currentTotal, Order order)

    {

        return currentTotal + GetShippingTotal(order);

    }

}


Now, our original DefaultCostCalculator is reworked to use an array of AbstractCalculators to compute the total cost for an order:

public class DefaultCostCalculator : ICostCalculator

{

    private AbstractCalculator[] _calculators;

 

    public DefaultCostCalculator(AbstractCalculator[] calculators)

    {

        _calculators = calculators;

    }

 

    public decimal CalculateTotal(Order order)

    {

        decimal currentTotal = 0;

 

        foreach (AbstractCalculator calculator in _calculators)

        {

            currentTotal = calculator.Calculate(currentTotal, order);

        }

 

        return currentTotal;

    }

}


Notice it takes an array of AbstractCalculator's as a constructor dependency... just like we did for configuration parameters in the early parts of this container tutorial series, we can actually pass arrays of dependencies to a component as well, so last of all let's see the container configuration:

<configuration>

 

  <configSections>

    <section name="castle"

        type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />

  </configSections>

 

  <castle>

    <components>

 

      <component id="calc.gst"

                service="IoC.Tutorials.Part13.AbstractCalculator, IoC.Tutorials.Part13"

                type="IoC.Tutorials.Part13.GstCalculator, IoC.Tutorials.Part13">

        <parameters>

          <GstRate>1.20</GstRate>

          <!-- GST is now 20% -->

        </parameters>

      </component>

 

      <component id="calc.shipping"

          service="IoC.Tutorials.Part13.AbstractCalculator, IoC.Tutorials.Part13"

          type="IoC.Tutorials.Part13.ShippingCalculator, IoC.Tutorials.Part13">

        <parameters>

          <FragileShippingPremium>0.0</FragileShippingPremium>

          <!-- No fragile goods premium any more-->

        </parameters>

      </component>

 

      <component id="calc.total"

        service="IoC.Tutorials.Part13.AbstractCalculator, IoC.Tutorials.Part13"

          type="IoC.Tutorials.Part13.TotalCalculator, IoC.Tutorials.Part13" />

 

      <component id="costCalculator.default"

                service="IoC.Tutorials.Part13.ICostCalculator, IoC.Tutorials.Part13"

                type="IoC.Tutorials.Part13.DefaultCostCalculator, IoC.Tutorials.Part13">

        <parameters>

          <calculators>

            <array>

              <value>${calc.total}</value>

              <value>${calc.shipping}</value>

              <value>${calc.gst}</value>

            </array>

          </calculators>

        </parameters>

      </component>

 

    </components>

  </castle>

 

</configuration>


Notice the default caculator now takes our 3 calculators as members of an array, and if we want to change the order in which they are evaluated, it's trivial to just move them around within the <array /> node, or to comment certain ones out.

And just to demonstrate that nothing has changed, the code for the program in part 13 is identical to part 12:

private static void Main(string[] args)

{

    WindsorContainer container = new WindsorContainer(new XmlInterpreter());

 

    Order order1 = new Order();

    order1.CountryCode = "NZ";

    order1.Items.Add(new OrderItem("water", 10, 1.0m, false));

    order1.Items.Add(new OrderItem("glass", 5, 20.0m, true));

 

    Order order2 = new Order();

    order2.CountryCode = "US";

    order2.Items.Add(new OrderItem("sand", 50, 0.2m, false));

 

    ICostCalculator costCalculator = container.Resolve<ICostCalculator>();

    Console.WriteLine("Cost to deliver Order 1: {0}", costCalculator.CalculateTotal(order1));

    Console.WriteLine("Cost to deliver Order 2: {0}", costCalculator.CalculateTotal(order2));

 

    Console.Read();

}


And the results are the same too:

Cost to deliver Order 1: 192.0000
Cost to deliver Order 2: 260.0


Beautiful :)

The next part will have a quick look at the "Startable" facility, enjoy.





posted @ Saturday, April 28, 2007 1:52:38 AM (New Zealand Standard Time, UTC+12:00)    Comments [0] | Trackback |
12_decorators.png
So... Decorators (for those who don't know, or don't remember) are a classic design pattern - have a look at the wikipedia entry if your curious on doing any background reading.

Up until now we've been creating abstractions and swapping implementations, but here we are going to chain implementations... so for this example we have an Order class which contains a bunch of order items, here's how that looks:

Order.cs

public class Order

{

    private string _countryCode;

    private readonly List<OrderItem> _items = new List<OrderItem>();

 

    public List<OrderItem> Items

    {

        get { return _items; }

    }

 

    public string CountryCode

    {

        get { return _countryCode; }

        set { _countryCode = value; }

    }

}


OrderItem.cs

public class OrderItem

{

    private string _name;

    private bool _isFragile;

    private int _quantity;

    private decimal _costPerItem;

 

    public OrderItem(string name, int quantity, decimal costPerItem, bool isFragile)

    {

        _name = name;

        _quantity = quantity;

        _costPerItem = costPerItem;

        _isFragile = isFragile;

    }

 

    public bool IsFragile

    {

        get { return _isFragile; }

        set { _isFragile = value; }

    }

 

    public int Quantity

    {

        get { return _quantity; }

        set { _quantity = value; }

    }

 

    public decimal CostPerItem

    {

        get { return _costPerItem; }

        set { _costPerItem = value; }

    }

 

    public string Name

    {

        get { return _name; }

        set { _name = value; }

    }

}


And now we have an interface that a class can implement to calculate the cost of an order:

public interface ICostCalculator

{

    decimal CalculateTotal(Order order);

}


And last of all we have an implementation of the cost calculator, which calculates the base cost of an order based on quantities and per-item costs:

public class DefaultCostCalculator : ICostCalculator

{

    public decimal CalculateTotal(Order order)

    {

        decimal total = 0;

 

        foreach (OrderItem item in order.Items)

        {

            total += (item.Quantity*item.CostPerItem);

        }

 

        return total;

    }

}


Pretty simple, so what about the program itself... well here it is, we just have 2 different orders we are calculating the total cost for:

internal class Program

{

    private static void Main(string[] args)