Skip to content

Commit

Permalink
added: DryIocZero example; fixed: xml-docs; updated: DryIoc to v3 pre…
Browse files Browse the repository at this point in the history
…view
  • Loading branch information
dadhi committed Mar 7, 2018
1 parent 6855403 commit 9069354
Show file tree
Hide file tree
Showing 14 changed files with 3,855 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ _TeamCity*
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
*.ncrunchsolution

# MightyMoose
*.mm.*
Expand Down
6 changes: 6 additions & 0 deletions MediatR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Examples.Unity", "samples\MediatR.Examples.Unity\MediatR.Examples.Unity.csproj", "{E6C51E44-59B4-4F4F-AFB3-4032CDDEF07A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediatR.Examples.BlueMilk", "samples\MediatR.Examples.BlueMilk\MediatR.Examples.BlueMilk.csproj", "{EF2C4BCB-061F-4679-8B78-9957B46B03BF}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediatR.Examples.DryIocZero", "samples\MediatR.Examples.DryIocZero\MediatR.Examples.DryIocZero.csproj", "{98FD2C03-9B69-4EDD-A268-BB765627FD70}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -100,6 +101,10 @@ Global
{EF2C4BCB-061F-4679-8B78-9957B46B03BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF2C4BCB-061F-4679-8B78-9957B46B03BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF2C4BCB-061F-4679-8B78-9957B46B03BF}.Release|Any CPU.Build.0 = Release|Any CPU
{98FD2C03-9B69-4EDD-A268-BB765627FD70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98FD2C03-9B69-4EDD-A268-BB765627FD70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98FD2C03-9B69-4EDD-A268-BB765627FD70}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98FD2C03-9B69-4EDD-A268-BB765627FD70}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -118,6 +123,7 @@ Global
{4A255E84-AAC5-4ED9-ABAC-822804DBA4F1} = {95CFF0CD-87A6-4CB6-A99F-42EAD0829E37}
{E6C51E44-59B4-4F4F-AFB3-4032CDDEF07A} = {95CFF0CD-87A6-4CB6-A99F-42EAD0829E37}
{EF2C4BCB-061F-4679-8B78-9957B46B03BF} = {95CFF0CD-87A6-4CB6-A99F-42EAD0829E37}
{98FD2C03-9B69-4EDD-A268-BB765627FD70} = {95CFF0CD-87A6-4CB6-A99F-42EAD0829E37}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D58286E3-878B-4ACB-8E76-F61E708D4339}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="DryIoc" Version="3.0.0-preview-02" />
<PackageReference Include="DryIoc" Version="3.0.0-preview-09" />
</ItemGroup>

</Project>
4 changes: 1 addition & 3 deletions samples/MediatR.Examples.DryIoc/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using DryIoc;
using MediatR.Pipeline;

namespace MediatR.Examples.DryIoc
{
Expand All @@ -27,7 +25,7 @@ private static IMediator BuildMediator(WrappingWriter writer)

//Pipeline works out of the box here

container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Ping).GetAssembly() }, type => type.GetTypeInfo().IsInterface);
container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Ping).GetAssembly() }, Registrator.Interfaces);

return container.Resolve<IMediator>();
}
Expand Down
383 changes: 383 additions & 0 deletions samples/MediatR.Examples.DryIocZero/Container.Generated.cs

Large diffs are not rendered by default.

286 changes: 286 additions & 0 deletions samples/MediatR.Examples.DryIocZero/Container.Generated.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
/*
The MIT License (MIT)

Copyright (c) 2016 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.
*/
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ CleanupBehavior processor="T4VSHost" CleanupAfterProcessingtemplate="true" #>
<#@ assembly Name="$(DryIocAssembly)" #>
<#@ assembly Name="$(ExpressionToCodeLibAssembly)" #>
<#@ import Namespace="DryIoc" #>
<#@ import Namespace="ImTools" #>
<#@ import Namespace="ExpressionToCodeLib" #>
<#@ assembly Name="System.Core" #>
<#@ assembly Name="System.Runtime" #>
<#@ import Namespace="System.Linq" #>
<#@ import Namespace="System.Linq.Expressions" #>
<#@ include File="$(ProjectDir)\Registrations.ttinclude" #>
<#
var container = GetContainerWithRegistrations()
.With(rules => rules.WithThrowIfRuntimeStateRequired());

var includeVariants = container.Rules.VariantGenericTypesInResolvedCollection;

var result = container.GenerateResolutionExpressions(regs => regs.SelectMany(r =>
SpecifyResolutionRoots(r).EmptyIfNull()).Concat(CustomResolutionRoots.EmptyIfNull()));

var exprToCode = ExpressionToCodeConfiguration.DefaultCodeGenConfiguration
.WithObjectStringifier(ObjectStringify.WithFullTypeNames);

string Code(object x) =>
x == null ? "null" :
x is Expression ? exprToCode.ToCode((Expression)x)
.Replace("DryIoc." + nameof(IfUnresolved), nameof(IfUnresolved))
.Replace("DryIoc." + nameof(IReuse), nameof(IReuse))
.Replace("DryIoc." + nameof(Reuse), nameof(Reuse))
.Replace("DryIoc." + nameof(SingletonReuse), nameof(SingletonReuse))
.Replace("DryIoc." + nameof(CurrentScopeReuse), nameof(CurrentScopeReuse))
.Replace("DryIoc." + nameof(FactoryType), nameof(FactoryType))
.Replace("DryIoc." + nameof(DefaultKey), nameof(DefaultKey))
.Replace("DryIoc." + nameof(DefaultDynamicKey), nameof(DefaultDynamicKey))
.Replace("DryIoc." + nameof(Request), nameof(Request))
.Replace("DryIoc." + nameof(RequestFlags), nameof(RequestFlags))
.Replace("DryIoc." + nameof(HiddenDisposable), nameof(HiddenDisposable))
.Replace("DryIoc." + nameof(ResolutionScopeName), nameof(ResolutionScopeName))
.Replace("DryIoc." + nameof(CompositeScopeName), nameof(CompositeScopeName))
: x is Request ? Code(container.GetRequestExpression((Request)x).ToExpression())
: Code(container.GetConstantExpression(x, x.GetType(), true).ToExpression());

string GetTypeNameOnly(string typeName) => typeName.Split('`').First().Split('.').Last();

string OptArg(string arg) => arg == "null" ? "" : ", " + arg;

var rootCodes = result.Roots.Select((r, i) =>
new { ServiceType = r.Key.ServiceType,
ServiceTypeCode = Code(r.Key.ServiceType),
ServiceKeyCode = Code(r.Key.ServiceKey),
RequiredServiceTypeCode = Code(r.Key.Details.RequiredServiceType),
ExpressionCode = Code(r.Value.Body),
CreateMethodName = "Get" + i + "_" + GetTypeNameOnly(r.Key.ServiceType.Name) });

var depCodes = result.ResolveDependencies.Select((r, i) =>
new { ServiceType = Code(r.Key.ServiceType),
ServiceKey = Code(r.Key.ServiceKey), ServiceKeyObject = r.Key.ServiceKey,
Expression = Code(r.Value),
RequiredServiceType = Code(r.Key.RequiredServiceType),
PreResolveParent = Code(r.Key.Parent),
CreateMethodName = "GetDep" + i + "_" + GetTypeNameOnly(r.Key.ServiceType.Name) });
#>
/*
========================================================================================================
NOTE: The code below is generated automatically at compile-time and not supposed to be changed by hand.
========================================================================================================
<# var errCount = result.Errors.Count;
if (errCount == 0) { #>
Generation is completed successfully.
<# } else { #>
There are <#=errCount#> generation issues (may be not an error dependent on context):

The issues with run-time registrations may be solved by `container.RegisterPlaceholder<T>()`
in Registrations.ttinclude. Then you can replace placeholders using `DryIocZero.Container.Register`
at runtime.

<# } #>
--------------------------------------------------------------------------------------------------------
<# var eNum = 0;
foreach(var e in result.Errors) { #>
<#=++eNum#>. <#=e.Key#>
Error: <#=e.Value.Message#>
<# } #>
*/

using System;
using System.Linq; // for Enumerable.Cast method required by LazyEnumerable<T>
using System.Collections.Generic;
using System.Threading;
using ImTools;
using static DryIocZero.ResolveManyResult;

namespace DryIocZero
{
partial class Container
{
[ExcludeFromCodeCoverage]
partial void GetLastGeneratedFactoryID(ref int lastFactoryID)
{
lastFactoryID = <#=Factory.GetNextID()#>; // generated: equals to last used Factory.FactoryID
}

[ExcludeFromCodeCoverage]
partial void ResolveGenerated(ref object service, Type serviceType)
{
<#
var index = 0;
foreach (var root in rootCodes.Where(f => f.ServiceKeyCode == "null"))
{
if (index++ > 0) WriteLine(@"
else");
#>
if (serviceType == <#=root.ServiceTypeCode#>)
service = <#=root.CreateMethodName#>(this);
<#
}
#>
}

[ExcludeFromCodeCoverage]
partial void ResolveGenerated(ref object service,
Type serviceType, object serviceKey, Type requiredServiceType, Request preRequestParent, object[] args)
{
<#
index = 0;
foreach (var rootGroup in rootCodes.Where(x => x.ServiceKeyCode != "null").GroupBy(x => x.ServiceTypeCode))
{
if (index++ > 0) WriteLine(@"
else");
#>
if (serviceType == <#=rootGroup.Key#>)
{
<#
var innerIndex = 0;
foreach (var root in rootGroup)
{
if (innerIndex++ > 0) WriteLine(@"
else");
#>
if (<#=root.ServiceKeyCode#>.Equals(serviceKey))
service = <#=root.CreateMethodName#>(this);
<#
}

#>
}
<#
}
#>
<#
foreach (var depGroup in depCodes.GroupBy(x => x.ServiceType))
{
if (index++ > 0) WriteLine(@"
else");
#>
if (serviceType == <#=depGroup.Key#>)
{
<#
var innerIndex = 0;
foreach (var dep in depGroup)
{
if (innerIndex++ > 0) WriteLine(@"
else");
#>
if (<#=dep.ServiceKeyObject == null ? "serviceKey == null"
: dep.ServiceKeyObject is DefaultKey ? "(serviceKey == null || " + dep.ServiceKey + ".Equals(serviceKey))"
: dep.ServiceKey + ".Equals(serviceKey)"#> &&
requiredServiceType == <#= dep.RequiredServiceType #> &&
Equals(preRequestParent, <#= dep.PreResolveParent #>))
service = <#=dep.CreateMethodName#>(this);
<#
}
#>
}
<#
}
#>
}

[ExcludeFromCodeCoverage]
partial void ResolveManyGenerated(ref IEnumerable<ResolveManyResult> services, Type serviceType)
{
services = ResolveManyGenerated(serviceType);
}

[ExcludeFromCodeCoverage]
private IEnumerable<ResolveManyResult> ResolveManyGenerated(Type serviceType)
{
<#
if (!rootCodes.Any())
{
#>
yield break;
<#
}
else
{
foreach (var rootGroup in rootCodes.GroupBy(x => x.ServiceType))
{
#>
if (serviceType == <#=rootGroup.First().ServiceTypeCode#>)
{
<#
foreach (var root in rootGroup)
{
#>
yield return Of(<#=root.CreateMethodName#><#=OptArg(root.ServiceKeyCode)#><#=OptArg(root.RequiredServiceTypeCode)#>);
<#
}

if (includeVariants && rootGroup.Key.IsGeneric())
{
var sourceType = rootGroup.Key;
var variants = rootCodes
.Where(x => x.ServiceType.IsGeneric() &&
x.ServiceType.GetGenericTypeDefinition() == sourceType.GetGenericTypeDefinition() &&
x.ServiceType != sourceType && x.ServiceType.IsAssignableTo(sourceType));
foreach (var variant in variants)
{
#>
yield return Of(<#=variant.CreateMethodName#><#=OptArg(variant.ServiceKeyCode)#><#=OptArg(variant.RequiredServiceTypeCode)#>); // co-variant
<#
}
}
#>
}

<#
}
}
#>
}

<#
foreach (var root in rootCodes)
{
#>
// <#=root.ServiceTypeCode#>
internal static object <#=root.CreateMethodName#>(IResolverContext r)
{
return <#=root.ExpressionCode#>;
}

<#
}
#>
<#
foreach (var dep in depCodes)
{
#>
// <#=dep.ServiceType#>
internal static object <#=dep.CreateMethodName#>(IResolverContext r)
{
return <#=dep.Expression#>;
}

<#
}
#>
}
}
Loading

0 comments on commit 9069354

Please sign in to comment.