diff --git a/README.md b/README.md index 7152eef..45ce4c4 100644 --- a/README.md +++ b/README.md @@ -8,30 +8,38 @@ See origin https://stackoverflow.com/a/39276306/2528649 ## Usage example In order to register your services you'll need to add following code to your Startup class: - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddByName() - .Add("key1") - .Add("key2") - .Add("key3") - .Build(); - +```csharp +services.AddTransient(); +services.AddTransient(); +services.AddTransient(); +services + .AddByName() + .Add("key1") + .Add("key2") + .Add("key3") + .Build(); +``` Then you can resolve the appropriate service in two ways. 1) Injecting the dependency from factory registration. - - services.AddScoped(s => new AccountController(s.GetServiceByName("key2"))); - +```csharp +services.AddScoped(s => new AccountController(s.GetServiceByName("key2"))); +``` In this case your client code is clean and has only dependency to `IService` and all the name resolution is performed on the factory expression. 2) Injecting dependency to IServiceByNameFactory interface: - - public AccountController(IServiceByNameFactory factory) { - var name = "key2"; // here you can have custom code of name resolution - _service = factory.GetByName(name); - } +```csharp +public class AccountController +{ + //... + + public AccountController(IServiceByNameFactory factory) + { + var name = "key2"; // here you can have custom code of name resolution + _service = factory.GetByName(name); + } +} +``` In this case your client code depends on both IServiceByNameFactory and IService which can be heplful in case when client has its own logic of name resolution.