Search code examples
castle-windsorautofacneo4jclient

Can someone help me convert this AutoFac registration to Windsor?


containerBuilder
    .Register<IGraphClient>(context =>
    {
        var graphClient = new GraphClient(new Uri("http://localhost:9999/db/data"));
        graphClient.Connect(); // Particularly this line
        return graphClient;
    })
    .SingleInstance();

While I can figure out how to register interfaces to concrete classes, this particular class needs to be a single instance (I'm pretty sure this is LifeStyle.Singleton) and also call the graphClient.Connect() method. That's the main part I'm stuck on.

Based on JeffN825's answer I did this:

container.Register(
                Component.For(
                    typeof (IGraphClient))
                    .ImplementedBy(typeof (GraphClient))
                    .LifeStyle.Singleton.UsingFactoryMethod(() =>
                                                                {
                                                                    var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"));
                                                                    graphClient.Connect();
                                                                    return graphClient;
                                                                }));

Solution

  • You can use the ComponentRegistration<T>.UsingFactoryMethod<T> method which takes a delegate (Func) if you want to control the instance creation yourself (which would also give you the chance to call Connect).