question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dependency Injection Support?

See original GitHub issue

Has any thought been given to DI support in Obvs?

It tends to be very hard to go back and bake in support for DI into frameworks later. Is it worth introducing low level support for dependency resolution now while it’s early goings to avoid the pain later? You could either introduce a proprietary IDependencyResolver interface as part of Obvs core or have Obvs take a dependency on the Microsoft.Framework.DependencyInjection package which would come with the added benefit of everyone implementing direct support for that for all the popular containers (e.g. AutoFac, Ninject, Unity, etc.) since it’s a building block of all the new frameworks in .NET vNext. The downside of taking the dependency on that is that it’s still beta and that would mean the version of Obvs that takes that dependency would need to be a beta version itself until that dependency RTMd.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
megakidcommented, Jan 1, 2019

I think @MovGP0’s final post sums up perfectly the accepted approach to Obvs + DI. Closing this.

1reaction
MovGP0commented, Jan 24, 2016

it is actually quite easy. just create a factory method that sets up the service bus using the fluid interface. then you can inject the factory method. Create additional factory methods for alternative configurations and mocks as needed.

Dependency Injection

Factory Method:

public static class MyFactory
{
    public static IServiceBus CreateServiceBus() 
    {
        return ServiceBus.Configure()
            .WithActiveMQEndpoints<ITestMessage>()
            .Named("Obvs.TestService")
            .UsingQueueFor<ICommand>()
            .ConnectToBroker("tcp://localhost:61616")
            .SerializedAsJson()
            .AsClientAndServer()
            .Create();
    }
}

using DryIoc:

var container = new Container();
container.Register<IServiceBus>(made: Made.Of(() => MyFactory.CreateServiceBus()), Reuse.Transient);
container.RegisterDelegate<Action<IServiceBus>>(r => () => r.Resolve<IServiceBus>(), Reuse.Transient);

Mocking

using Moq:

public static class MyMockFactory
{
    public static IServiceBus CreateServiceBus()
    {
        var mock = new Mock<IServiceBus>();
        mock.Setup(...);
        // mock.Verify(...);
        return mock.Object;
    }
}

using DryIoc:

var container = new Container();
container.Register<IServiceBus>(made: Made.Of(() => MyMockFactory.CreateServiceBus()), Reuse.Transient);
container.RegisterDelegate<Action<IServiceBus>>(r => () => r.Resolve<IServiceBus>(), Reuse.Transient);

Usage

Injecting bus:

public sealed class MyClass
{
    private IServiceBus ServiceBus { get; }

    public MyClass(IServiceBus> serviceBus)
    {
        ServiceBus  = serviceBus;
    }

    public void UseServiceBus()
    {
        var serviceBus = ServiceBus;
        // ...
    }

    // TODO: Dispose everything
}

Creating bus when needed:

public sealed class MyClass
{
    private Action<IServiceBus> ServiceBusFactory { get; }

    public MyClass(Action<IServiceBus> serviceBusFactory)
    {
        ServiceBusFactory  = serviceBusFactory;
    }

    public void UseServiceBus()
    {
        var serviceBus = ServiceBusFactory();
        // ...
    }

    // TODO: Dispose everything
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency injection in ASP.NET Core
ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) ...
Read more >
Dependency injection - .NET
.NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between ...
Read more >
Dependency Injection with Code Examples
Dependency injection supports these goals by decoupling the creation of the usage of an object. That enables you to replace dependencies without changing ......
Read more >
Keyed service dependency injection container support
In this post I discuss the new "keyed service" support for the dependency injection container, introduced in .NET 8 preview 7.
Read more >
Dependency injection in Android
Quickly bring your app to life with less code, using a modern declarative approach to UI, and the simplicity of Kotlin. ... Start...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found