Archive for 2009

Side Effect Free Retrieval Pattern

Comments

A colleague of mine and I were discussing good verb replacements for “GetOrCreate” data retrieval patterns, where the “Create” part is responsible for the instantiation of a new instance of something.  While the pattern I present here did not solve his particular problem, its at least worth sharing.  Its obvious, and probably in use commonly already, but I figure its worth noting.

Essentially the goal is to get away from a “GetOrCreate” master method call and just have two methods: Get(args) and Get(args, Foo default) and then leave the responsibility of default generation up to Foo.  This way there’s no unintended side effects and you don’t have to explicitly coalesce (though with this pattern you could as easily coalesce; strictly speaking it isn’t as “discoverable”)…

var foo = FooService.Get(23) ?? new Foo { ... };

So here’s the pattern defined in code:

public class Foo : ICloneable
{
    private readonly static Foo __default =
        new Foo { Bar = "...", Baz = Int32.MinValue };

    public string Bar { get; set; }
    public int Baz { get; set; }

    public static Foo Default()
    {
        return (Foo)__default.Clone();
    }

    public object Clone()
    {
        return new Foo { Bar = this.Bar, Baz = this.Baz };
    }
}

public class FooService
{
    public Foo Get(int baz)
    {
        // retrieve from data store...
        return null;
    }

    public Foo Get(int baz, Func<Foo> @default)
    {
        return Get(baz) ?? @default.Invoke();
    }
}

You’ll see that any associated overhead with Clone only occurs if Get(baz) returns null.  I have specifically stepped around the common naming standard of “GetDefault()” for the function – its named like a property - because its intended usage is as follows:

var foo = new FooService().Get(12, Foo.Default);


An ICloneable reference type is pretty much required, though any immutable reference type could get by without cloning because any changes gives you a new copy of that type.


Khorne Berzerkers

Comments

Some Khorne Berzerkers I finished up after having them assembled and sitting in a box for over six years.  Also trying out my new camera and figuring out how to take pictures.

 
Khorne Berzerkers Khorne Berzerkers (All)


Hordes: Lord of the Feast

Comments

One of the coolest Hordes models and also one of the weakest.  I painted this a long time ago and its one of the funnest paint jobs I've done.  When I use this model its only because it looks awesome and intimidating.

Lord of the Feast 

Update: a much better photo.


Warhammer 40K Tyranids

Comments

I painted these 8 horma-terma-whatever-gaunts in Hive Fleet Leviathan paint scheme more than a year ago. They came out fairly well, but it took about twelve hours to do. Upon completion I determined I shall not have a Tyranid force painted in Leviathan color scheme. Instead I'm going to do something that involves an airbrush and wood stain.

Tyranids Wide

Tyranids Pair


Trying out SyntaxHighligher and PreCode

Comments

Just making sure this is correct.  By this I mean PreCode (requires Windows Live Writer 2009) with SyntaxHighligher setup.  PreCode is also a stand alone program.

 

public bool Validate(IValidationDictionary modelState, string prefix)
{
    // xVal example code
    var dataAnnotationErrors = from prop in TypeDescriptor.GetProperties(_entity).Cast<PropertyDescriptor>()
           from attribute in prop.Attributes.OfType<ValidationAttribute>()
           where !attribute.IsValid(prop.GetValue(_entity))
           select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), _entity);

    var brokenRules = GetBrokenRules();

    if (!String.IsNullOrEmpty(prefix))
        prefix += ".";

    if (dataAnnotationErrors.Any())
        dataAnnotationErrors.ForEach(ei => modelState.AddError(prefix + ei.PropertyName, ei.ErrorMessage));
    if (brokenRules.Any())
        brokenRules.ForEach(rule => modelState.AddError(prefix + rule.Property, rule.Message));

    return modelState.IsValid;
}

 

There is a “Fix Indentation” button in PreCode.  I am in love.

Now just to setup the clipboard SWF thing.

I should seriously consider a theme with a wider content area.