Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some colors and formats #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServiceA>();
services.AddTransient<ServiceB>();
services.AddTransient<ServiceC>();
services.AddByName<IService>()
.Add<ServiceA>("key1")
.Add<ServiceB>("key2")
.Add<ServiceC>("key3")
.Build();

```csharp
services.AddTransient<ServiceA>();
services.AddTransient<ServiceB>();
services.AddTransient<ServiceC>();
services
.AddByName<IService>()
.Add<ServiceA>("key1")
.Add<ServiceB>("key2")
.Add<ServiceC>("key3")
.Build();
```
Then you can resolve the appropriate service in two ways.

1) Injecting the dependency from factory registration.

services.AddScoped<AccountController>(s => new AccountController(s.GetServiceByName<IService>("key2")));

```csharp
services.AddScoped<AccountController>(s => new AccountController(s.GetServiceByName<IService>("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<IService> factory) {
var name = "key2"; // here you can have custom code of name resolution
_service = factory.GetByName(name);
}
```csharp
public class AccountController
{
//...

public AccountController(IServiceByNameFactory<IService> 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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also put the interface names in code quotes?