Skip to content

Commit 35d511a

Browse files
committed
Improve namespace/type/target name resolution once again...
1 parent 115162a commit 35d511a

31 files changed

Lines changed: 420 additions & 304 deletions

shared/Extensions/EnumerableExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,38 @@ public static void Each<T>(this IEnumerable<T> source, Action<T> action)
2323
foreach (T element in source)
2424
action(element);
2525
}
26+
27+
// Array is allocated and the enumerable is not enumerated lazy
28+
//public static IEnumerable<T> Create<T>(params T[] args) => args;
29+
public static IEnumerable<T> Create<T>(T arg1)
30+
{
31+
yield return arg1;
32+
}
33+
public static IEnumerable<T> Create<T>(T arg1, T arg2)
34+
{
35+
yield return arg1;
36+
yield return arg2;
37+
}
38+
public static IEnumerable<T> Create<T>(T arg1, T arg2, T arg3)
39+
{
40+
yield return arg1;
41+
yield return arg2;
42+
yield return arg3;
43+
}
44+
public static IEnumerable<T> Create<T>(T arg1, T arg2, T arg3, T arg4)
45+
{
46+
yield return arg1;
47+
yield return arg2;
48+
yield return arg3;
49+
yield return arg4;
50+
}
51+
public static IEnumerable<T> Create<T>(T arg1, T arg2, T arg3, T arg4, T arg5)
52+
{
53+
yield return arg1;
54+
yield return arg2;
55+
yield return arg3;
56+
yield return arg4;
57+
yield return arg5;
58+
}
2659
}
2760
}

shared/Http/RouteBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static class RouteBuilder
77
{
88
public static string BuildRoute(string areaName, string controllerName, string childRoute)
99
{
10-
string url = String.Join("/", new[] { areaName, controllerName, childRoute }.Where(x => !String.IsNullOrEmpty(x)));
10+
string url = String.Join("/", EnumerableExtensions.Create(areaName, controllerName, childRoute).Where(x => !String.IsNullOrEmpty(x)));
1111
return url;
1212
}
1313
}

src/Dibix.Http.Server/Runtime/HttpParameterResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Dibix.Http.Server
1313
public static class HttpParameterResolver
1414
{
1515
#region Fields
16-
private static readonly ICollection<Type> KnownDependencies = new[] { typeof(IDatabaseAccessorFactory) };
16+
private static readonly Type[] KnownDependencies = { typeof(IDatabaseAccessorFactory) };
1717
private static readonly Lazy<PropertyAccessor> DebugViewAccessor = new Lazy<PropertyAccessor>(BuildDebugViewAccessor);
1818
private static readonly string ItemSourceName = ItemParameterSource.SourceName;
1919
private const string SelfPropertyName = "$SELF";
@@ -690,7 +690,7 @@ private static Expression BuildSingleValueStructuredTypeConversion(string parame
690690
, out Expression enumeratorStatement
691691
);
692692

693-
Expression block = Expression.Block(new[] { structureTypeVariable, enumeratorVariable }, structuredTypeAssign, enumeratorStatement, structureTypeVariable);
693+
Expression block = Expression.Block(EnumerableExtensions.Create(structureTypeVariable, enumeratorVariable), structuredTypeAssign, enumeratorStatement, structureTypeVariable);
694694
return block;
695695
}
696696

src/Dibix.Sdk/CodeAnalysis/Rules/AliasSqlCodeAnalysisRule.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public sealed class AliasSqlCodeAnalysisRule : SqlCodeAnalysisRule
1010

1111
public override void Visit(JoinTableReference node)
1212
{
13-
TableReferenceWithAlias unaliasedTable = new[] { node.FirstTableReference, node.SecondTableReference }.OfType<TableReferenceWithAlias>().FirstOrDefault(x => x.Alias == null);
13+
TableReferenceWithAlias unaliasedTable = EnumerableExtensions.Create(node.FirstTableReference, node.SecondTableReference)
14+
.OfType<TableReferenceWithAlias>()
15+
.FirstOrDefault(x => x.Alias == null);
16+
1417
if (unaliasedTable != null)
1518
base.Fail(unaliasedTable, "Multiple table sources must be aliased");
1619
}

src/Dibix.Sdk/CodeAnalysis/Rules/NamingConventionSqlCodeAnalysisRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public PatternNormalizer(string pattern, string namingConventionPrefix)
276276

277277
public string Normalize()
278278
{
279-
string replacementPattern = String.Join("|", new[] { @"\*" }.Concat(this._map.Keys.Select(x => $@"\<{x}\>")));
279+
string replacementPattern = String.Join("|", EnumerableExtensions.Create(@"\*").Concat(this._map.Keys.Select(x => $@"\<{x}\>")));
280280
string mask = Regex.Replace(this._pattern, replacementPattern, this.OnMatch);
281281
return mask;
282282
}

src/Dibix.Sdk/CodeGeneration/Environment/LayerName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Dibix.Sdk.CodeGeneration
22
{
3-
internal static class LayerName
3+
public static class LayerName
44
{
55
public const string Business = "Business";
66
public const string Data = "Data";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Dibix.Sdk.CodeGeneration
2+
{
3+
public class NamespacePath
4+
{
5+
public string ProductName { get; }
6+
public string AreaName { get; }
7+
public string LayerName { get; }
8+
public string RelativeNamespace { get; }
9+
public string Path { get; }
10+
11+
public NamespacePath(string productName, string areaName, string layerName, string relativeNamespace, string path)
12+
{
13+
this.ProductName = productName;
14+
this.AreaName = areaName;
15+
this.LayerName = layerName;
16+
this.RelativeNamespace = relativeNamespace;
17+
this.Path = path;
18+
}
19+
20+
public override string ToString() => this.Path;
21+
}
22+
}

src/Dibix.Sdk/CodeGeneration/Environment/NamespaceUtility.cs

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Dibix.Sdk.CodeGeneration
7+
{
8+
internal static class PathUtility
9+
{
10+
public static string EnsureAreaName(string areaName)
11+
{
12+
if (String.IsNullOrEmpty(areaName))
13+
throw new InvalidOperationException("The project does not support areas since no 'AreaName' property was specified");
14+
15+
return areaName;
16+
}
17+
18+
public static string BuildRootNamespace(string productName, string areaName)
19+
{
20+
StringBuilder sb = new StringBuilder(productName);
21+
if (!String.IsNullOrEmpty(areaName))
22+
sb.Append('.')
23+
.Append(areaName);
24+
25+
return sb.ToString();
26+
}
27+
28+
public static NamespacePath BuildAbsoluteNamespace(string productName, string areaName, string layerName, string relativeNamespace)
29+
{
30+
var (_, _, path) = BuildPath(productName, ref areaName, layerName, ref relativeNamespace, targetNamePath: null);
31+
return new NamespacePath(productName, areaName, layerName, relativeNamespace, path);
32+
}
33+
34+
public static TargetPath BuildAbsoluteTargetName(string productName, string areaName, string layerName, string relativeNamespace, string targetNamePath)
35+
{
36+
var (@namespace, typeName, path) = BuildPath(productName, ref areaName, layerName, ref relativeNamespace, targetNamePath);
37+
return new TargetPath(productName, areaName, layerName, relativeNamespace, @namespace, typeName, path);
38+
}
39+
40+
public static string BuildRelativeNamespace(string rootNamespace, string layerName, string absoluteNamespace)
41+
{
42+
bool multipleAreas = rootNamespace.IndexOf('.') < 0;
43+
int startIndex = rootNamespace.Length + 1;
44+
if (!multipleAreas)
45+
startIndex += +layerName.Length + 1;
46+
47+
return startIndex < absoluteNamespace.Length ? absoluteNamespace.Substring(startIndex) : null;
48+
}
49+
50+
private static (string @namespace, string typeName, string path) BuildPath(string productName, ref string areaName, string layerName, ref string relativeNamespace, string targetNamePath)
51+
{
52+
IList<string> relativeNamespaceParts = (relativeNamespace ?? String.Empty).Split(new []{ '.' }, StringSplitOptions.RemoveEmptyEntries).ToList();
53+
54+
string targetName = null;
55+
if (targetNamePath != null)
56+
{
57+
IList<string> childTokenParts = targetNamePath.Split('.').ToList();
58+
targetName = childTokenParts.Last();
59+
childTokenParts.RemoveAt(childTokenParts.Count - 1);
60+
relativeNamespaceParts.AddRange(childTokenParts);
61+
}
62+
63+
if (areaName == null && relativeNamespaceParts.Any())
64+
{
65+
areaName = relativeNamespaceParts[0];
66+
relativeNamespaceParts.RemoveAt(0);
67+
}
68+
69+
70+
relativeNamespace = String.Join(".", relativeNamespaceParts);
71+
string[] finalParts = EnumerableExtensions.Create(productName, areaName, layerName, relativeNamespace, targetName)
72+
.Where(x => !String.IsNullOrEmpty(x))
73+
.ToArray();
74+
75+
string path = String.Join(".", finalParts);
76+
string absoluteNamespace = targetNamePath != null ? String.Join(".", finalParts.Take(finalParts.Length - 1)) : path;
77+
return (absoluteNamespace, targetName, path);
78+
}
79+
}
80+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Dibix.Sdk.CodeGeneration
2+
{
3+
public sealed class TargetPath : NamespacePath
4+
{
5+
public string AbsoluteNamespace { get; }
6+
public string TargetName { get; }
7+
8+
public TargetPath(string productName, string areaName, string layerName, string relativeNamespace, string absoluteNamespace, string targetName, string path) : base(productName, areaName, layerName, relativeNamespace, path)
9+
{
10+
this.AbsoluteNamespace = absoluteNamespace;
11+
this.TargetName = targetName;
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)