Last active
August 14, 2022 06:13
-
-
Save mrange/8fe6617f0d373363eed1391e709063a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace CsDI | |
{ | |
partial record DI | |
{ | |
IY? _singletonOf_IY; | |
IZ? _singletonOf_IZ; | |
public void Get(out IX v) | |
{ | |
v = new X(this); | |
} | |
public void Get(out IY v) | |
{ | |
_singletonOf_IY ??= new Y(this); | |
v = _singletonOf_IY; | |
} | |
public void Get(out IZ v) | |
{ | |
_singletonOf_IZ ??= new Z(this); | |
v = _singletonOf_IZ; | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
var dependencies = new [] | |
{ | |
Transient("IX", "X") | |
, Singleton("IY", "Y") | |
, Singleton("IZ", "Z") | |
}; | |
#> | |
namespace CsDI | |
{ | |
partial record DI | |
{ | |
<# foreach(var dependency in dependencies) { #> | |
<# if (!dependency.Transient) { #> | |
<#=dependency.InterfaceName#>? _singletonOf_<#=dependency.InterfaceName#>; | |
<# } #> | |
<# } #> | |
<# foreach(var dependency in dependencies) { #> | |
public void Get(out <#=dependency.InterfaceName#> v) | |
{ | |
<# if (!dependency.Transient) { #> | |
_singletonOf_<#=dependency.InterfaceName#> ??= new <#=dependency.ClassName#>(this); | |
v = _singletonOf_<#=dependency.InterfaceName#>; | |
<# } else { #> | |
v = new <#=dependency.ClassName#>(this); | |
<# } #> | |
} | |
<# } #> | |
} | |
} | |
<#+ | |
(string InterfaceName, string ClassName, bool Transient) Transient(string inm, string cnm) | |
{ | |
return (inm ?? "<NULL>", cnm ?? "<NULL>", true); | |
} | |
(string InterfaceName, string ClassName, bool Transient) Singleton(string inm, string cnm) | |
{ | |
return (inm ?? "<NULL>", cnm ?? "<NULL>", false); | |
} | |
#> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CsDI; | |
var di = new DI(); | |
di.Get(out IX x); | |
Console.WriteLine(x.Message()); | |
namespace CsDI | |
{ | |
interface IZ | |
{ | |
string Message(); | |
} | |
interface IY | |
{ | |
string Message(); | |
} | |
interface IX | |
{ | |
string Message(); | |
} | |
class Z : IZ | |
{ | |
public Z(DI di) | |
{ | |
} | |
public string Message() | |
{ | |
return $"Z"; | |
} | |
} | |
class Y : IY | |
{ | |
IZ _z; | |
public Y(DI di) | |
{ | |
di.Get(out _z); | |
} | |
public string Message() | |
{ | |
return $"Y: _z:({_z.Message()})"; | |
} | |
} | |
class X : IX | |
{ | |
IY _y; | |
IZ _z; | |
public X(DI di) | |
{ | |
di.Get(out _y); | |
di.Get(out _z); | |
} | |
public string Message() | |
{ | |
return $"X: _y:({_y.Message()}), _z:({_z.Message()})"; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment