Thursday 20 August 2009

Lazy Loading of Dependencies in Unity

I have been learning the Unity IoC container recently as we will be making use of it in a project I am working on. Like all IoC containers, it makes it nice and easy to automatically construct an object, fulfilling all its dependencies.

One issue that comes up frequently when using IoC containers, is how to implement lazy loading. For example, suppose my class has a dependency on IEmailSender, but only uses it in certain circumstances. I might not wish for the concrete implementation to be created until I actually know I need it.

public class MyClass(IEmailSender emailSender)

One quick way round this is to take a dependency on the container instead. With Unity, the container comes already registered, so you can simply change the constructor prototype. Now you can call container.Resolve<IEmailSender> at the point you are ready to use it.

public class MyClass(IUnityContainer container)

The disadvantage of this solution is that we have now obscured the real dependencies of MyClass. It could ask for anything it likes from the container, and we have to examine the code to find out what it actually uses. Fortunately, there is a way we can solve this using Unity’s ability to allow you to register open generic types.

Suppose we create a generic class called Lazy, that implements ILazy as follows:

public interface ILazy<T>
{
    T Resolve();
    T Resolve(string namedInstance);
}

public class Lazy<T> : ILazy<T>
{
    IUnityContainer container;

    public Lazy(IUnityContainer container)
    {
        this.container = container;
    }

    public T Resolve()
    {
        return container.Resolve<T>();
    }

    public T Resolve(string namedInstance)
    {
        return container.Resolve<T>(namedInstance);
    }
}

Now we need to tell our container to use Lazy when someone asks for ILazy:

container.RegisterType(typeof(ILazy<>),typeof(Lazy<>));

And now that allows us to change our original class to have the following prototype:

public class MyClass(ILazy<IEmailService> emailServiceFactory)

Now we have advertised that our class depends on an IEmailService, but have not created it immediately on construction of MyClass, nor have we allowed MyClass to get at the IUnityContainer itself.

Saturday 8 August 2009

Live Mesh Wishlist

I have been using Windows Live Mesh for over a year now, and I have to say it has become an invaluable tool for me. However, I do have a few feature requests I would like to see, to improve its usefulness and usability.

1. Recycle Bin

The biggest risk with Live Mesh is that if you accidentally delete a file on one PC, it gets deleted on all PCs. So you must not think of Live Mesh as a viable backup option, even though it does replicate your files across multiple PCs. I would like to see an option for deleted files to go into a recycle bin in the cloud. So long as you have available space in your 5GB, it should be possible to restore files you have previously deleted. Obviously it should be possible to empty that recycle bin if necessary.

2. Syncing status

The client software needs to give an indication of whether it is still in syncing or not. Often I make changes to a file at work just before I go home. I don’t want to turn the PC off until I know that the change has been uploaded to the cloud.

3. Moving folder problem

In theory it should not be possible to inadvertently delete everything from a synced folder, but I managed to do so. I moved the parent folder which contained the Live Mesh folder to another drive, and moments later, the contents of the folder had been deleted on all my synchronized devices. Fortunately I was able to restore the data, but I would like Live Mesh to handle this scenario more gracefully.

4. Ignore list

It should be possible to “ignore” certain files in a synchronized folder. This would be particularly useful for source code. You could do this at the subfolder level or by extension / filename matching.

5. Backup Option

Finally, if Microsoft want to monetise Live Mesh, offering a backup solution with it would be ideal. This should keep not just deleted files, but could also store previous versions of files, allowing you to revert to earlier versions. On top of this, you could add a new type of Live Mesh folder, where you right-click and select that you simply want it to be backed up. Live Mesh would then upload its contents to the cloud, but the synchronization with other devices would not be needed. If it were reasonably priced, and you could control the scheduling of its bandwidth usage, I would definitely be interested.

Thursday 6 August 2009

Using TFS to find what files a user has got checked out

Posting this as a reminder to self of how to do it…

tf.exe status /user:username

Can also add a "/s:" parameter to specify a TFS server, but seems to default to the right one for me.