Annotations...

So I've been mulling some ideas over after the whole abusing
lambdas for Hash table construction (here
and here) ... and after reading a post on Jb
Evain's blog
I decided to create a little bit of code for
doing annotations... so given a class say:

public class ClassA

{

    public int Id { get; set; }

    public string Name { get; set; }

}

You can then do this kind of thing (assuming you've added the
apropriate namespace where the Annotations static class
resides in)

[Test]

public void AnnotateClass()

{

    ClassA classA = new ClassA();

    classA.Annotate(IsValid => false);

    classA.Annotate(MapsToTable => "TblClassA",
Key => "Id");

    classA.Annotate(Roles => new []
{"Administrator", "User"});

   
Assert.IsFalse(classA.Annotation("IsValid"));

    Assert.AreEqual("TblClassA",
classA.Annotation("MapsToTable"));

    Assert.AreEqual("Id",
classA.Annotation("Key"));

}

Or, perhaps you want to attach some annotations to a
specific property... no problem!

[Test]

public void AnnotateProperty()

{

    ClassA classA = new ClassA();

    classA.Annotate(() => classA.Name,
CanBeNull => true);

    bool canBeNull =
classA.Annotation(() => classA.Name,
"CanBeNull");

    Assert.AreEqual(true, canBeNull);

}

Under the hood the values are stored against a dictionary where
the keys (in this case the instance of classA) are weak
referenced... so once classA is garbage collected the entries in
the dictionary will also dissapear in time (next time any method
touches the dictionary).

The nice thing is obviously you can directly interogate the
Annotations static class itself with a query expression to say
find all objects with a certain annotation.

Read More

more mucking about with hashes...

So, I got a comment on the last post about hashes from
lambdas
(from Andrey
Shchekin
)... It pointed out the fact that you don't need to
use expressions at all... which hilights an observation that I
hadn't made myself - such that the lambda parameter
names are available in the generated delegate... which of
course makes perfect sense!

So given:

Func func = Name => "Value";

You can get the lambda parameter "Name" from the function
delegate by calling:



func.Method.GetParameters()[0].Name (would
return "Name")



Here's the revised Hash method from Andrey:

public Dictionary Hash(params
Func[] args)

where T : class

{

    var items = new Dictionary<>
T>();

    foreach (var func in args)

    {

        var item =
func(null);

       
items.Add(func.Method.GetParameters()[0].Name, item);

    }

    return items;

}

very elegant and simple :)

He even did some stats, which I suspect are probably a lot more
accurate then my inital observations:



For 10000 consecutive calls:









WithAdd
10.0144ms
WithLambdas
9713.968ms
WithLambdasConstantsOnly
240.3456ms
WithDelegates
30.0432ms
Now what about multiple parameters... so far I can't think
of any uses I would have for it... perhaps a 2 level configuration
dictionary?

[Test]

public void HashTwoLevelDict()

{

    Dictionary<>
object>> config = this.Hash(

     (Connection, DriverClass) =>
typeof(SqlClientDriver),

     (Dialect, DialectClass) =>
typeof(MsSql2000Dialect),

     (Connection, Provider) =>
typeof(DriverConnectionProvider),

     (Connection, ConnectionString)
=>"Data Source=.;Initial Catalog=test;Integrated
Security=SSPI");

    Assert.AreEqual(typeof(SqlClientDriver),
config["Connection"]["DriverClass"]);

}

Who knows... I look forward to seeing how Lambdas get used and
abused for non-functional programming tasks :)
Read More

mucking about with hashes...

So, I got a comment on the last post about hashes from lambdas (from Andrey Shchekin)... It pointed out the fact that you don't need to use expressions at all... which hilights an observation that I hadn't made myself - such that the lambda parameter names are available in the generated delegate... which of course makes perfect sense!

So given:

Func func = Name => "Value"; 

You can get the lambda parameter "Name" from the function delegate by calling:
func.Method.GetParameters()[0].Name // would return "Name" 

Here's the revised Hash method from Andrey:
public Dictionary Hash(params Func[] args)
where T : class {
var items = new Dictionary();
foreach (var func in args) {
var item = func(null);
items.Add(func.Method.GetParameters()[0].Name, item);
} return items;
}

Very elegant and simple :)

He even did some stats, which I suspect are probably a lot more accurate then my inital observations:

For 10000 consecutive calls:







WithAdd 10.0144ms
WithLambdas 9713.968ms
WithLambdasConstantsOnly 240.3456ms
WithDelegates 30.0432ms

Now what about multiple parameters... so far I can't think of any uses I would have for it... perhaps a 2 level configuration dictionary?
[Test]
public void HashTwoLevelDict()
{
Dictionary> config = this.Hash((Connection, DriverClass) => typeof(SqlClientDriver), (Dialect, DialectClass) => typeof(MsSql2000Dialect), (Connection, Provider) => typeof(DriverConnectionProvider), (Connection, ConnectionString) =>"Data Source=.;Initial Catalog=test;Integrated Security=SSPI");
Assert.AreEqual(typeof(SqlClientDriver), config["Connection"]["DriverClass"]);
}

Who knows... I look forward to seeing how Lambdas get used and abused for non-functional programming tasks :)

Read More

Container tutorials... not yet forgotten

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 :)

Read More