-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98f41b5
commit b292d2c
Showing
35 changed files
with
1,230 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#### Simplified factory | ||
|
||
[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Basics/SimplifiedFactoryScenario.cs) | ||
|
||
This example shows how to create and initialize an instance manually in a simplified form. When you use a lambda function to specify custom instance initialization logic, each parameter of that function represents an injection of a dependency. Since C# 10, you can also specify the `Tag(...)` attribute before the lambda function parameter to specify the tag of the injected dependency. | ||
|
||
|
||
```c# | ||
interface IDependency | ||
{ | ||
DateTimeOffset Time { get; } | ||
|
||
bool IsInitialized { get; } | ||
} | ||
|
||
class Dependency : IDependency | ||
{ | ||
public DateTimeOffset Time { get; private set; } | ||
|
||
public bool IsInitialized { get; private set; } | ||
|
||
public void Initialize(DateTimeOffset time) | ||
{ | ||
Time = time; | ||
IsInitialized = true; | ||
} | ||
} | ||
|
||
interface IService | ||
{ | ||
IDependency Dependency { get; } | ||
} | ||
|
||
class Service(IDependency dependency) : IService | ||
{ | ||
public IDependency Dependency { get; } = dependency; | ||
} | ||
|
||
DI.Setup(nameof(Composition)) | ||
.Bind("now datetime").To(_ => DateTimeOffset.Now) | ||
// Injects Dependency and DateTimeOffset instances | ||
// and performs further initialization logic | ||
// defined in the lambda function | ||
.Bind<IDependency>().To(( | ||
Dependency dependency, | ||
[Tag("now datetime")] DateTimeOffset time) => | ||
{ | ||
dependency.Initialize(time); | ||
return dependency; | ||
}) | ||
.Bind().To<Service>() | ||
|
||
// Composition root | ||
.Root<IService>("MyService"); | ||
|
||
var composition = new Composition(); | ||
var service = composition.MyService; | ||
service.Dependency.IsInitialized.ShouldBeTrue(); | ||
``` | ||
|
||
The following partial class will be generated: | ||
|
||
```c# | ||
partial class Composition | ||
{ | ||
private readonly Composition _root; | ||
|
||
[OrdinalAttribute(20)] | ||
public Composition() | ||
{ | ||
_root = this; | ||
} | ||
|
||
internal Composition(Composition parentScope) | ||
{ | ||
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root; | ||
} | ||
|
||
public IService MyService | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get | ||
{ | ||
DateTimeOffset transientDateTimeOffset3 = DateTimeOffset.Now; | ||
Dependency transientDependency1; | ||
{ | ||
Dependency localDependency25 = new Dependency(); | ||
DateTimeOffset localTime26 = transientDateTimeOffset3; | ||
localDependency25.Initialize(localTime26); | ||
transientDependency1 = localDependency25; | ||
} | ||
|
||
return new Service(transientDependency1); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Class diagram: | ||
|
||
```mermaid | ||
classDiagram | ||
class Composition { | ||
<<partial>> | ||
+IService MyService | ||
} | ||
class Dependency { | ||
+Dependency() | ||
} | ||
class DateTimeOffset | ||
Service --|> IService | ||
class Service { | ||
+Service(IDependency dependency) | ||
} | ||
class IService { | ||
<<interface>> | ||
} | ||
Composition ..> Service : IService MyService | ||
Dependency *-- DateTimeOffset : "now datetime" DateTimeOffset | ||
Service *-- Dependency : IDependency | ||
``` | ||
|
Oops, something went wrong.