Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Dec 13, 2024
1 parent 8498248 commit 81506c0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 40 deletions.
24 changes: 11 additions & 13 deletions readme/func-with-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ interface IService
ImmutableArray<IDependency> Dependencies { get; }
}

class Service : IService
class Service(Func<int, int, IDependency> dependencyFactory): IService
{
public Service(Func<int, int, IDependency> dependencyFactory) =>
Dependencies =
[
..Enumerable
.Range(0, 10)
.Select((_, index) => dependencyFactory(index, 99))
];

public ImmutableArray<IDependency> Dependencies { get; }
public ImmutableArray<IDependency> Dependencies { get; } =
[
dependencyFactory(0, 99),
dependencyFactory(1, 99),
dependencyFactory(2, 99)
];
}

DI.Setup(nameof(Composition))
Expand Down Expand Up @@ -75,9 +72,10 @@ DI.Setup(nameof(Composition))

var composition = new Composition();
var service = composition.Root;
service.Dependencies.Length.ShouldBe(10);
service.Dependencies[3].Id.ShouldBe(3);
service.Dependencies[3].SubId.ShouldBe(99);
service.Dependencies.Length.ShouldBe(3);
service.Dependencies[0].Id.ShouldBe(0);
service.Dependencies[1].Id.ShouldBe(1);
service.Dependencies[2].Id.ShouldBe(2);
```

Using a binding of the form `.Bind<T>().To<T>("some statement")` is a kind of hack that allows you to replace an injection with just its own string.
Expand Down
11 changes: 5 additions & 6 deletions readme/func-with-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ interface IService
ImmutableArray<IDependency> Dependencies { get; }
}

class Service([Tag("my tag")] Func<IDependency> dependencyFactory)
: IService
class Service([Tag("my tag")] Func<IDependency> dependencyFactory): IService
{
public ImmutableArray<IDependency> Dependencies { get; } =
[
..Enumerable
.Range(0, 10)
.Select(_ => dependencyFactory())
dependencyFactory(),
dependencyFactory(),
dependencyFactory()
];
}

Expand All @@ -33,7 +32,7 @@ DI.Setup(nameof(Composition))

var composition = new Composition();
var service = composition.Root;
service.Dependencies.Length.ShouldBe(10);
service.Dependencies.Length.ShouldBe(3);
```

The following partial class will be generated:
Expand Down
2 changes: 1 addition & 1 deletion readme/func.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface IService
ImmutableArray<IDependency> Dependencies { get; }
}

class Service(Func<IDependency> dependencyFactory) : IService
class Service(Func<IDependency> dependencyFactory): IService
{
public ImmutableArray<IDependency> Dependencies =>
[
Expand Down
2 changes: 1 addition & 1 deletion tests/Pure.DI.UsageTests/BaseClassLibrary/FuncScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface IService
ImmutableArray<IDependency> Dependencies { get; }
}

class Service(Func<IDependency> dependencyFactory) : IService
class Service(Func<IDependency> dependencyFactory): IService
{
public ImmutableArray<IDependency> Dependencies =>
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,14 @@ interface IService
ImmutableArray<IDependency> Dependencies { get; }
}

class Service : IService
class Service(Func<int, int, IDependency> dependencyFactory): IService
{
public Service(Func<int, int, IDependency> dependencyFactory) =>
Dependencies =
[
..Enumerable
.Range(0, 10)
.Select((_, index) => dependencyFactory(index, 99))
];

public ImmutableArray<IDependency> Dependencies { get; }
public ImmutableArray<IDependency> Dependencies { get; } =
[
dependencyFactory(0, 99),
dependencyFactory(1, 99),
dependencyFactory(2, 99)
];
}
// }

Expand Down Expand Up @@ -100,9 +97,10 @@ public void Run()

var composition = new Composition();
var service = composition.Root;
service.Dependencies.Length.ShouldBe(10);
service.Dependencies[3].Id.ShouldBe(3);
service.Dependencies[3].SubId.ShouldBe(99);
service.Dependencies.Length.ShouldBe(3);
service.Dependencies[0].Id.ShouldBe(0);
service.Dependencies[1].Id.ShouldBe(1);
service.Dependencies[2].Id.ShouldBe(2);
// }
composition.SaveClassDiagram();
}
Expand Down
11 changes: 5 additions & 6 deletions tests/Pure.DI.UsageTests/BaseClassLibrary/FuncWithTagScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ interface IService
ImmutableArray<IDependency> Dependencies { get; }
}

class Service([Tag("my tag")] Func<IDependency> dependencyFactory)
: IService
class Service([Tag("my tag")] Func<IDependency> dependencyFactory): IService
{
public ImmutableArray<IDependency> Dependencies { get; } =
[
..Enumerable
.Range(0, 10)
.Select(_ => dependencyFactory())
dependencyFactory(),
dependencyFactory(),
dependencyFactory()
];
}
// }
Expand All @@ -51,7 +50,7 @@ public void Run()

var composition = new Composition();
var service = composition.Root;
service.Dependencies.Length.ShouldBe(10);
service.Dependencies.Length.ShouldBe(3);
// }
composition.SaveClassDiagram();
}
Expand Down

0 comments on commit 81506c0

Please sign in to comment.