Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim-Maes committed Oct 11, 2024
1 parent f40d778 commit 1fe0b15
Showing 1 changed file with 61 additions and 61 deletions.
122 changes: 61 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,67 @@ public class AnotherKeyedService : IKeyedService
}
```

### Options Registration

Decorate your class containing the options with `[RegisterOptions]` and specify the corresponding section in `appsettings.json`.

```csharp
[RegisterOptions("testOptions")]
public class TestOptions
{
public string Test { get; set; } = "";
}

//appsettings.json:
{
"testOptions": {
"test": "test"
}
}
```

Now you can use this value when injecting `IOptions<TestOptions>` in your service

### Generics

**Define a generic interface:**

Decorate the generic interface with the `[RegisterGenericInterface]` attribute.

```csharp
[RegisterGenericInterface]
public interface IRepository<T> where T : BaseEntity
{
void add(T entity);
}
```

**Create the implementation:**
```csharp
[AddTransient(typeof(IRepository<>))]
public class Repository<T> : IRepository<T> where T : BaseEntity
{
public Repository()
{
}

public void add(T entity)
{
// Implementation here
}
}
```

**How to use**

You can now resolve instances of this type from `IServiceProvider`
```csharp
var customerRepo = serviceProvider.GetService<IRepository<Customer>>();
var productRepo = serviceProvider.GetService<IRepository<Product>>();
```

Both customerRepo and productRepo will be instances of Repository<T> but will operate on Customer and Product types, respectively.

### Decorators

Bindicate allows you to register decorators using attributes. Decorators wrap existing services to add additional behavior while preserving the original service's interface.
Expand Down Expand Up @@ -236,67 +297,6 @@ public class LoggingDecorator : IMyService
}
```

### Options Registration

Decorate your class containing the options with `[RegisterOptions]` and specify the corresponding section in `appsettings.json`.

```csharp
[RegisterOptions("testOptions")]
public class TestOptions
{
public string Test { get; set; } = "";
}

//appsettings.json:
{
"testOptions": {
"test": "test"
}
}
```

Now you can use this value when injecting `IOptions<TestOptions>` in your service

### Generics

**Define a generic interface:**

Decorate the generic interface with the `[RegisterGenericInterface]` attribute.

```csharp
[RegisterGenericInterface]
public interface IRepository<T> where T : BaseEntity
{
void add(T entity);
}
```

**Create the implementation:**
```csharp
[AddTransient(typeof(IRepository<>))]
public class Repository<T> : IRepository<T> where T : BaseEntity
{
public Repository()
{
}

public void add(T entity)
{
// Implementation here
}
}
```

**How to use**

You can now resolve instances of this type from `IServiceProvider`
```csharp
var customerRepo = serviceProvider.GetService<IRepository<Customer>>();
var productRepo = serviceProvider.GetService<IRepository<Product>>();
```

Both customerRepo and productRepo will be instances of Repository<T> but will operate on Customer and Product types, respectively.

## License

This project is licensed under the MIT license.

0 comments on commit 1fe0b15

Please sign in to comment.