1

次のようなことをしたいと思います。

public class LightInjectTests
{
    [Fact]
    public void CanInjectItemIntoScope()
    {
        var container = new ServiceContainer();
        container.Register<IService, Service>(new PerScopeLifetime());

        var txId = Guid.NewGuid();
        var context = new Context(txId);

        using (var scope = container.BeginScope())
        {
            scope.Inject<IContext>(context);

            var service = scope.GetInstance<IService>();

            service.Context.Should().BeSameAs(context);
        }
    }

    interface IContext { }
    class Context : IContext
    {
        public Context(Guid txId) => TxId = txId;

        public Guid TxId { get; }
    }

    interface IService
    {
        IContext Context { get; }
    }
    class Service : IService
    {
        public Service(IContext context) => Context = context;

        public IContext Context { get; }
    }

}

これは可能ですか?どうすればいいですか?

4

0 に答える 0