1

I would like to do something like the following:

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

}

Is this possible? How would I go about it?

Doug Wilson
  • 4,185
  • 3
  • 30
  • 35

0 Answers0