Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 2, 2024
1 parent ee178ad commit 50ab845
Show file tree
Hide file tree
Showing 34 changed files with 679 additions and 263 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ dotnet run
- [Custom attributes](readme/custom-attributes.md)
- [Custom universal attribute](readme/custom-universal-attribute.md)
- [Custom generic argument attribute](readme/custom-generic-argument-attribute.md)
- [Bind attribute](readme/bind-attribute.md)
- [Bind attribute with lifetime and tag](readme/bind-attribute-with-lifetime-and-tag.md)
- [Bind attribute for a generic type](readme/bind-attribute-for-a-generic-type.md)
### Interception
- [Decorator](readme/decorator.md)
- [Interception](readme/interception.md)
Expand Down
4 changes: 2 additions & 2 deletions readme/async-disposable-scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ partial class Composition: IDisposable, IAsyncDisposable
() =>
{
Composition transientComposition2 = this;
Session localValue60 = new Session(transientComposition2);
return localValue60;
Session localValue63 = new Session(transientComposition2);
return localValue63;
});
}
}
Expand Down
10 changes: 5 additions & 5 deletions readme/auto-scoped.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ partial class Composition
Composition transientComposition2 = this;
IService transientIService1;
{
Composition localBaseComposition62 = transientComposition2;
Composition localBaseComposition65 = transientComposition2;
// Creates a session
var localSession63 = new Composition(localBaseComposition62);
transientIService1 = localSession63.SessionRoot;
var localSession66 = new Composition(localBaseComposition65);
transientIService1 = localSession66.SessionRoot;
}

IService localValue61 = transientIService1;
return localValue61;
IService localValue64 = transientIService1;
return localValue64;
});
}
}
Expand Down
119 changes: 119 additions & 0 deletions readme/bind-attribute-for-a-generic-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#### Bind attribute for a generic type

[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Attributes/BindAttributeForGenericTypeScenario.cs)


```c#
interface IDependency<T>
{
public void DoSomething();
}

class Dependency<T> : IDependency<T>
{
public void DoSomething() { }
}

class Facade
{
[Bind(typeof(IDependency<TT>))]
public IDependency<T> GetDependency<T>() => new Dependency<T>();
}

interface IService
{
public void DoSomething();
}

class Service(IDependency<int> dep) : IService
{
public void DoSomething() => dep.DoSomething();
}

DI.Setup(nameof(Composition))
.Bind().As(Lifetime.Singleton).To<Facade>()
.Bind().To<Service>()

// Composition root
.Root<IService>("Root");

var composition = new Composition();
var service = composition.Root;
service.DoSomething();
```

The following partial class will be generated:

```c#
partial class Composition
{
private readonly Composition _root;
private readonly object _lock;

private Facade? _singletonFacade39;

[OrdinalAttribute(20)]
public Composition()
{
_root = this;
_lock = new object();
}

internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
_lock = _root._lock;
}

public IService Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_root._singletonFacade39 == null)
{
lock (_lock)
{
if (_root._singletonFacade39 == null)
{
_root._singletonFacade39 = new Facade();
}
}
}

IDependency<int> transientIDependency1;
{
Facade localValue24 = _root._singletonFacade39!;
transientIDependency1 = localValue24.GetDependency<int>();
}

return new Service(transientIDependency1);
}
}
}
```

Class diagram:

```mermaid
classDiagram
class Composition {
<<partial>>
+IService Root
}
class Facade {
+Facade()
}
Service --|> IService
class Service {
+Service(IDependencyᐸInt32ᐳ dep)
}
class IDependencyᐸInt32ᐳ
class IService {
<<interface>>
}
Composition ..> Service : IService Root
Service *-- IDependencyᐸInt32ᐳ : IDependencyᐸInt32ᐳ
IDependencyᐸInt32ᐳ o-- "Singleton" Facade : Facade
```

122 changes: 122 additions & 0 deletions readme/bind-attribute-with-lifetime-and-tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#### Bind attribute with lifetime and tag

[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Attributes/BindAttributeWithLifetimeAndTagScenario.cs)


```c#
interface IDependency
{
public void DoSomething();
}

class Dependency : IDependency
{
public void DoSomething() { }
}

class Facade
{
[Bind(lifetime: Lifetime.Singleton, tags: ["my tag"])]
public IDependency Dependency { get; }= new Dependency();
}

interface IService
{
public void DoSomething();
}

class Service([Tag("my tag")] IDependency dep) : IService
{
public void DoSomething() => dep.DoSomething();
}

DI.Setup(nameof(Composition))
.Bind().As(Lifetime.Singleton).To<Facade>()
.Bind().To<Service>()

// Composition root
.Root<IService>("Root");

var composition = new Composition();
var service = composition.Root;
service.DoSomething();
```

The following partial class will be generated:

```c#
partial class Composition
{
private readonly Composition _root;
private readonly object _lock;

private IDependency? _singletonIDependency0;
private Facade? _singletonFacade39;

[OrdinalAttribute(20)]
public Composition()
{
_root = this;
_lock = new object();
}

internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
_lock = _root._lock;
}

public IService Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_root._singletonIDependency0 == null)
{
lock (_lock)
{
if (_root._singletonIDependency0 == null)
{
if (_root._singletonFacade39 == null)
{
_root._singletonFacade39 = new Facade();
}

{
Facade localValue26 = _root._singletonFacade39!;
_root._singletonIDependency0 = localValue26.Dependency;
}
}
}
}

return new Service(_root._singletonIDependency0!);
}
}
}
```

Class diagram:

```mermaid
classDiagram
class Composition {
<<partial>>
+IService Root
}
class IDependency
class Facade {
+Facade()
}
Service --|> IService
class Service {
+Service(IDependency dep)
}
class IService {
<<interface>>
}
Composition ..> Service : IService Root
IDependency o-- "Singleton" Facade : Facade
Service o-- "Singleton" IDependency : "my tag" IDependency
```

Loading

0 comments on commit 50ab845

Please sign in to comment.