Part 2 - Array Configuration



So welcome to part 2, today we're going to look at some more config - this time it's arrays...



So we have a class, that tells us when we can down tools and go to the beach...


public class HolidayService
{
private DateTime[] _holidays;

public DateTime[] Holidays
{
get
{
return _holidays;
}
set { _holidays = value; }
}

public bool IsHoliday(DateTime date)
{
if (_holidays != null)
{
DateTime matchDate = date.Date;
foreach (DateTime test in Holidays)
{
if (test.Date.Equals(matchDate))
{
return true;
}
}
}

return false;
}
}



We can set the days we are allowed off by adding dates to the holidays array...



So lets look at our code for running this service:


internal class Program
{
private static void Main(string[] args)
{
WindsorContainer container = new WindsorContainer(new XmlInterpreter());

HolidayService holidayService = container.Resolve();

DateTime xmas = new DateTime(2007, 12, 25);
DateTime newYears = new DateTime(2008, 1, 1);

if (holidayService.IsHoliday(xmas))
Console.WriteLine("merry xmas!");
else
Console.WriteLine("xmas is only for management!");

if (holidayService.IsHoliday(newYears))
Console.WriteLine("happy new year");
else
Console.WriteLine("new year, you haven't done all the work for last year!");

Console.Read();
}
}



The service configuration is just like last time, we have the component registered with no parameters... so if we run the program we shall see on the screen:



xmas is only for management!

new year, you haven't done all the work for last year!


Mum will be dissapointed, she'd done a roast 'n all... so lets get some configuration in their so we can have a few days off...




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





2007-12-24
2007-12-25
2008-1-1








Now let's see if we're going to have a merry xmas and a happy new year - a quick run of our application....



merry xmas!

happy new year



You don't just have to use arrays either, you can do lists too - though I didn't show them here, just use list instead of array in the xml configuration...


See you for part 3 where we do dictionary configurations.
Read More

Part 1 - Simple configuration


Welcome to part 1... today we're looking at supporting basic configuration in components without additional coding by using the windsor container.

So - with the container we can provide configuration for a component which is loaded at runtime, so every time you start your app it will be read in - you can do this using the configuration support in .Net - but using castle makes support for this very code-light in comparison... let's take a look.



So first off, we have a class we can use to calculate the tax on a gross ammount:


public class TaxCalculator
{
private decimal _rate = 0.125m;

public decimal Rate
{
set { _rate = value; }
get { return _rate; }
}

public decimal CalculateTax(decimal gross)
{
return Math.Round(_rate*gross, 2);
}
}



By default the tax rate is 12.5% - but we can change the tax rate by setting the "Rate" property.



Now, lets look at setting up the container... so we have this code in our sample:


private static void Main(string[] args)
{
WindsorContainer container = new WindsorContainer(new XmlInterpreter());

TaxCalculator calculator = container.Resolve();

decimal gross = 100;
decimal tax = calculator.CalculateTax(gross);

Console.WriteLine("Gross: {0}, Tax: {1}", gross, tax);
Console.Read();
}



The windsor container is constructed with an "XmlInterpreter" - this configuration will pull the setup for our container from a section in the App.config.



Let's have a look at that:




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








Running the program will display: Gross: 100, Tax: 12.50



Now, what about changing the tax rate in the configuration?




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



0.25





Now running the program will display: Gross: 100, Tax: 25.00



And that's part 1 done - so now you can see how can we supply configuration parameters, and provide sensible defaults for them.

Next time we'll look at configuring arrays...

Read More

2007-04-15 - Architecture Camp 2007

Well it's over - and it's been great!

First off Architecture Camp 2007 was a lot of fun to present at this weekend... the relaxed nature and opportunities for everyone to talk, rather than just listening, was great - a nice soft introduction to a newbie presenter such as myself!

And a big hat-tip to all the guys who helped put this together, including Alex James, Kurt (where's your blog boy!) and Chris + all the other presenters.

The thing I liked the most about this event was the number of people who brought their a-game... damn but there were some seriously experienced and intelligent guys in the audience, asking great questions - contrasted with the often pointless heckling and misguided questioning you can get at events like teched, this event was great ... if you couldn't make it, definitely try to get to the next architecture camp, and if you were a no-show ... shame on you!

I'm still not 100% sure if my session got recorded or not, but if it did, I'll point you to it at some point...  in the mean time - here's the presentation itself in both pdf and pptx (powerpoint 2007) formats.

Read More

Codeplex... Damned if you do... Damned if you dont...

In a follow up to this  post I made yesterday, it seems some people are annoyed that a Codeplex command line client exists at all...


I still can not help but feel some people are missing the point of Codeplex all together... as I see it (and these are only my opinions on the matter) - Codeplex:
  • Supersedes the aging gotdotnet.
  • Demonstrates TFS features
    • Scalability for high volumes of users (not necessarily usability, but they're working on that)
    • Case study for any large organizations considering deploying it across their organization.


  • Provides a place Microsoft can.
    • Host open-source projects for life.

    • Control and support the environment.
    • Keep a single message across their organization - we use TFS for source control from now on.




(At this point it's worth noting I'm a SVN/Trac/CC.Net guy myself... I can't actually justify the expenses of TFS at the moment)

For me the reasons to use codeplex over another open source provider are simply:

  • It's different / new.
  • It has a strong Microsoft / .Net focus.
  • It's easier to get going for a newbie .Net developer (integrated source control in VS.Net, simple wiki pages baked-in)
  • You can play with TFS.


They are reasons, but hardly "compelling" ones ;o)

The beta command line client is a good thing, but to counter that "6 months wasted effort" with a call to replace TFS with subversion in Codeplex ... that just seems counter-intuitive to me... suddenly you have a roll-on effect of zero integration between source control and the in-built issue tracking mechanisms... so what, lets replace that with Trac??... and now that comes with it's own wiki... so ditch the existing one... and suddenly Codeplex is nothing more then a shell for managing file releases with some forums.

What kind of message would it send to microsoft customers who have invested time and effort into team system... let's be reasonable, it's just not going to happen, so whats the next logical step... improve the tooling to work with the existing source control solution... first step to achieving that... build a decent command line client!

So... if you are looking for subversion based hosting why not use well established players who have been supporting open source from the beginning like SourceForge or tigris... don't they still deserve your love now more then ever since Codeplex is on the scene... what better way to vote than with your choice of OSS project hosting?

Codeplex is an alternative, not a replacement, for the likes of Sourceforge... perhaps what we need is a OssHostMatrix, much like WikiMatrix that compares
feature for feature so you can pick the host that's appropriate to your project on features alone, rather then bitching about what's missing in Codeplex, when it's freely available elsewhere...

I'm starting to feel sorry for some of the codeplex guys (like

Jonathan Wanagel, who I commend for being vocal about what's going on at codeplex... he seems to pop up on a lot of blogs I read) ... they certainly do seem a little damned if you, damned if you don't at the moment - especially when progress is slowly being made and demonstrated.

Read More

Some notes on creating your own license keys

If you have to make your own license keys i.e.

ABCDE-ABCDE-ABCDE-ABC1Z-A32DE

Here's some thoughts/ideas for things you can do to break up the key space a little so people can not easily brute-force your keys by fiddliing just a couple of numbers.

  • Select a range of numbers and letters that can be directly mapped to 32 values (10 digits and 22 alpha maybe?).
  • Know your maths.. creating helper classes for unpacking and packing bytes will help.
  • In this example you could carry 125bits (15 bytes and just over a nibble ;o) ... or 13 bytes + CRC16 (or Maybe an Adler16).
  • Use lookup tables to translate sequential values into pseudo-random values.
  • Break any multi-byte values up into individual bytes, stuff em around your payload of bytes in non-sequential offsets.
  • Calculate the CRC for your payload and include it as part of the key's data, verify it when you're decoding.
  • You can use structs with a sequential layout and a packing of 1 to define your structure...
  • Use Marshal.Copy etc. methods to copy between the struct and a byte array.
  • Remap individual bits in the overall payload.


The last one is something that works really well... it's hard
to avoid certain digits "sticking" when generating sequential keys
no matter what you do... but if you remap the individual bits of a
15 byte array (i.e. using a lookup table containing 120 indexes)
you end up with very unique looking keys that have no sequential behaviour to them (especially if you redistribute the bits of the CRC into all the bytes of the key, which in turn are repacked into 5 bit letters).
Read More