briefly hacked...


My aging dasBlog install got hacked last night some time... by the turks ;o) all back up and running again now, upgraded to dasBlog 1.9 as well... might migrate to a different blog platform at some point (either Subtext, or I might give nBlogr a whirl because I know the technology stack it sits on reasonably well.

Read More

Annotations - here's the code

Here's the code for my last post on Annotations - I tidied up a few things up (it's still very basic but it does work) there's a single test fixture to give you a guide for usage... so things like:

    [Test]
public void QueryStoreForClassAnnotationsWithCertainKey()
{
ClassA target1 = new ClassA();
ClassA target2 = new ClassA();
ClassA target3 = new ClassA();
target1.Annotate(Description => "class number 1");
target2.Annotate(Description => "class number 2");
target3.Annotate(Parsed => true);

var results = AnnotationStore.Classes
.Where(a => a.HasKey("Description"))
.ToList();

Assert.AreEqual(2, results.Count);
}

And also the equivalent thing is possible for members... though I suspect annotating members isn't all that useful in most cases...

  [Test]
public void QueryStoreForMemberAnnotations()
{
ClassA target1 = new ClassA();
ClassA target2 = new ClassA();
ClassA target3 = new ClassA();

target1.Annotate(() => target1.FirstName,
CamelCase => true); // annotating a property
target1.Annotate(() => target1.Field,
Ignored => true); // annotating a field
target2.Annotate(() => target2.Execute(),
Parsed => true); // annotating a method
target3.Annotate(Parsed => true);

var results = AnnotationStore.Members
.Where(p => p.HasKey("CamelCase"))
.ToList();

Assert.AreEqual(1, results.Count);
}

Read More

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