-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
477 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
"NETSTANDARD", | ||
"Newobj", | ||
"overriden", | ||
"pfid", | ||
"rebalance", | ||
"Serilog", | ||
"Statefully", | ||
|
12 changes: 12 additions & 0 deletions
12
playground/DryIoc.Messages.MediatRLikeExample.CompileTimeDI/.config/dotnet-tools.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": 1, | ||
"isRoot": true, | ||
"tools": { | ||
"dotnet-t4": { | ||
"version": "2.3.1", | ||
"commands": [ | ||
"t4" | ||
] | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ssages.MediatRLikeExample.CompileTimeDI/CompileTimeDI/CompileTimeRegistrations.Example.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma warning disable 1591 | ||
|
||
namespace Example | ||
{ | ||
public interface IService { } | ||
|
||
public class MyService : IService | ||
{ | ||
public MyService(IDependencyA a, DependencyB<string> b, RuntimeDependencyC c) { } | ||
} | ||
|
||
public interface IDependencyA { } | ||
|
||
public class DependencyA : IDependencyA { } | ||
|
||
// let's make it struct for fun | ||
public struct DependencyB<T> | ||
{ | ||
public readonly IDependencyA A; | ||
public DependencyB(IDependencyA a) => A = a; | ||
} | ||
|
||
public class RuntimeDependencyC | ||
{ | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...essages.MediatRLikeExample.CompileTimeDI/CompileTimeDI/CompileTimeRegistrations.ttinclude
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<# | ||
// TODO: | ||
// 1. Fill-in method `GetContainerWithRegistrations` below with creation of DryIoc `Container` and registrations. | ||
// 2. Specify the resolution roots via `SpecifyResolutionRoots` method, see example below. | ||
// 3. Save the "Container.Generated.tt" file. Confirm the Visual Studio prompt if any. | ||
// 4. Check the "Container.Generated.cs" for the generated results and issues. | ||
// | ||
// Note: | ||
// - When specifying assembly path, you may use $(SolutionDir), $(ProjectDir), $(Configuration) parameters. | ||
// | ||
#> | ||
<#@ import Namespace="DryIoc" #> | ||
<#@ import Namespace="DryIoc.ImTools" #> | ||
<#// TODO: Insert the assemblies and namespaces of your services to be registered in container #> | ||
<#@ import Namespace="Example" #> | ||
<#+ | ||
// TODO: Specify the container and registrations ... | ||
IContainer GetContainerWithRegistrations() | ||
{ | ||
var container = new Container(); | ||
|
||
// TODO: Register compile-time resolved services using the same DryIoc API | ||
// or move part (or all) of the existing registrations here from your current DI configuration. | ||
|
||
// These are example registrations to build the IService resolution root and its dependencies | ||
container.Register<IService, MyService>(); | ||
container.Register<IDependencyA, DependencyA>(); | ||
container.Register(typeof(DependencyB<>), setup: Setup.With(asResolutionCall: true)); | ||
|
||
// Note that `RegisterDelegate`, `RegisterInstance` and `Use` methods are not supported because | ||
// they using the run-time state. | ||
// Instead you may use `RegisterPlaceholder` to put a hole in the generated object graph, | ||
// then you fill it in with the run-time registration, e.g. `container.Register<RuntimeDependency>();` | ||
container.RegisterPlaceholder<RuntimeDependencyC>(); | ||
|
||
// You may batch register assemblies as well | ||
// container.RegisterMany(new[] { MyAssembly }); | ||
|
||
return container; | ||
} | ||
|
||
// TODO: For each passed registration specify what resolution roots it provides, null if none | ||
ServiceInfo[] SpecifyResolutionRoots(ServiceRegistrationInfo reg) => | ||
reg.AsResolutionRoot ? reg.ToServiceInfo().One() : null; | ||
|
||
// TODO: Specify the resolution roots explicitly | ||
ServiceInfo[] CustomResolutionRoots = | ||
{ | ||
ServiceInfo.Of<Example.IService>(), | ||
}; | ||
|
||
// TODO: Specify the namespace to go into `using` instead of qualifying its types all the times | ||
// You may generate the Container.Generated.cs first, then look what you want to move to `using` | ||
string[] NamespaceUsings = | ||
{ | ||
"Example", | ||
//"Foo.Bar.Buzz", | ||
}; | ||
#> |
113 changes: 113 additions & 0 deletions
113
...und/DryIoc.Messages.MediatRLikeExample.CompileTimeDI/CompileTimeDI/Container.Generated.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// <auto-generated/> | ||
/* | ||
The MIT License (MIT) | ||
Copyright (c) 2016-2022 Maksim Volkau | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
/* | ||
======================================================================================================== | ||
The code below is generated automatically at compile-time and changes to it will be lost on the next gen. | ||
======================================================================================================== | ||
Generation is completed successfully. | ||
-------------------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using DryIoc.ImTools; | ||
|
||
// Provided `NamespaceUsings`: | ||
using Example; | ||
|
||
namespace DryIoc | ||
{ | ||
partial class Container | ||
{ | ||
partial void HasCompileTimeGeneratedContainer(ref bool hasIt) => hasIt = true; | ||
|
||
partial void ResolveGenerated(ref object service, Type serviceType) | ||
{ | ||
if (serviceType == typeof(IService)) | ||
service = Get_IService_0(this); | ||
} | ||
|
||
partial void ResolveGenerated(ref object service, | ||
Type serviceType, object serviceKey, Type requiredServiceType, Request preRequestParent, object[] args) | ||
{ | ||
if (serviceType == typeof(DependencyB<string>)) | ||
{ | ||
if (serviceKey == null && | ||
requiredServiceType == null && | ||
Equals(preRequestParent, Request.Empty.Push( | ||
typeof(IService), | ||
20001, | ||
typeof(MyService), | ||
Reuse.Transient, | ||
RequestFlags.IsResolutionCall|RequestFlags.DoNotPoolRequest))) | ||
service = GetDependency_DependencyB_0(this); | ||
} | ||
} | ||
|
||
partial void ResolveManyGenerated(ref IEnumerable<ResolveManyResult> services, Type serviceType) | ||
{ | ||
services = ResolveManyGenerated(serviceType); | ||
} | ||
|
||
private IEnumerable<ResolveManyResult> ResolveManyGenerated(Type serviceType) | ||
{ | ||
if (serviceType == typeof(IService)) | ||
yield return ResolveManyResult.Of(r => Get_IService_0(r)); | ||
} | ||
|
||
internal static IService Get_IService_0(IResolverContext r) => | ||
new MyService( | ||
new DependencyA(), | ||
((DependencyB<string>)r.Resolve( | ||
typeof(DependencyB<string>), | ||
default(object), | ||
IfUnresolved.Throw, | ||
default(System.Type), | ||
Request.Empty.Push( | ||
typeof(IService), | ||
20001, | ||
typeof(MyService), | ||
Reuse.Transient, | ||
RequestFlags.IsResolutionCall|RequestFlags.StopRecursiveDependencyCheck|RequestFlags.DoNotPoolRequest), | ||
default(object[]))), | ||
((RuntimeDependencyC)r.Resolve( | ||
typeof(RuntimeDependencyC), | ||
default(object), | ||
IfUnresolved.Throw, | ||
default(System.Type), | ||
Request.Empty.Push( | ||
typeof(IService), | ||
20001, | ||
typeof(MyService), | ||
Reuse.Transient, | ||
RequestFlags.IsResolutionCall|RequestFlags.StopRecursiveDependencyCheck|RequestFlags.DoNotPoolRequest), | ||
default(object[])))); | ||
|
||
internal static DependencyB<string> GetDependency_DependencyB_0(IResolverContext r) => | ||
new DependencyB<string>(new DependencyA()); | ||
|
||
} | ||
} |
Oops, something went wrong.