Skip to content

rmalcomber/DependecyInjectionExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Dependency Injection Example

Here is a simple example of using dependency injection within a DotNetCore console application. This is provided to show how easy it is to set up and run dependency injection.

Runing the code

I have only tested this in Visual Studio 2017/2019 Preview on a windows machine. However this should be applicable for Mac and Linux with DotNetCore.

In Program.cs there are this lines

var writers = provider.GetServices<IWriter>();
foreach (var writer in writers)
{
    TextToWrite.WriteText(writer, authorProvider);
}

It should be faily simple to only add one item to your service collection, for example in the function ConfigureWriters()

private static void ConfigureWriters(IServiceCollection serviceCollection)
{
    serviceCollection.AddTransient<IWriter, ConsoleWriter>();

}

And update the lines as follows

var writer = provider.GetService<IWriter>();

TextToWrite.WriteText(writer, authorProvider);

About

Quick Example of dependency injection

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages