Skip to content

Commit

Permalink
#76 Ability to get/know the type of object for which the factory was …
Browse files Browse the repository at this point in the history
…called - improved samples
  • Loading branch information
NikolayPianikov committed Dec 17, 2024
1 parent 285530a commit 40a5802
Show file tree
Hide file tree
Showing 40 changed files with 181 additions and 186 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ dotnet run
- [Required properties or fields](readme/required-properties-or-fields.md)
- [Root binding](readme/root-binding.md)
- [Async Root](readme/async-root.md)
- [Consumer types](readme/consumer-types.md)
### Lifetimes
- [Transient](readme/transient.md)
- [Singleton](readme/singleton.md)
Expand Down Expand Up @@ -301,7 +302,6 @@ dotnet run
- [Exposed roots via root arg](readme/exposed-roots-via-root-arg.md)
- [Exposed generic roots](readme/exposed-generic-roots.md)
- [Exposed generic roots with args](readme/exposed-generic-roots-with-args.md)
- [Serilog](readme/serilog.md)
- [DI tracing via serilog](readme/di-tracing-via-serilog.md)
### Applications
- Console
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 @@ -139,8 +139,8 @@ partial class Composition: IDisposable, IAsyncDisposable
Func<Session> perBlockFunc1 = new Func<Session>([MethodImpl(MethodImplOptions.AggressiveInlining)] () =>
{
Composition transientComposition3 = this;
Session localValue93 = new Session(transientComposition3);
return localValue93;
Session localValue89 = new Session(transientComposition3);
return localValue89;
});
return new Program(perBlockFunc1);
}
Expand Down
20 changes: 10 additions & 10 deletions readme/async-root.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,26 @@ partial class Composition
public Task<IService> GetMyServiceAsync(CancellationToken cancellationToken)
{
TaskFactory<IService> perBlockTaskFactory2;
CancellationToken localCancellationToken43 = cancellationToken;
CancellationToken localCancellationToken37 = cancellationToken;
TaskCreationOptions transientTaskCreationOptions3 = TaskCreationOptions.None;
TaskCreationOptions localTaskCreationOptions44 = transientTaskCreationOptions3;
TaskCreationOptions localTaskCreationOptions38 = transientTaskCreationOptions3;
TaskContinuationOptions transientTaskContinuationOptions4 = TaskContinuationOptions.None;
TaskContinuationOptions localTaskContinuationOptions45 = transientTaskContinuationOptions4;
TaskContinuationOptions localTaskContinuationOptions39 = transientTaskContinuationOptions4;
TaskScheduler transientTaskScheduler5 = TaskScheduler.Default;
TaskScheduler localTaskScheduler46 = transientTaskScheduler5;
perBlockTaskFactory2 = new TaskFactory<IService>(localCancellationToken43, localTaskCreationOptions44, localTaskContinuationOptions45, localTaskScheduler46);
TaskScheduler localTaskScheduler40 = transientTaskScheduler5;
perBlockTaskFactory2 = new TaskFactory<IService>(localCancellationToken37, localTaskCreationOptions38, localTaskContinuationOptions39, localTaskScheduler40);
Func<IService> perBlockFunc1 = new Func<IService>([MethodImpl(MethodImplOptions.AggressiveInlining)] () =>
{
IService localValue47 = new Service(new Dependency());
return localValue47;
IService localValue41 = new Service(new Dependency());
return localValue41;
});
Task<IService> transientTask0;
// Injects an instance factory
Func<IService> localFactory48 = perBlockFunc1;
Func<IService> localFactory42 = perBlockFunc1;
// Injects a task factory creating and scheduling task objects
TaskFactory<IService> localTaskFactory49 = perBlockTaskFactory2;
TaskFactory<IService> localTaskFactory43 = perBlockTaskFactory2;
// Creates and starts a task using the instance factory
transientTask0 = localTaskFactory49.StartNew(localFactory48);
transientTask0 = localTaskFactory43.StartNew(localFactory42);
return transientTask0;
}
}
Expand Down
10 changes: 5 additions & 5 deletions readme/auto-scoped.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ partial class Composition
{
Composition transientComposition3 = this;
IService transientIService2;
Composition localBaseComposition95 = transientComposition3;
Composition localBaseComposition91 = transientComposition3;
// Creates a session
var localSession96= new Composition(localBaseComposition95);
var localSession92= new Composition(localBaseComposition91);
// Provides a root
transientIService2 = localSession96.SessionRoot;
IService localValue94 = transientIService2;
return localValue94;
transientIService2 = localSession92.SessionRoot;
IService localValue90 = transientIService2;
return localValue90;
});
return new Program(perBlockFunc1);
}
Expand Down
4 changes: 2 additions & 2 deletions readme/bind-attribute-for-a-generic-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ partial class Composition
}

IDependency<int> transientIDependency1;
Facade localInstance_1182D12750 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12750.GetDependency<int>();
Facade localInstance_1182D12744 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12744.GetDependency<int>();
return new Service(transientIDependency1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions readme/bind-attribute-with-lifetime-and-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ partial class Composition
_root._singletonFacade43 = new Facade();
}

Facade localInstance_1182D12752 = _root._singletonFacade43!;
_root._singletonIDependency0 = localInstance_1182D12752.Dependency;
Facade localInstance_1182D12746 = _root._singletonFacade43!;
_root._singletonIDependency0 = localInstance_1182D12746.Dependency;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions readme/bind-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ partial class Composition
}

IDependency transientIDependency1;
Facade localInstance_1182D12751 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12751.Dependency;
Facade localInstance_1182D12745 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12745.Dependency;
return new Service(transientIDependency1);
}
}
Expand Down
8 changes: 4 additions & 4 deletions readme/build-up-of-an-existing-generic-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ partial class Composition
{
Guid transientGuid2 = Guid.NewGuid();
Dependency<Guid> transientDependency1;
Dependency<Guid> localDependency56= new Dependency<Guid>();
localDependency56.SetId(transientGuid2);
localDependency56.Name = name;
transientDependency1 = localDependency56;
Dependency<Guid> localDependency52= new Dependency<Guid>();
localDependency52.SetId(transientGuid2);
localDependency52.Name = name;
transientDependency1 = localDependency52;
return new Service<Guid>(transientDependency1);
}
}
Expand Down
8 changes: 4 additions & 4 deletions readme/build-up-of-an-existing-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ partial class Composition
{
Guid transientGuid2 = Guid.NewGuid();
Dependency transientDependency1;
var localDependency53= new Dependency();
localDependency53.SetId(transientGuid2);
localDependency53.Name = name;
transientDependency1 = localDependency53;
var localDependency47= new Dependency();
localDependency47.SetId(transientGuid2);
localDependency47.Name = name;
transientDependency1 = localDependency47;
return new Service(transientDependency1);
}
}
Expand Down
21 changes: 10 additions & 11 deletions readme/serilog.md → readme/consumer-types.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#### Serilog
#### Consumer types

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

`ConsumerTypes` is used to get the list of consumer types of a given dependency. It contains an array of types and guarantees that it will contain at least one element. The use of `ConsumerTypes` is demonstrated on the example of [Serilog library](https://serilog.net/):


```c#
Expand Down Expand Up @@ -41,8 +43,7 @@ partial class Composition
.Bind().To(ctx =>
{
ctx.Inject<Serilog.ILogger>("from arg", out var logger);
var consumers = ctx.ConsumerTypes;
return consumers.Length == 1 ? logger.ForContext(consumers[0]) : logger;
return logger.ForContext(ctx.ConsumerTypes[0]);
})

.Bind().To<Dependency>()
Expand Down Expand Up @@ -83,13 +84,11 @@ partial class Composition
get
{
Serilog.ILogger transientILogger3;
Serilog.ILogger localLogger11 = _argLogger;
var localConsumers12= new Type[1]{typeof(Dependency)};
transientILogger3 = localConsumers12.Length == 1 ? localLogger11.ForContext(localConsumers12[0]) : localLogger11;
Serilog.ILogger localLogger48 = _argLogger;
transientILogger3 = localLogger48.ForContext(new Type[1]{typeof(Dependency)}[0]);
Serilog.ILogger transientILogger1;
Serilog.ILogger localLogger13 = _argLogger;
var localConsumers14= new Type[1]{typeof(Service)};
transientILogger1 = localConsumers14.Length == 1 ? localLogger13.ForContext(localConsumers14[0]) : localLogger13;
Serilog.ILogger localLogger49 = _argLogger;
transientILogger1 = localLogger49.ForContext(new Type[1]{typeof(Service)}[0]);
return new Service(transientILogger1, new Dependency(transientILogger3));
}
}
Expand All @@ -112,7 +111,7 @@ classDiagram
Service *-- Dependency : IDependency
ILogger o-- ILogger : "from arg" Argument "logger"
Dependency *-- ILogger : ILogger
namespace Pure.DI.UsageTests.Advanced.SerilogScenario {
namespace Pure.DI.UsageTests.Basics.ConsumerTypesScenario {
class Composition {
<<partial>>
+IService Root
Expand Down
11 changes: 4 additions & 7 deletions readme/di-tracing-via-serilog.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ partial class Composition
.Bind<Serilog.ILogger>().To(ctx =>
{
ctx.Inject("from arg", out Serilog.ILogger logger);
var consumers = ctx.ConsumerTypes;
return consumers.Length == 1 ? logger.ForContext(consumers[0]) : logger;
return logger.ForContext(ctx.ConsumerTypes[0]);
})

.Bind().To<Dependency>()
Expand Down Expand Up @@ -93,9 +92,8 @@ partial class Composition
get
{
Serilog.ILogger transientILogger0;
Serilog.ILogger localLogger2 = _argLogger;
var localConsumers3= new Type[1]{typeof(Composition)};
transientILogger0 = localConsumers3.Length == 1 ? localLogger2.ForContext(localConsumers3[0]) : localLogger2;
Serilog.ILogger localLogger1 = _argLogger;
transientILogger0 = localLogger1.ForContext(new Type[1]{typeof(Composition)}[0]);
return transientILogger0;
}
}
Expand All @@ -109,8 +107,7 @@ partial class Composition
OnNewInstance<Dependency>(ref transientDependency2, null, Lifetime.Transient);
Serilog.ILogger transientILogger1;
Serilog.ILogger localLogger0 = _argLogger;
var localConsumers1= new Type[1]{typeof(Service)};
transientILogger1 = localConsumers1.Length == 1 ? localLogger0.ForContext(localConsumers1[0]) : localLogger0;
transientILogger1 = localLogger0.ForContext(new Type[1]{typeof(Service)}[0]);
Service transientService0 = new Service(transientILogger1, OnDependencyInjection<IDependency>(transientDependency2, null, Lifetime.Transient));
OnNewInstance<Service>(ref transientService0, null, Lifetime.Transient);
return OnDependencyInjection<IService>(transientService0, null, Lifetime.Transient);
Expand Down
6 changes: 3 additions & 3 deletions readme/exposed-generic-roots-with-args.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ partial class Composition
}

Integration.IMyGenericService<int> transientIMyGenericService1;
int localId5 = id;
Integration.CompositionWithGenericRootsAndArgsInOtherProject localInstance_1182D1276 = _root._singletonCompositionWithGenericRootsAndArgsInOtherProject44!;
transientIMyGenericService1 = localInstance_1182D1276.GetMyService<int>(localId5);
int localId3 = id;
Integration.CompositionWithGenericRootsAndArgsInOtherProject localInstance_1182D1274 = _root._singletonCompositionWithGenericRootsAndArgsInOtherProject44!;
transientIMyGenericService1 = localInstance_1182D1274.GetMyService<int>(localId3);
return new Program(transientIMyGenericService1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions readme/exposed-generic-roots.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ partial class Composition
}

Integration.IMyGenericService<int> transientIMyGenericService1;
Integration.CompositionWithGenericRootsInOtherProject localInstance_1182D1274 = _root._singletonCompositionWithGenericRootsInOtherProject43!;
transientIMyGenericService1 = localInstance_1182D1274.GetMyService<int>();
Integration.CompositionWithGenericRootsInOtherProject localInstance_1182D1272 = _root._singletonCompositionWithGenericRootsInOtherProject43!;
transientIMyGenericService1 = localInstance_1182D1272.GetMyService<int>();
return new Program(transientIMyGenericService1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions readme/exposed-roots-via-arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ partial class Composition
get
{
Integration.IMyService transientIMyService1;
Integration.CompositionInOtherProject localInstance_1182D1278 = _argBaseComposition;
transientIMyService1 = localInstance_1182D1278.MyService;
Integration.CompositionInOtherProject localInstance_1182D1276 = _argBaseComposition;
transientIMyService1 = localInstance_1182D1276.MyService;
return new Program(transientIMyService1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions readme/exposed-roots-via-root-arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ partial class Composition
public Program GetProgram(Integration.CompositionInOtherProject baseComposition)
{
Integration.IMyService transientIMyService1;
Integration.CompositionInOtherProject localInstance_1182D1279 = baseComposition;
transientIMyService1 = localInstance_1182D1279.MyService;
Integration.CompositionInOtherProject localInstance_1182D1277 = baseComposition;
transientIMyService1 = localInstance_1182D1277.MyService;
return new Program(transientIMyService1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions readme/exposed-roots-with-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ partial class Composition
}

Integration.IMyService transientIMyService1;
Integration.CompositionWithTagsInOtherProject localInstance_1182D12710 = _root._singletonCompositionWithTagsInOtherProject43!;
transientIMyService1 = localInstance_1182D12710.MyService;
Integration.CompositionWithTagsInOtherProject localInstance_1182D1278 = _root._singletonCompositionWithTagsInOtherProject43!;
transientIMyService1 = localInstance_1182D1278.MyService;
return new Program(transientIMyService1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions readme/exposed-roots.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ partial class Composition
}

Integration.IMyService transientIMyService1;
Integration.CompositionInOtherProject localInstance_1182D1277 = _root._singletonCompositionInOtherProject43!;
transientIMyService1 = localInstance_1182D1277.MyService;
Integration.CompositionInOtherProject localInstance_1182D1275 = _root._singletonCompositionInOtherProject43!;
transientIMyService1 = localInstance_1182D1275.MyService;
return new Program(transientIMyService1);
}
}
Expand Down
10 changes: 5 additions & 5 deletions readme/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ partial class Composition
// Some custom logic for creating an instance.
// For example, here's how you can inject and initialize
// an instance of a particular type:
bool localIsFake54 = isFake;
if (localIsFake54)
bool localIsFake50 = isFake;
if (localIsFake50)
{
{transientIDependency1 = new FakeDependency();
goto transientIDependency1Finish; }
}
Dependency localDependency55 = new Dependency(transientDateTimeOffset3);
localDependency55.Initialize();
transientIDependency1 = localDependency55;
Dependency localDependency51 = new Dependency(transientDateTimeOffset3);
localDependency51.Initialize();
transientIDependency1 = localDependency51;
transientIDependency1Finish:;
return new Service(transientIDependency1);
}
Expand Down
4 changes: 2 additions & 2 deletions readme/func-with-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ partial class Composition
}
}

Dependency localDependency60 = new Dependency(_root._singletonClock43!, transientInt323, transientInt324);
return localDependency60;
Dependency localDependency56 = new Dependency(_root._singletonClock43!, transientInt323, transientInt324);
return localDependency56;
};
return new Service(transientFunc1);
}
Expand Down
4 changes: 2 additions & 2 deletions readme/func-with-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ partial class Composition
{
Func<IDependency> perBlockFunc1 = new Func<IDependency>([MethodImpl(MethodImplOptions.AggressiveInlining)] () =>
{
IDependency localValue61 = new Dependency();
return localValue61;
IDependency localValue57 = new Dependency();
return localValue57;
});
return new Service(perBlockFunc1);
}
Expand Down
4 changes: 2 additions & 2 deletions readme/func.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ partial class Composition
{
Func<IDependency> perBlockFunc1 = new Func<IDependency>([MethodImpl(MethodImplOptions.AggressiveInlining)] () =>
{
IDependency localValue59 = new Dependency();
return localValue59;
IDependency localValue55 = new Dependency();
return localValue55;
});
return new Service(perBlockFunc1);
}
Expand Down
Loading

0 comments on commit 40a5802

Please sign in to comment.