Skip to content

Commit

Permalink
#74 Add the ability to build up an object - rename Initialize to BuildUp
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 12, 2024
1 parent 9db79ea commit bf282cd
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ dotnet run
- [Class arguments](readme/class-arguments.md)
- [Root arguments](readme/root-arguments.md)
- [Tags](readme/tags.md)
- [Build up of an existing object](readme/build-up-of-an-existing-object.md)
- [Field injection](readme/field-injection.md)
- [Initialization](readme/initialization.md)
- [Method injection](readme/method-injection.md)
- [Property injection](readme/property-injection.md)
- [Default values](readme/default-values.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#### Initialization
#### Build up of an existing object

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

It is possible to create an object yourself and then inject the required dependencies via methods, properties or fields.
In other words, injecting the necessary dependencies via methods, properties, or fields into an existing object.


```c#
Expand Down Expand Up @@ -41,7 +41,7 @@ DI.Setup(nameof(Composition))
.Bind<IDependency>().To(ctx =>
{
var dependency = new Dependency();
ctx.Initialize(dependency);
ctx.BuildUp(dependency);
return dependency;
})
.Bind<IService>().To<Service>()
Expand Down Expand Up @@ -78,10 +78,10 @@ partial class Composition
{
Guid transientGuid2 = Guid.NewGuid();
Dependency transientDependency1;
var localDependency28= new Dependency();
localDependency28.SetId(transientGuid2);
localDependency28.Name = name;
transientDependency1 = localDependency28;
var localDependency27= new Dependency();
localDependency27.SetId(transientGuid2);
localDependency27.Name = name;
transientDependency1 = localDependency27;
return new Service(transientDependency1);
}
}
Expand Down
6 changes: 3 additions & 3 deletions readme/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ partial class Composition
// Some custom logic for creating an instance.
// For example, here's how you can inject
// an instance of a particular type
Dependency localDependency27 = new Dependency(transientDateTimeOffset3);
Dependency localDependency28 = new Dependency(transientDateTimeOffset3);
// And do something about it.
localDependency27.Initialize();
localDependency28.Initialize();
// And at the end return an instance
transientDependency1 = localDependency27;
transientDependency1 = localDependency28;
return new Service(transientDependency1);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Pure.DI.Core/Components/Api.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,7 @@ internal interface IContext
void Inject<T>(object tag, out T value);

/// <summary>
/// Perform injection for fields, properties, and methods for an existing object. Cannot be used outside of the binding setup.
/// Builds up of an existing object. In other words, injects the necessary dependencies via methods, properties, or fields into an existing object. Cannot be used outside of the binding setup.
/// <example>
/// <code>
/// DI.Setup("Composition")
Expand All @@ -2456,7 +2456,7 @@ internal interface IContext
/// {
/// var service = new Service();
/// // Initialize an instance with all necessary dependencies
/// ctx.Initialize(service);
/// ctx.BuildUp(service);
///
///
/// return service;
Expand All @@ -2466,7 +2466,7 @@ internal interface IContext
/// </summary>
/// <param name="value">An existing object for which the injection(s) is to be performed.</param>
/// <typeparam name="T">Object type.</typeparam>
void Initialize<T>(T value);
void BuildUp<T>(T value);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/FactoryRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private ExpressionStatementSyntax CreateAssignmentExpression(SyntaxNode returnBo
{
Name: GenericNameSyntax
{
Identifier.Text: nameof(IContext.Initialize),
Identifier.Text: nameof(IContext.BuildUp),
TypeArgumentList.Arguments: [not null]
},
Expression: IdentifierNameSyntax ctx3
Expand All @@ -181,7 +181,7 @@ private ExpressionStatementSyntax CreateAssignmentExpression(SyntaxNode returnBo
{
Name: IdentifierNameSyntax
{
Identifier.Text: nameof(IContext.Initialize)
Identifier.Text: nameof(IContext.BuildUp)
},
Expression: IdentifierNameSyntax ctx4
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ when invocation.ArgumentList.Arguments.Count is 1 or 2
Resolvers.Add(invocation);
break;

case nameof(IContext.Initialize)
case nameof(IContext.BuildUp)
when invocation.ArgumentList.Arguments.Count is 1:
Initializers.Add(invocation);
break;
Expand All @@ -44,7 +44,7 @@ when invocation.ArgumentList.Arguments.Count is 1 or 2
Resolvers.Add(invocation);
break;

case nameof(IContext.Initialize)
case nameof(IContext.BuildUp)
when invocation.ArgumentList.Arguments.Count is 1:
Initializers.Add(invocation);
break;
Expand Down
6 changes: 3 additions & 3 deletions tests/Pure.DI.IntegrationTests/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ private static void SetupComposition()
.Bind(374).To(_ => "Abc")
.Bind<IDependency<string>>().To(ctx => {
var dep = new Dependency<string>();
ctx.Initialize(dep);
ctx.BuildUp(dep);
return dep;
})
.Bind<IService>().To<Service>()
Expand Down Expand Up @@ -1566,7 +1566,7 @@ private static void SetupComposition()
.Bind().To(_ => 33)
.Bind().To(ctx => {
var dep = new Dependency();
ctx.Initialize(dep);
ctx.BuildUp(dep);
return dep;
})
.Bind<IService>().To<Service>()
Expand Down Expand Up @@ -1639,7 +1639,7 @@ private static void SetupComposition()
.Bind().To(_ => 33)
.Bind().To(ctx => {
var dep = new Dependency();
ctx.Initialize(dep);
ctx.BuildUp(dep);
return dep;
})
.Bind<IService>().To<Service>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
$v=true
$p=9
$d=Initialization
$h=It is possible to create an object yourself and then inject the required dependencies via methods, properties or fields.
$d=Build up of an existing object
$h=In other words, injecting the necessary dependencies via methods, properties, or fields into an existing object.
*/

// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedParameter.Local
// ReSharper disable ArrangeTypeModifiers

namespace Pure.DI.UsageTests.Basics.InitializationScenario;
namespace Pure.DI.UsageTests.Basics.BuildUpScenario;

using Shouldly;
using Xunit;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void Run()
.Bind<IDependency>().To(ctx =>
{
var dependency = new Dependency();
ctx.Initialize(dependency);
ctx.BuildUp(dependency);
return dependency;
})
.Bind<IService>().To<Service>()
Expand Down

0 comments on commit bf282cd

Please sign in to comment.