Saturday, December 26, 2009

Challenge yourself

I have been participating in some contests in december by submitting some code to microsoft. The following is two of my contributions that weren't picked as the winner.

The assignment for my first contribution was to make an extension method for one of the new types in .net 4.0. What I made was an extension method that would return a text presentation of a BigInteger in whatever base you want (16 being the maximum as I don't know any standard for higher bases).

The second assignment was to implement group functionality for collections that would take several properties to group by. I chose to make an extension method that would take strings as input and group the collection by the values of the properties identifier by the strings. The result is a collection that contains a collection for each group. This was my first time for using Linq together with reflection.

Friday, December 18, 2009

The design flaw of foreach

The foreach loop contruct in C# is very useful, but it has one major design flaw. The design flaw I'm thinking of is the NullPointerException it throws when it's given a collection that is null.
I do realize that it makes sense to throw an exception when no input is provided. The problem is that I have never seen an application that expects an exception from the foreach contruct.
With foreach you must know that the collection is set, or wrap the whole thing in a check for null. This adds an extra codeblock to your code.

I have created an extension method for collections so I can avoid the extra codeblock and do something like this:

IList<SomeObject> list = GetObjects();
foreach(SomeObject o in list.NullChecked()){

Even if the GetObjects() method returns null your code wont fail. It just doesn't execute the loop.

public static IEnumerable<T> NullChecked<T>(this IEnumerable<T> input){
        return input;
        return new List<T>();

Tuesday, December 08, 2009

The Gartner Report

The other day I read an article that quoted a Gartner Report that said something like: "In the future there will be a demand for fast computer drives". Maybe its just me... but aren't statements like that a bit vague. Its the kind of visions you might read in the dilbert comic. They are so obvious that if they don't come true its because the world has collapsed in the meantime. Anyone can state the obvious like that.

Here's my vision for the future:
In the future things will be different.
If you want this elaborated you have to buy my report which I will have to make up. :)

Monday, December 07, 2009

Easy loops

Loops are common in a lot of code. I made a few contructs in plain to make it even easier to do simple iterations in .net.
//do something 3 times

//count an integer from 3 to 8 (both included) and do some action for each value.
3.To(8).Do(i=>Console.WriteLine("The number is: " + i ))

I know this is sort of thing is common in languages like Ruby or F# but that's not gonna stop me from doing it in C# :)

The implementation can be found at

Saturday, November 14, 2009

Wrapping objects with interfaces

Have you ever had to map data to or from 3rd party components? Have you ever had to use reflection to access properties or methods you know the signature for on object you dont' have type for at compile time? This has happened a few times for me, especially when I have to deal with webservices defined by autogenerated classes.

My solution to this is to create an interface that resembles the objects I need to handle. I have made a small helper that will make proxy object for you that implements the provided interface and forwards all property calls and method calls to the object you need access to. This is how you can use it.

IMyInterface wrapped = anyObject.WrapAs<IMyInterface>();

Now you are able to create an interaction that can operate on your object. This might also be a way integrate with components you don't want a direct dependency on.

Tuesday, November 10, 2009

Dynamic duck typing in .Net 3.5

I just saw a post about wrapping reflection as extension methods.

Reflection as such is not rocket science. Reflection is for discovering features of objects you don't know how is structured at compile time or it is for interaction with objects you know how operate but don't have a type for.
It's not a big problem to use reflection to access regular properties or methods. The extension methodsI created for wrapping properties and non generic methods are merely wrapping some simple reflection calls.

Alright, let's get to it:

Setting the value of a property.
o.SetValue<string>("Title","Some title");

Getting the value of a property.
string title = o.GetValue<string>("Title");

Calling a method

With generic methods its a different story. I can't find it just by a name. I have to find the version that supports generics. Not only do I need to provide parameters for the method call but also the generic types. This means I can't wrap a generic method call in just one extension method.

This is the situation I want to handle. I have an object and want to find a Dao object for it to be able to save it. If you know the type compile it could be something like this:

IDao dao = factory.GetDao<Blog>();

The same code executed with my new extension methods:

object o;// object you want to save
object dao = factory.GetMethod<object>("GetDao",o.GetType()).Call();

It's a bit more verbose than direct code, but a lot more compact and precise than if you had to use the System.Reflection namespace. The GetMethod method gathers the name of the method together with a list of types that defines the methods generic properties. The Call method accepts the parameters for the method call and invokes the method.

The source for the extension methods can be found here.

Friday, November 06, 2009

Extensions methods FTW

Extension methods is my favorite language feature of .Net. Although they are just symantic sugar they provide a nice way of compacting code to express its real intent.

Here is a collection of my favourite homemade extension methods. Most have been included in the plain framework.

#1: My extension method for making !string.IsNullOrEmpty(somestring) more readable.

#2: Bringing the IN operator from sql to .net. It's implemented on enums but could be any type.

#3: Execute an operation on a collection. I use this all the time and I also have a version that can return the result of an operation as a new collection. That is basically equal to what the .Select extension from System.Linq does. The functional approach of F# inspired me to this.

#4: Get a typesafe value from a web request

#5: Gets all the controls of a certain type from a form. Originally I also had a version that operated on any type of object. I soon found out that searching an object structure without any constraints is a performance disaster. Being limited to finding controls on a form is much better as it's a well defined hierarchy that easy and fast to traverse. The idea springs from looking at JQuery. Some examples of usage can be found in my blogspot about oneliners in .net

#6 Save an object. This operates on types not defined yet. If you implement the IBusinessEntity<> interface you will automatically get a Save() method. It gives a feeling and ease of use as that of the Active Record pattern, without the object knowing anything about persistence. The plain framework also contains extension methods for Delete() and Validate().

What would make this perfect is the ability to define extension methods to be used in a static context. If my business class was Product, I would then be able have a Product.Get(...) method. That would be cool.

Thursday, November 05, 2009

Flank your problems

The other day I took my youngest daughter to football practice. She is 5 years old and has a lot of energy to spare. Its great fun to watch these small kids play around on the field.

When I saw them playing I noticed a pattern I have seen in software development. All the kids were chasing the ball at once. This made it a somewhat confusing match to watch. The problem was not that the girls lacked the technical skills to play football or that they didn't understand what the goal of the game was. The problem was that they were so entutiastic about getting the ball to the other end of the field that they all rushed to the ball to help the team get the ball to the other end.

I think this anti pattern should be called something like "Beeing too specialized" or "Follow the leader". The pattern to overcome this would be the "Think before you act" or "Flank your problems".

My point is that you shouldn't rush for a quick solution as there might be a better and faster solution if you're open minded.

Thursday, September 10, 2009

One liners in .Net

Just wanted to show a few one liners from a project I'm working on. I get a lot of data from a third party as XML. In my business logic I want to work with objects so I have made a class that wraps on entity in the XML.

var orgs = doc.SelectNodes("//Organisation").Cast<xmlnode>().Do<xmlnode,wrappedorganisation>(x => new WrappedOrganisation(x));

So in this line all the organisations in the xml are located and a list of WrappedOrganisation is created from it. With the next one we had to hide a lot of textboxes on a page if a related hidden field had the value "0".

Page.findControl<placeholder>().Do(x => x.Visible = x.findControl<hiddenfield>().Count(h => h.Value != "0") != 0);

The next one retrieves a list of custom objects from some textboxes on a UI.

IList<customvalues> list = UserControl.findControl<textbox>().Do(x => timeSpecs.Add(new CustomValues() { Amount = int.Parse(x.Text), Month = StartDate.Value.AddMonths(i++) }));

Finally a line that identifies TextBoxes on a UI with a value that is not an integer and marking them.

UserControl.findControl<textbox>().Where(x => !int.TryParse(x.Text, out i)).Do(x => x.CssClass = "borderRed").Do(x => messages.Add("Not a valid amount"));

Friday, August 28, 2009

Labour is not just labour

I am doing some consulting for an international company. Seeing how departments in different countries of the company tries to solve the same problem has been a great experience.

I work and live in Denmark, and with high salaries you would think that here it would be very popular to outsource IT tasks to low budget countries. Well it isn't. It's not that companies haven't realized that labour is cheaper elsewhere, it just doesn't fit the way we work.

For one thing there is the language barrier. Although most people in Denmark speak english, this is not the language of choice for defining business rules. If business rules were to be implemented in another country all the business rules would have to be translated, loosing a few in the process. Thats quite an overhead to manage.

Another thing is that we are used to having people as the most expensive resource in anything we do. This means that we are used to working smart. Saving time is one of the most valuable features of your work. We are used to having very flat organisation structures with 'Agile' processes and rapid feedback. Offshoring just doesn't fit into this way of working. It would require much more strict project organisations.

The kind of work we could outsource would be the repetitive work but I think we are used to ulitize the computer for repetitive work which is what it is good at.

Some places converting data would be a manual task, but that is not common in Denmark. As labour is expensive, it's much cheaper to let a computer do the conversion.

Although I see the potential of offshoring IT development for the right assigments, I'm not seeing it being much of a success anytime soon in Denmark.

Monday, August 17, 2009

Going to Jaoo

I'm going to the jaoo conference this year

If its going to be anything like the last two times I've been there my head will be exploding with new ideas when I get back.

Saturday, August 15, 2009

Defining knowledge

Years ago I had a discussion with some coworkers about how the company we were working in could improve it's technical capacity. This discussion made me aware of how differently people look at this. Most of all it is the difference in what people think is important, beeing a knowledge driven company. The sales people don't really care other than they want us to be able to deliver what our customers demand. They way they want us to evolve is in measurable terms that can directly be used in sales talks with our customers. This could be certifications of our employees, adopting new platforms etc.

I don't think knowledge is something you can measure as such. You can take a lot of courses but if you are not able to put it in context you cannot use it for much. To me knowledge comes from exact facts combined with experience. In our line of business we sell our creativity which is based on our knowledge... Measuring creativity is a bit hard.

Thursday, August 13, 2009

Learning to read

My daugther is learning to read. At the moment she has learned all the letters and can slowly spell her way through easy word. If words wore just spelled the way they are pronounced she would be an excellent reader already.

These days there is a lot of difference between the written language and the spoken language of danish. This has to do with the fact that the written language was defined a long time ago and the spoken language has changed a lot since.

This is just too inefficient for me. There are more exceptions to the rules than cases that follow the rules. Is this was some business process I had to analyse I would recommend refactoring the whole thing. Today, writing danish is not as much about knowing the sounds of the letters as it is to remember the exact spelling of each word.

Norwegian is similar to danish but in Norway they have refactored the whole spelling thing.I don't have any exact figures but I guess a lot could be saved in education by making things a bit less complex.

Wednesday, August 12, 2009

Is EF an ORM

I just read the "vote of no confidence" agains the Entity Framework (EF).

I have previously discarded the framework for the fact that it didn't support POCO. They have tried to recover by implementing IPOCO but its a workaround that doesn't really fit the issue of not supporting POCO. Not supporting POCO means that Entity Framework introduces dependencies to your domain model of Entity Framework that are not necessary and forces you to use your domain model in a certain way.

So why is this bad? A few extra lines of code in an entire application isn't too bad. It really isn't, but the purpose of the ORM is to create a persistent ignorant model. If we take plain as an example plain would have to be changed to support EF. Suddenly the nice layering would be broken because persistance logic would have to be applied directly in the domain logic or the UI layer.

As far as I understand version 4.0 of Entity Framework supports persistent ignorance better.

Tuesday, August 11, 2009

Software Factories are Evil

I started this post a loooong time ago.

A resent pod cast on .net Rocks and an article in reminded me of what I wanted to write.

I think code generation is a sign of bad design. Code generation is a sign of a need for repetitive code. Repeating code is the certain path to errors.

The worst kind is the one where you are supposed to edit some of the resulting code. The purpose of the process is to let you specify something in an abstract manner and have the details of the system taken care of by the generator. Once you edit the code you brake the abstraction and you will no longer be able to make changes to your application using the high abstraction. Examples of this could be the Windows Forms editor in visual studio. When you drag components on to your form, a lot of code is generated behind the scene. If you have to change anything in the generated code you might loose it later if some components are moved around on the design surface.

Another way of generating code is to generate static code that doesn't need to be changed by the user. This can be done to create a structure that the user can utilize in his/her program. I'm suggesting that you avoid this as well if possible. The bad impact on your program is not as big as on the previous method though. The reason for applying this could be to work as a shortcut to make something compile together.
An example of this could be the designer file for aspx files in visual studio. When visual studio parses an aspx file an C# file is created so it can be compiled as part of a C# project. The problem with this file is that it adds no new information to the project, but adds complexity in terms of extra files. Once in a while this file will be out of sync and you have to restart visual studio.

Initializing an object migth be so compex that you need a sniplet to assist you every time. If this is the case you should create a simpler way to initialize your object rather than create a sniplet for it.

My recomendation is therefore that if you are in need of code generation in your project you should reconsider your application design.

Thursday, August 06, 2009

World domination: Google vs. Microsoft

Both Microsoft and Google are large wordwide companies. I'm very dependant on both companies everyday.

The two companies have different market strategies. As for google product I use them because they are cheap and good. With MS its much more that there are no alternatives. This makes me reluctant every time I get a new product to use by MS. Its not that the products are not great. Its the question of what other products I will be forced to use. I think that if MS products were not so sticky and so dependant on other MS products I would probably still be using them because some of them are quite good. I would do so even more happily than today. I would be able to use other vendors for just those part where MS products didn't fit.

Wednesday, August 05, 2009

Documents versus contextual information

Well.. Let me start by asking for your needs. If you say that its for legal purposes and is to be used for documentation together with a digital signature or strictly for printing I would tend to suggest the document approach. I any other case I would recommend storing you information in a contextual manner.

A document is basically a black box where you can dump your data. You can find your data by a folderpath and a filename. Thats basically what metadata you get for your document. For me I might as well put the document straight in the trashcan because I know I won't be able to find the document when I need it in a month when there are several versions of the document and a lot of documents have been written since.

The contextual information is different. I'm in particullar thinking of wikis. Here information is normalised to its 3rd degree, not being repeated. When you need the information somewhere you just make a reference to it.

It's an uneven match. Is your information to be used or are they just to be archived.

Monday, August 03, 2009

DataSource, To bind it all.

A small step for mankind, but a giant leap Plain.

We have found a way to bind a lot of the stuff contained in Plain together in an easy manner. When designing Plain we focused on few dependencies, high extendability, flexibility and ease of use. The ease of use is the one that can be hard to align with the other.

Plain is quite easy to use but very flexible. Code was however still needed to hook things together. Its no longer so. By using a Plain DataSource for ASP.Net, you are no longer needed to write any code to hook things together. Just place a DataSource on your page and define what domain class it should operate on and point it to the query ( defined in your business layer). The beauty of using a datasource is that a lot of standard .net usercontrols know how to use it to retreive and send data. DataSource objects also contain information about paging and sorting, and your UserControls will be able to take advantage of that just by enabling them to do so.

Wednesday, July 08, 2009

The good and bad of recent .Net

The Good:
  • Extension methods
    I must admit that I can't get enough of extension methods. They make it much easier to create powerfull structures that are very easy to use, without compromising your good architecture.
  • Lambda expressions
    These are not much of a revolution, but I like having a more compact way of writing code.
  • WCF
    WCF scores high on the simplicity and extendability. I'm not sure I would have designed it the same way, but it does the job. It's coolest feature is that you can start out simple and add complexity a long the way.
  • F#
    F# brings functional programming to .net. The functional paragime brought to .net is very exciting and it will hopefully inspire a lot of people out there.

The Bad:
  • Silverlight
    This framework has been hyped a lot. Although it has a lot of cool features, its lacking the most important one: Interoperability. If I choose silverlight I'm still forcing all my user to windows.
  • WorkFlow Foundation
    I had high hopes for this framework. I have previously worked on workflow systems and was looking forward to having a framework that would ease the creation of workflow systems. I think the problem is that workflow foundation wants to do too much. Anything is possible and that makes it closer to a generic than to a dsl for workflows. I would have preferred a more specialized framework.
  • Entity Framework
    The good thing is that more .net developers have now heard of ORM. The bad thing is that the ideas with EF are quite bad and I would choose another ORM anyday. Too bad this means that more and more will use EF and at some point we are probably all going to be forced into using it.
  • MSTest
    This product doesn't really make a difference. It doesn't provide anything that was not out there already. My preference is still NUnit.
  • TFS
    If you haven't tried this, then don't mind. It is way too ambitious and too complex to use.
  • Monodevelop
    Mono has come a long way. The big problem is and will always be that Mono will always be behind ms .net. Mono has serious problems in being compliant with .net because .net is so closed.

Tuesday, June 09, 2009

Sales people explained

It has always been hard for me to communicate with sales people. I think this is a problem most technical people experience. It doesn't matter whether they are selling me things, or selling my work. They just live in another absurd and parallel universe.

It is common knowledge that communicating across continents can be a challenge because of cultural differences. It's the same when communicating with sales people. These are the main differences to "normal" people as I see it.
  • A more liberal approach to the truth or maybe just a more ignorant attitude towards facts. They are often only interested in the good story. I you want to hear about the perfect world or solution, do contact a sales person.
  • A low signal to noise ratio.
    The goal for communication for a sales person is the communication itself. Whether any useful information is communicated is often not important. The entropy of a conversation can therefore be very low. Often it's just not worth the communication.
  • Have no interest in, and no knowledge of what they speak of. The best feature of a sales person is his ability to small talk. Most of the time he won't have the slightest idea about what he is selling.
Don't get me wrong. I generally don't think salespeople are liars or bad people. I just don't think they always comprehend the stuff they are working with. Therefore it is harder for them to separate right from wrong. Therefore they are hard (or impossible) to communicate with.

Friday, May 22, 2009

Bulk calling of methods

You have your domain model defined in a class called DomainModel. You have a lot of usercontrols that handle seperate parts of the domain model. All the user controls implement the following interface:

You want to call the PopulateControl method on all your usercontrols to get data mapped to the UI. This is how you would do with plain.

Sunday, May 17, 2009

When did things stop being natural

I might be a bit off on this post.

I'm just a bit confused with a word. To begin with everything was natural. When you apply a natural process to a natural substance, it will remain natural. So when and how did some things become unnatural?

Thursday, May 07, 2009

Creating a bug report

I have seen some bad bug reports. This is my little guide how to write a useful bug report.
  • Get to the point. It’s about a bug, and that’s what you should be writing about. It is of no in-terest why this bug is so important to you.
  • Explain the context. Al though you might think the context is obvious, explain it anyway. Explain how you can reproduce it.
  • Be polite. The people that are to fix the bug are probably very enthusiastic about fixing your problem. By calling them names they will be less so. They don't necessarily have anything to do with the bug in the first place.
  • Give it a priority.

Wednesday, May 06, 2009

Validations in Plain

We thought we were very original when we thought of the validations for plain. It turns out that there is a NHibernate Validation project that uses the same idea.

Although we were not aware of the NHIbernate Validation (and I guess the other way around) the way the validations are defined are very similar. The biggest difference is that Plain validation is focused on the domain model and the UI. NHibernate is focused on the domain model and the data layer. Plain Validation has (or it is supposed to have) translations for displaying validation errors directly in the UI.

As the two validation frameworks have very similar capabilities, we might make a mapping for you.

Programs I'll lay out for a fresh install

I've seen a few bloggers listing the programs they want on their machines.

I have thought of this as well, and the conclusion is that the only program I need to be bothered installing is apt-get. When I have apt-get installed and it is pointed to the repositories I want, any other program is an ease to install and won't take long. I can wait untill I need that program to install it.

This of course only goes when I'm on Linux. There is no apt-get on Windows, and installing a fresh machine will take a looong time. Keeping track of licenses makes the process even longer. I won't go into that. :)

Monday, May 04, 2009

Being pragmatic

Being pragmatic means that you consider what works over what is correct or pretty.

I'm a big fan of being pragmatic. A big mistake I see is that people think that if you are pragmatic you should not be concerned with doing things smart. This is wrong. I believe pragmatic is about spending the least amount of time creating a thing that fulfills a need.
If the need is to be able to maintain it, the pragmatic solution won't be to hardcode everything.

The pragmativc solution is not:
  • Ignore common sense
  • Start coding before you think
  • Avoid design
The pragmatic solution for me is almost always to stop and think about what makes sense. This is the way I solve my problems in the most optimized way. I hope you will too!

Saturday, April 25, 2009

Configuring Sharpdevelop for web projects

I want a small .net IDE installation that is fast. I have for a long time been happy with Sharpdevelop but one big issue is that there is no default webserver provided that you can use to develop web apps on.

Luckily my good friend Kristian Gundry has fixed this for me.

By providing the right parameters you can start a webserver. Either you can start the mono xsp2 webserver or you can use the one that comes with Visual Studio Express.

Happy happy joy joy! :)

Thursday, April 23, 2009


Consider the following:
I provide you with a .net 3.5 dll that implements the class SomeClass plus some operations that operate on this class. The Isvalid method has no side effects, will throw no exception and always return true.

You run the following code:
What is output to the screen?
  1. "o is valid"
  2. "o is not valid"
  3. "Exception has been thrown"
  4. impossible to predict
Place your bets please

Sunday, April 12, 2009

Note to self

When building an lightspeed vesel don't make the brake a button on the dash board. At light speed you will only be able to move backwards in the vesel and you will never reach a button in front of you.

Friday, April 03, 2009

Misunderstanding: The path to new ideas

Isn't it frustrating that people misunderstand each other all the time?

At one point I though that all the problems people have communicating were holding us back. Even though a person has a brilliant idea, he might be the only one who truly understands it. A lot is lost in translation. Wouldn't the world just be magnificent if you just had to tell people things once and they would understand. There is just one little problem with this illusion. If we all just understood each other there would be little reason to interpret what you hear and come up with new ideas. Perception has a big role here.

So, the next time you are annoyed with somebody because they just don't get what you are saying to them or by people that can't explain anything in an easy manner, remember it is for a better cause. EVOLUTION! Even the simplest things can be misunderstood and will likely be. It is the reason we have come so far.

I have many times wished that I instantly could grasp the ideas of some of the great thinkers. Instead I have to read a lot of books and spend a lot of time interpreting what is written. My main source of inspiration comes from information that I don't understand.
My theory is that most ideas spring from existing ones that were misunderstood.

Friday, March 27, 2009

Alt.Net FTW!

I attended an usergroup the other day. There was a lot of dialog of what is and if the movement has already started dying.

It doesn't really matter if there is a movement. is more about concept and a way of thinking than it is about a bunch of people. As long as people develop tools and components to use with .net and don't just wait for the next release from Microsoft, the spirit of will live.

So if you are a .net developer and your primary source of ideas comes from Microsoft then you don't have to care about what is. If you on the other hand keep looking out for better ways to do stuff, an meeting might be for you.

Saturday, March 14, 2009

Working on collections and searching for objects

I am reading a book about F# which is a functional language for .net. At the same time I have been introduced to JQuery which is a javascript framework for querying and manipulating objects on a html page. It has sprung some ideas for the plain framework.

What I'm striving for is the possibility to query an object structure for objects. When the objects are returned, it should be possible to perform actions on all these objects in one statement. LINQ might be usefull for querying but at the moment I'm guessing that it won't fulfill my needs.

Lets me give an example of what is currently in progress. This will find all TextBox objects that contain the value '0' and set the background colour on it:

form.find<textbox>("[.Text='0']").Do(t=>t.BackColor= Color.Azure);

The .find method is an extension method on object, which means you can use it for any object. The .Do method is an extension method to all IEnumerable<> that lets you perform the lambda function on all instances contained in the list.

Another example is by using the Map method.

IList<string> textboxValues = form.find<textbox>().Map<string>(t=>x.Name +": " + x.Text);

This will produce a list of strings with that name and value of all textboxes somewhere on the form. The Map method is currently not an extension method as I haven't figured out how to implement the syntax of just just providing one generic parameter to an extension method working on a generic type :(
Actually there is a Map extension method, but it requires you to provide the generic type of the collection as well. In this case it would be .Map<textbox,string>(...)

The one thing that is missing is the syntax to search for objects. Will it be LINQ or will it be a homegrown dsl?

If you want to use the .Do method here it is:

public delegate void OneParemeterNoReturn(T x);
public static void Do<t>(this IEnumerable<t> o,OneParemeterNoReturn<t> func){
foreach(T t in o){

otherwise you can wait for it to apear in the plain framework :)

Tuesday, February 10, 2009

Creating overloaded methods with extension methods

I have an interface in an application.

I want the interface to be simple to implement so it basically contains one method that has a number of parameters.

I want the interface to be simple to use, so I have implemented a number of extension methods that will work as overloads for the method specified in the interface.

So far so good. My next wish is to make the interface generic. This is where the everything falls apart. Apparently you are not able to make extension methods on a generic type. Every time i come across such mismatch between part of the .Net framework I am very sad. I wish the next version could just be a little coherent. :(