Data-access quicky - A micro mapper

Someone (Yes you @lgrave) started taunting me on twitter. He said that he could do data access in only 50 lines using reflection. This gave me an idea. How small can you go with a mapper?<!--more-->

Here's one, done in only 32 lines of code including some silly comments from me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Mapper<T>
{
private static Dictionary<Type, Delegate> _typeMappings =
new Dictionary<Type, Delegate>();
public Mapper()
{
if(!_typeMappings.ContainsKey(typeof(T)))
InitializeTypeMapping();
}
public IEnumerable<T> Load(IDataReader reader)
{
while(reader.Read())
yield return (T)_typeMappings[typeof (T)].DynamicInvoke(reader);
}
private void InitializeTypeMapping()
{
var recordParameter = Expression.Parameter(typeof(IDataRecord), "record");
// Trust me, you don't want to know ;-)
var lambdaExpression = Expression.Lambda(Expression.MemberInit(Expression.New(typeof(T)),
typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Select(
property => Expression.Bind((MemberInfo) property, Expression.Convert(
Expression.Call(recordParameter, "GetValue", null,
Expression.Call(recordParameter, "GetOrdinal", null,
Expression.Constant(property.Name))),
property.PropertyType)))), recordParameter);
_typeMappings[typeof (T)] = lambdaExpression.Compile();
}
}

Don't invent it your self: 5 great CSS frameworks to rock your website

When you have build a website before you know the drill of building HTML and creating CSS files to style the stuff you created using HTML. For some it's a pain, for others a form of pure art. I think however that it can also be part of a not-invented-here syndrome that developers have grown.<!--more-->

If you're a hybrid developer or a regular developer building a website you're probably looking at this huge mountain of work called styling the website. It however can be made when you have a ready made grid in which you can position the elements that are on the page.

Hello I'm a hybrid

So there you go, I'm a hybrid. It should not surprise you as there are many like me. Developers that have a defect in their brain that causes them to write certain code constructions and have a bit of a design lookout on life. People with a creative brain that are hard to control, most of the time.<!--more--> <h2>No I'm not a designer</h2> Nah, I can't be a designer all my life. Simply because I lack the training to do a proper job. I know about colours and I know about design, but I can't produce unique ideas 24 hours per day. I need to work on code, orthogonal lines, squares and other technical stuff.

I do love orange chairs and hip-hop music from time to time. I also value freedom alot! Some of you may have noticed that while working with me.