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

虽然我可以弄清楚如何将接口注册到具体的类,但这个特定的类需要是一个实例(我很确定这是 LifeStyle.Singleton)并且还调用了 graphClient.Connect() 方法。这是我坚持的主要部分。

根据 JeffN825 的回答,我这样做了:

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;
                                                                }));
4

1 回答 1

0

ComponentRegistration<T>.UsingFactoryMethod<T>如果您想自己控制实例创建(这也让您有机会调用 Connect),您可以使用接受委托 (Func) 的方法。

于 2012-03-22T04:59:35.543 回答