Skip to content

Initialize without Dependency Injection

Berin Loritsch edited this page Jan 10, 2019 · 2 revisions

When you have a quick and dirty application you don't need all the added burden of setting up Dependency Injection, and I really wanted DHaven.Faux to handle both scenarios. You can use it with both NetCore apps and standard Framework apps as well. You'll need one of these somewhere in your application:

FauxCollection faux = new FauxCollection(typeof(Program));

Where that is accessible from everywhere, and the typeof(Program) is a class that exists in the parent application. That type is the anchor where Faux will search all the dependencies to find the interfaces and implementations for Faux services you have. If there are no implementations, Faux will create them for you, so no worries. This adds a couple seconds to your startup time, and there really isn't any way around that unfortunately. When you need an instance of your service, you get it like this:

var fortuneService = faux.GetInstance<IFortuneService>();

And you use the interface like expected. You only need one FauxCollection in your application, and it is safe to make it a singleton for your app. You can fine tune the interaction with Discovery and Hystrix using an appsettings.json file next to your application.

Clone this wiki locally