Category name:Design

Injected objects should not be disposed be the class that receives the instance

I just read Windsor: Component Burden described as a (bad) side effect but this is not how it should work.

Disposable objects should be cleanup by its creator. This is the easiest way to define the disposing responsibility!

In the mentioned article they have a situation where  A injects X into B at construction and then worry that B doesn’t dispose X. Well the reason for that is that B shouldn’t. A should dispose X when it knows for sure that X will not be used anymore. Either at the end of the application of when it knows that B is already garbage collected.

Lets start with an example by first defining X:

interface IX{
//...
}

class X : IX, IDisposable{
//...
}

Example where A is responsible for disposing X:

class A{
void Test() {
using(IDisposable x = new X()){
object b = new B(x);
//... doing cool stuff with b
}
}
}

class B{
IX _x;

public B(IX x) {
_x = x;
}

//... The cool methods of B
}

Example where B is responsible for disposing X:

abstract class A : IFactory{
void Create<T>();

void Test() {
object b = new B(this);
//... doing cool stuff with b
}
}

class B : IDisposable{
IX _x;

public C(IFactory factory) {
_x = factory.Create<IX>();
}

public Dispose() {
IDisposable d = _x as IDisposable;
if(d==null) d.Dispose();
}
}

In the example above you clearly see who’s responsible for disposing X. It’s creator cleansup and this is how it should be done everywhere in my humble opinion. If implementation is doing it in a different way then refactor your code asap to make it more readable and more maintainable.

Ofcourse these simple examples are not like the component architecture of windsor but the solution they describe there smells. If class A injects X into B at construction and B implements IDisposable then class B isn’t responsible for disposing X. A should do the cleanup so must know when X can be safely disposed. This implies that A should know something about B’s lifetime.

  • Either dispose X when the application domain shutsdown; or
  • Knows when A is garbage colllected by checking for example its weak reference; or
  • Subclassing B at runtime to implement a callback or demanding B to implement a custom interface so A can subscribe to a dispose event.

Why read uncommitted data?

I just read Dennis post about his adventures in isolation level land. He says he does not know a good reason to read uncommitted data because of dirty reads.

Well he should refrase this like: Using the isolation level read uncommitted data could result in a dirty read.

Reading uncommitted data can be very interesting. Not only because you can read data that “is not modified at all but not accessible because of a lock”. For example a eventlog table, a table that contains statistics about page requests.

A side benefit is that a select normally also creates a lock on a table for itself. That is because a single query adheres the ACID rules. This means that a select will lock data while it is running. Sometimes you want non-blocking reads as for example Sahil Malik writes. Lets say you have a log table in your database. You know you only do inserts. Each insert will do locking. Lets say you have some SELECT queries that will result in a table scan. You really don’t want to have a table-lock while it is running because else the application will not be able to add new rows to the table. You know in advance that you will never read dirty data because you only do inserts. And that reminds me of a post of my own Change mssql isolation levels to read uncommitted data from some months ago

So when to use this? Well if locking a table will stall other operations and reading uncommitted data isn’t that interesting. You use this sort of queries mostly for reporting functionality or even just normal read operations. As in reading! Not read data and then update the data. You will need optimistic concurrency control for that and that requires a timestamp column to validate or a total record compare. With a timestamp.. you MUST be sure that the record data read in the first place is not dirty.You cant use the read uncommitted here. But the nice thing about the total record compare is that you can read uncommitted data because the OCC solution relies on the data and not on a timestamp. So it IS possible but in most environments you see timestamps because comparing one timestamp column is cheap.

Browsrcamp helps testing your design with Safari

Today I was reading the website statistics with Google Analytics. It shows that about 2,5% of our visits come from Safari. I only tested our design with Internet Explorer and Firefox and it looks great on those browsers.


I remembered a website which could do a page request, render the results in Safari and present it to you as a jpeg. I googled for test safari and the first hit that showed up was Browsrcamp. Really an excellent developer tool if you don’t have access to a mac with osx.

Website design not my favorite hobby

Although I am an ict nerd and resulting affection for bitfucking and neverending refactoring I also do some sports. I play volleyball and the current website of my volleybal club yum lacks some general design that works correct on both firefox and internet explorer. I’m a member of this club for some years now and I didn’t like the design from the start. So while a friend said he was working in his spare time with a redesign of the website internals I proposed to help out. I don’t know why I said that because now I’m screwed 🙂


My main objectives for the site are:



  • No tables for the websites design.
  • Crossbrowser.
  • Not too much graphics.
  • No frames.
  • XHTML proof
  • CSS validation proof

So I made a design by hand with my *excellent* css and crossbrowser knowledge (%$*@ crossbrowser makes me insane!) and within a few hours I had a design I could live with as a first draft for reviewing. I ofcourse lied a bit about my css knowledge as I”m a ‘back-end boy’. I like to play with photoshop but really lack the skills of creating a good gui or website design. The current version looks quite ok if you don’t mind that I judge my own work. Below some WIP screenshots of the design.


Version 1 Version 2 Version 3 Version 4


Below the proof that my website is non CSS friendly.


Version 4

  • Recent Posts
  • Recent Comments
  • Archives
  • Categories
  • Meta