Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Dec 16, 2024
1 parent 18eccab commit 85731a0
Show file tree
Hide file tree
Showing 36 changed files with 529 additions and 142 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ dotnet run
- [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
- [Schrödinger's cat](readme/Console.md)
Expand Down
1 change: 1 addition & 0 deletions readme/advanced-interception.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal partial class Composition : IInterceptor
_interceptors = [this];
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private partial T OnDependencyInjection<T>(
in T value,
object? tag,
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 localValue79 = new Session(transientComposition3);
return localValue79;
Session localValue85 = new Session(transientComposition3);
return localValue85;
});
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 localCancellationToken29 = cancellationToken;
CancellationToken localCancellationToken35 = cancellationToken;
TaskCreationOptions transientTaskCreationOptions3 = TaskCreationOptions.None;
TaskCreationOptions localTaskCreationOptions30 = transientTaskCreationOptions3;
TaskCreationOptions localTaskCreationOptions36 = transientTaskCreationOptions3;
TaskContinuationOptions transientTaskContinuationOptions4 = TaskContinuationOptions.None;
TaskContinuationOptions localTaskContinuationOptions31 = transientTaskContinuationOptions4;
TaskContinuationOptions localTaskContinuationOptions37 = transientTaskContinuationOptions4;
TaskScheduler transientTaskScheduler5 = TaskScheduler.Default;
TaskScheduler localTaskScheduler32 = transientTaskScheduler5;
perBlockTaskFactory2 = new TaskFactory<IService>(localCancellationToken29, localTaskCreationOptions30, localTaskContinuationOptions31, localTaskScheduler32);
TaskScheduler localTaskScheduler38 = transientTaskScheduler5;
perBlockTaskFactory2 = new TaskFactory<IService>(localCancellationToken35, localTaskCreationOptions36, localTaskContinuationOptions37, localTaskScheduler38);
Func<IService> perBlockFunc1 = new Func<IService>([MethodImpl(MethodImplOptions.AggressiveInlining)] () =>
{
IService localValue33 = new Service(new Dependency());
return localValue33;
IService localValue39 = new Service(new Dependency());
return localValue39;
});
Task<IService> transientTask0;
// Injects an instance factory
Func<IService> localFactory34 = perBlockFunc1;
Func<IService> localFactory40 = perBlockFunc1;
// Injects a task factory creating and scheduling task objects
TaskFactory<IService> localTaskFactory35 = perBlockTaskFactory2;
TaskFactory<IService> localTaskFactory41 = perBlockTaskFactory2;
// Creates and starts a task using the instance factory
transientTask0 = localTaskFactory35.StartNew(localFactory34);
transientTask0 = localTaskFactory41.StartNew(localFactory40);
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 localBaseComposition81 = transientComposition3;
Composition localBaseComposition87 = transientComposition3;
// Creates a session
var localSession82= new Composition(localBaseComposition81);
var localSession88= new Composition(localBaseComposition87);
// Provides a root
transientIService2 = localSession82.SessionRoot;
IService localValue80 = transientIService2;
return localValue80;
transientIService2 = localSession88.SessionRoot;
IService localValue86 = transientIService2;
return localValue86;
});
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_1182D12736 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12736.GetDependency<int>();
Facade localInstance_1182D12742 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12742.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_1182D12738 = _root._singletonFacade43!;
_root._singletonIDependency0 = localInstance_1182D12738.Dependency;
Facade localInstance_1182D12744 = _root._singletonFacade43!;
_root._singletonIDependency0 = localInstance_1182D12744.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_1182D12737 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12737.Dependency;
Facade localInstance_1182D12743 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12743.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> localDependency42= new Dependency<Guid>();
localDependency42.SetId(transientGuid2);
localDependency42.Name = name;
transientDependency1 = localDependency42;
Dependency<Guid> localDependency48= new Dependency<Guid>();
localDependency48.SetId(transientGuid2);
localDependency48.Name = name;
transientDependency1 = localDependency48;
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 localDependency39= new Dependency();
localDependency39.SetId(transientGuid2);
localDependency39.Name = name;
transientDependency1 = localDependency39;
var localDependency45= new Dependency();
localDependency45.SetId(transientGuid2);
localDependency45.Name = name;
transientDependency1 = localDependency45;
return new Service(transientDependency1);
}
}
Expand Down
192 changes: 192 additions & 0 deletions readme/di-tracing-via-serilog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#### DI tracing via serilog

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


```c#
interface IDependency;

class Dependency : IDependency;

interface IService
{
IDependency Dependency { get; }
}

class Service : IService
{
public Service(ILogger<Service> log, IDependency dependency)
{
Dependency = dependency;
log.Information("Created");
}

public IDependency Dependency { get; }
}

interface ILogger<T>: Serilog.ILogger;

class Logger<T>(Serilog.ILogger logger) : ILogger<T>
{
private readonly Serilog.ILogger _logger =
logger.ForContext(typeof(T));

public void Write(LogEvent logEvent) =>
_logger.Write(logEvent);
}

partial class Composition: ILogEventSink
{
private readonly Action<LogEvent>? _eventsHandler;
private readonly Serilog.ILogger? _logger;
private readonly ILogger<Composition>? _compositionLogger;

public Composition(Action<LogEvent> eventsHandler)
: this()
{
_eventsHandler = eventsHandler;
_logger = new Serilog.LoggerConfiguration()
.WriteTo.Sink(this)
.CreateLogger();
_compositionLogger = new Logger<Composition>(_logger);
}

private void Setup() =>
DI.Setup(nameof(Composition))

.Hint(Hint.OnNewInstance, "On")
.Hint(Hint.OnDependencyInjection, "On")
.Bind<Serilog.ILogger>().To(_ => _logger!)
.Bind().As(Lifetime.Singleton).To<Logger<TT>>()

.Bind().To<Dependency>()
.Bind().To<Service>()
.Root<IService>(nameof(Root));

public void Emit(LogEvent logEvent) =>
_eventsHandler?.Invoke(logEvent);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
partial void OnNewInstance<T>(ref T value, object? tag, Lifetime lifetime) =>
_compositionLogger?.Information("Created [{Value}], tag [{Tag}] as {Lifetime}", value, tag, lifetime);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private partial T OnDependencyInjection<T>(in T value, object? tag, Lifetime lifetime)
{
_compositionLogger?.Information("Injected [{Value}], tag [{Tag}] as {Lifetime}", value, tag, lifetime);
return value;
}
}

var events = new List<LogEvent>();
var composition = new Composition(logEvent => events.Add(logEvent));
var service = composition.Root;
```

The following partial class will be generated:

```c#
partial class Composition
{
private readonly Composition _root;
private readonly Lock _lock;

private Logger<Service>? _singletonLogger47;

[OrdinalAttribute(256)]
public Composition()
{
_root = this;
_lock = new Lock();
}

internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
_lock = _root._lock;
}

public IService Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
Dependency transientDependency1 = new Dependency();
OnNewInstance<Dependency>(ref transientDependency1, null, Lifetime.Transient);
if (_root._singletonLogger47 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonLogger47 is null)
{
Serilog.ILogger transientILogger2 = _logger!;
OnNewInstance<Serilog.ILogger>(ref transientILogger2, null, Lifetime.Transient);
Logger<Service> _singletonLogger47Temp;
_singletonLogger47Temp = new Logger<Service>(OnDependencyInjection<Serilog.ILogger>(transientILogger2, null, Lifetime.Transient));
OnNewInstance<Logger<Service>>(ref _singletonLogger47Temp, null, Lifetime.Singleton);
Thread.MemoryBarrier();
_root._singletonLogger47 = _singletonLogger47Temp;
}
}
}

Service transientService0 = new Service(OnDependencyInjection<ILogger<Service>>(_root._singletonLogger47!, null, Lifetime.Singleton), OnDependencyInjection<IDependency>(transientDependency1, null, Lifetime.Transient));
OnNewInstance<Service>(ref transientService0, null, Lifetime.Transient);
return OnDependencyInjection<IService>(transientService0, null, Lifetime.Transient);
}
}


partial void OnNewInstance<T>(ref T value, object? tag, Lifetime lifetime);

private partial T OnDependencyInjection<T>(in T value, object? tag, Lifetime lifetime);
}
```

Class diagram:

```mermaid
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
LoggerᐸServiceᐳ --|> ILoggerᐸServiceᐳ
Dependency --|> IDependency
Composition ..> Service : IService Root
Service o-- "Singleton" LoggerᐸServiceᐳ : ILoggerᐸServiceᐳ
Service *-- Dependency : IDependency
LoggerᐸServiceᐳ *-- ILogger : ILogger
namespace Pure.DI.UsageTests.Advanced.DITracingViaSerilogScenario {
class Composition {
<<partial>>
+IService Root
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class ILoggerᐸServiceᐳ {
<<interface>>
}
class IService {
<<interface>>
}
class LoggerᐸServiceᐳ {
+Logger(ILogger logger)
}
class Service {
+Service(ILoggerᐸServiceᐳ log, IDependency dependency)
}
}
namespace Serilog {
class ILogger {
<<interface>>
}
}
```

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 localIsFake40 = isFake;
if (localIsFake40)
bool localIsFake46 = isFake;
if (localIsFake46)
{
{transientIDependency1 = new FakeDependency();
goto transientIDependency1Finish; }
}
Dependency localDependency41 = new Dependency(transientDateTimeOffset3);
localDependency41.Initialize();
transientIDependency1 = localDependency41;
Dependency localDependency47 = new Dependency(transientDateTimeOffset3);
localDependency47.Initialize();
transientIDependency1 = localDependency47;
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 localDependency46 = new Dependency(_root._singletonClock43!, transientInt323, transientInt324);
return localDependency46;
Dependency localDependency52 = new Dependency(_root._singletonClock43!, transientInt323, transientInt324);
return localDependency52;
};
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 localValue47 = new Dependency();
return localValue47;
IDependency localValue53 = new Dependency();
return localValue53;
});
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 localValue45 = new Dependency();
return localValue45;
IDependency localValue51 = new Dependency();
return localValue51;
});
return new Service(perBlockFunc1);
}
Expand Down
Loading

0 comments on commit 85731a0

Please sign in to comment.