ObjectBuilder dependency injection and ASP.NET Web Services

295 Views Asked by At

I'm using Microsoft.Practices.ObjectBuilder in a web project to handle dependency injection (between view and presenter for example).

I've recently added an ASP.NET Web Service (.asmx) to handle ajax calls. I would like to use dependency injection with ObjectBuilder here as well, but I can't get it working.

I tried to just simply add something like this:

[CreateNew]
public MyClass MyClass
{
    set
    {
        _myClass = value;
    }
}

But this isn't working. The property setter of MyClass is never called. Is it even possible to get this working?

1

There are 1 best solutions below

0
Fendy On

Regarding to old legacy system, especially for those using static - stateless classes, I found this design to be the most fit:

public MyClass MyClass
{
    get
    {
        if(_myClass == null){ _myClass = new DefaultMyClass(); }
        return _myClass;
    }
    set
    {
        _myClass = value;
    }
}

With this, I can still do dependency injection in unit test, using setter injection.

IMyClassUser m = new MyClassUser();
m.MyClass = new MockMyClass();

While, at the very point, does not change the existing code too much. And later when the times come, it can be modified using constructor DI.

It will also help your legacy colleague whom does not understand DI too much, to able to track the code faster (using Go to Definition). And it can survive even without interface/class documentation.