Skip to content

Commit

Permalink
Ad comments for default bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Sep 19, 2024
1 parent 1622025 commit 4e0ed65
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 6 deletions.
3 changes: 3 additions & 0 deletions readme/async-root.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ partial class Composition
return localValue21;
});
Task<IService> transientTask0;
// Injects an instance factory
Func<IService> localFactory22 = perBlockFunc1;
// Injects a task factory creating and scheduling task objects
TaskFactory<IService> localTaskFactory23 = perBlockTaskFactory2;
// Creates and starts a task using the instance factory
transientTask0 = localTaskFactory23.StartNew(localFactory22);
return transientTask0;
}
Expand Down
6 changes: 6 additions & 0 deletions readme/generic-async-composition-roots-with-constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ partial class Composition
return localValue50;
});
Task<IService<T, bool>> transientTask0;
// Injects an instance factory
Func<IService<T, bool>> localFactory52 = perBlockFunc1;
// Injects a task factory creating and scheduling task objects
TaskFactory<IService<T, bool>> localTaskFactory53 = perBlockTaskFactory2;
// Creates and starts a task using the instance factory
transientTask0 = localTaskFactory53.StartNew(localFactory52);
return transientTask0;
}
Expand All @@ -128,8 +131,11 @@ partial class Composition
return localValue58;
});
Task<IService<T, T1>> transientTask0;
// Injects an instance factory
Func<IService<T, T1>> localFactory59 = perBlockFunc1;
// Injects a task factory creating and scheduling task objects
TaskFactory<IService<T, T1>> localTaskFactory60 = perBlockTaskFactory2;
// Creates and starts a task using the instance factory
transientTask0 = localTaskFactory60.StartNew(localFactory59);
return transientTask0;
}
Expand Down
2 changes: 2 additions & 0 deletions readme/lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ partial class Composition
return localValue33;
});
Lazy<IDependency> transientLazy1;
// Injects an instance factory
Func<IDependency> localFactory34 = perBlockFunc2;
// Creates an instance that supports lazy initialization
transientLazy1 = new Lazy<IDependency>(localFactory34, true);
return new Service(transientLazy1);
}
Expand Down
3 changes: 3 additions & 0 deletions readme/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ partial class Composition
return localValue42;
});
Task<IDependency> transientTask1;
// Injects an instance factory
Func<IDependency> localFactory43 = perBlockFunc2;
// Injects a task factory creating and scheduling task objects
TaskFactory<IDependency> localTaskFactory44 = perBlockTaskFactory3;
// Creates and starts a task using the instance factory
transientTask1 = localTaskFactory44.StartNew(localFactory43);
return new Service(transientTask1);
}
Expand Down
1 change: 1 addition & 0 deletions readme/tracking-async-disposable-instances-in-delegates.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ partial class Composition
accumulator41.Add(transientDependency4);
}
Owned<IDependency> perBlockOwned2;
// Creates the owner of an instance
Owned localOwned8 = accumulator41;
IDependency localValue9 = transientDependency4;
perBlockOwned2 = new Owned<IDependency>(localValue9, localOwned8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ partial class Composition
accumulator42.Add(transientDependency3);
}
Owned<IService> perBlockOwned0;
// Creates the owner of an instance
Owned localOwned10 = accumulator42;
IService localValue11 = new Service(transientDependency3);
perBlockOwned0 = new Owned<IService>(localValue11, localOwned10);
Expand Down
1 change: 1 addition & 0 deletions readme/tracking-disposable-instances-in-delegates.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ partial class Composition
accumulator41.Add(transientDependency4);
}
Owned<IDependency> perBlockOwned2;
// Creates the owner of an instance
Owned localOwned13 = accumulator41;
IDependency localValue14 = transientDependency4;
perBlockOwned2 = new Owned<IDependency>(localValue14, localOwned13);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ partial class Composition
accumulator42.Add(transientDependency3);
}
Owned<IService> perBlockOwned0;
// Creates the owner of an instance
Owned localOwned15 = accumulator42;
IService localValue16 = new Service(transientDependency3);
perBlockOwned0 = new Owned<IService>(localValue16, localOwned15);
Expand Down
1 change: 1 addition & 0 deletions readme/valuetask.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ partial class Composition
{
ValueTask<IDependency> transientValueTask1;
IDependency localValue45 = new Dependency();
// Initializes a new instance of the ValueTask class using the supplied instance
transientValueTask1 = new ValueTask<IDependency>(localValue45);
return new Service(transientValueTask1);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,14 @@ public void Build(BuildContext ctx, in DpFactory factory)
}
else
{
var leadingTrivia = syntaxNode.GetLeadingTrivia().ToFullString().Trim();
if (!string.IsNullOrEmpty(leadingTrivia))
{
code.AppendLine(leadingTrivia);
}

code.Append($"{ctx.BuildTools.GetDeclaration(variable)}{variable.VariableName} = ");
var text = syntaxNode.GetText();
var text = syntaxNode.WithoutTrivia().GetText();
lines.AddRange(text.Lines);
}

Expand Down
33 changes: 29 additions & 4 deletions src/Pure.DI.Core/Features/Default.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private static void Setup()
.Bind<Owned<TT>>()
.As(Lifetime.PerBlock)
.To(ctx => {
// Creates the owner of an instance
ctx.Inject<Owned>(out var owned);
ctx.Inject<TT>(ctx.Tag, out var value);
return new Owned<TT>(value, owned);
Expand All @@ -49,7 +50,9 @@ private static void Setup()
.Bind<global::System.Lazy<TT>>()
.To(ctx =>
{
// Injects an instance factory
ctx.Inject<global::System.Func<TT>>(ctx.Tag, out var factory);
// Creates an instance that supports lazy initialization
return new global::System.Lazy<TT>(factory, true);
})
.Bind<global::System.Threading.CancellationToken>().To(_ => global::System.Threading.CancellationToken.None)
Expand All @@ -68,8 +71,11 @@ private static void Setup()
.Bind<global::System.Threading.Tasks.Task<TT>>()
.To(ctx =>
{
// Injects an instance factory
ctx.Inject(ctx.Tag, out global::System.Func<TT> factory);
// Injects a task factory creating and scheduling task objects
ctx.Inject(out global::System.Threading.Tasks.TaskFactory<TT> taskFactory);
// Creates and starts a task using the instance factory
return taskFactory.StartNew(factory);
})
#endif
Expand All @@ -78,14 +84,17 @@ private static void Setup()
.To(ctx =>
{
ctx.Inject(ctx.Tag, out TT value);
// Initializes a new instance of the ValueTask class using the supplied instance
return new global::System.Threading.Tasks.ValueTask<TT>(value);
})
#endif
#if NETSTANDARD || NET || NETCOREAPP
.Bind<global::System.Lazy<TT, TT1>>()
.To(ctx =>
{
// Injects an instance factory
ctx.Inject<global::System.Func<TT>>(ctx.Tag, out var factory);
// Injects a metadata
ctx.Inject<TT1>(ctx.Tag, out var metadata);
return new global::System.Lazy<TT, TT1>(factory, metadata, true);
})
Expand Down Expand Up @@ -175,13 +184,29 @@ private static void Setup()
#endif
#endif
#if NET6_0_OR_GREATER
.Bind<global::System.Random>().To(_ => global::System.Random.Shared)
.Bind<global::System.Random>().To(_ =>
{
// Provides a thread-safe Random instance that may be used concurrently from any thread
return global::System.Random.Shared;
})
#endif
#if NETCOREAPP2_0 || NET || NETSTANDARD2_0_OR_GREATER
.Bind<global::System.Text.Encoding>().To(_ => global::System.Text.Encoding.Default)
.Bind<global::System.Text.Encoding>().To(_ =>
{
// Gets an encoding for the operating system's current ANSI code page
return global::System.Text.Encoding.Default;
})
#endif
.Bind<global::System.Text.Decoder>().As(Lifetime.PerBlock).To((global::System.Text.Encoding encoding) => encoding.GetDecoder())
.Bind<global::System.Text.Encoder>().As(Lifetime.PerBlock).To((global::System.Text.Encoding encoding) => encoding.GetEncoder())
.Bind<global::System.Text.Decoder>().As(Lifetime.PerBlock).To((global::System.Text.Encoding encoding) =>
{
// Gets a decoder that converts an encoded sequence of bytes into a sequence of characters
return encoding.GetDecoder();
})
.Bind<global::System.Text.Encoder>().As(Lifetime.PerBlock).To((global::System.Text.Encoding encoding) =>
{
// Gets an encoder that converts a sequence of Unicode characters into an encoded sequence of bytes
return encoding.GetEncoder();
})
;
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Pure.DI.IntegrationTests/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ static class Setup
private static void SetupComposition()
{
DI.Setup("Composition")
.Bind<IDependency>().To(ctx => new Dependency())
.Bind<IDependency>().To(ctx =>
// My Comment
new Dependency())
.Bind<IService>().To<Service>()
.Root<IService>("Service");
}
Expand All @@ -62,6 +64,7 @@ public static void Main()
// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(["True"], result);
result.GeneratedCode.Contains("// My Comment").ShouldBeTrue(result);
}

[Fact]
Expand Down

0 comments on commit 4e0ed65

Please sign in to comment.