Caramel – Alpha source code is public

I have added the Caramel code generator to my public repository at google code.

Please note that this is very very alpha and not actually useful yet.

Before it can be used I need to finish the template support and fully implement the DB meta data importer.

But if you are interested in this kind of stuff and want to help out building it, please let me know.

Hidden gem: BuildManager.GetType

I’ve been digging through the ObjectDataSource today and I was trying to figure out how they created the datasource instance from the type name.

Now some clever reader might say “I know, I know, Type.GetType(string)”… But that’s wrong..
Type.GetType(string) requires full type names with assembly name and the whole shebang.

The ObjectDataSource is able to create instances w/o all that information.
So after a while of digging I found a precious gem, hidden in System.Configuration:

Very very nice :-)

I can now inherit ObjectDataSource and fetch the type via buildmanager and then call NAspect to proxy the type and get an AOP enabled datasource.

Thats all for now, just wanted to share this one :-)

Typed factories in C#3 – Managed “new”

One thing that always bothered me with factory methods in earlier versions of C# is that the only way to make a generic factory method, you have to pass in all the constructor args as an object array.

A normal factory method might look something like this:

public T Create<T>(params object[] args) 

And you invoke it with:

Person person = MyFactory.Create<Person>(arg1,arg2,arg3);

When doing so, you have no clue what args you can pass to it, there is no intellisense here, we only know that our Create method takes a dynamic numer of args.

So wouldn’t it be sweet if we could make a factory that knows what args you can pass to it?
I think so, and I found a solution for it today.

I solved it like this:

Person person = MyFactory.Create(() => new Person("Roger","Alsing"));

As you see, instead of passing an object array to my create method, I pass it a lambda expression.
The Create method can then examine the expression tree from the lambda, and do whatever it want with the parameters.

The code for dealing with this looks like this:

static T Create<T>(Expression<Func<T>> exp) 
    LambdaExpression lambda = exp as LambdaExpression; 
    NewExpression body = lambda.Body as NewExpression;                 
    List<object> argsList = new List<object>(); 
    foreach (var arg in body.Arguments) 
        //create a lambda to wrap up the expression for "arg" 
        LambdaExpression argLambda = Expression.Lambda(arg); 
        //compile the lambda 
        var argDel = argLambda.Compile(); 
        //get the arg value by invoking the compiled lambda 
        object argValue = argDel.DynamicInvoke(); 
        //add the raw value to the arg list 
    object[] argsArr = argsList.ToArray();     
    //this is where you might want to modify. 
    //eg, make it create some proxy type instead 
    //this is an naive example so I will just create an instance 
    //of the original type and return it.. 
    T instance = (T)Activator.CreateInstance(typeof(T), argsArr); 
    return instance; 

This way I could make typed factory calls for eg. NAspect or NPersist and let the factory return an AOP proxy that is instanced with the parameters from the lambda expression.

Or you could use it in order to describe different singletons, create singletons based on the inparams.
Patrik Löwendahl blogged about singleton patterns today
You can do the same with this approach, but with the benefit that you get typed inparams for the constructor.

You could say that this becomes a form of managed “new”, where you can write a “new” expression and benefit from intellisense and all that, and still be able to override the “new” and return other types or whatever you want.

Hope you like it :-)

An improved version of this with support for property/field initializers can now be found at:

AlbinoHorse – Class Designer

I have published my class designer component at CodePlex.

You can find binaries and sourcecode here:

AlbinoHorse is a Windows Forms control for UML Class diagrams that I’m currently developing.
The idea is to use it in our next version of ObjectMapper. (Mapper tool for ORM entities)



MyLisp is now named DSLisp – Domain Specific (Language) Lisp.

I have published it as a project on CodePlex:

The new name imples the purpose of the project.
To act as a host for DSL’s.

The idea is to compile your DSL into the DSLisp AST.
And then run your DSL inside the DSLisp engine.
By doing this you can very easily add single step debugging and breakpoint support to your DSL. 

See the codeplex site for more info.

Adding Linq support to NPersist

I’m currently adding some real Linq support to NPersist.
I did start on it when the first public previews of Linq was released, but then I was kind of stuck in World of Warcraft for a while (OK, slightly more than a while).
But I’ve finally broken out of the world of Azeroth and I’m back in the real world again.

So, here is a few sneak peeks of what will be possible:

 var res = from cust in ctx.Repository<Customer>() 
          where (from order in cust.Orders 
                 where order.OrderDate == 
                 new DateTime(2008,01,01) && order.Total == 3.1 
                 select order).Count > 0 
          select cust;

 The Linq query will be turned into our own DSL NPath :

select * 
from Customer 
where ((( 
	select count(*) 
	from Orders 
         where ((OrderDate = #2008-01-01#) and (Total = 3.1)) 
        ) > 0))

The NPath query will then be transformed into SQL once the user tries to access the result.
Sure, it is some slight overhead to transform a 2nd query language, but the NPath parser is extremely fast, and NPersist use NPath internally so we just wanted to avoid making two SQL generators.

The SQL generation is by far the biggest beast inside NPersist, it’s big, furry and very angry, so we just want to stay away from it if we can. ;-)

We have also managed to solve the problem with Linq and Load spans that have haunted us since we first saw Linq.
“How the heck do we add load spans to the select when we cannot change the syntax of the select part?”
The solution is quite simple: “You don’t”
Instead, we pass the loadspan to the query source, like this:

var res = from cust in ctx.Repository(new LoadSpan<Customer> 

          select cust; 

This way we do not need to invent weird hacks that don’t really fit into the Linq syntax.
The solution might have been obvious for others, but we were really stuck on it, pretty much because we have always specified load spans in the select clause of NPath.

MyLisp Editor

I’ve started to make a very simple editor for MyLisp, just so I can get some highlighting and testrun my snippets.

Here is the exclusive world first screenshot ;-)


Well, thats about it, source and binaries will be out once it’s more complete.
(The SyntaxHighlight editor component is available for download at )