-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
9c79c45
commit c4c231d
Showing
3 changed files
with
63 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
50 changes: 50 additions & 0 deletions
50
src/Validation.ApiDependencyValidators/ApiDependencyValidator.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,50 @@ | ||
namespace Byndyusoft.ArchitectureTesting.Validation.ApiDependencyValidators | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Abstractions.ServiceContracts.Dependencies; | ||
using Abstractions.ServiceImplementations; | ||
using Abstractions.Validation.DependencyValidators; | ||
using Abstractions.Validation.Extensions; | ||
|
||
public class ApiDependencyValidator : DependencyValidatorBase<ApiDependency> | ||
{ | ||
private static bool IsApiClient(Assembly assembly) | ||
{ | ||
var cleanedAssemblyName = assembly.GetName().Name!.CleanString(); | ||
if (cleanedAssemblyName.Equals("Byndyusoft.ApiClient".CleanString())) | ||
return false; | ||
|
||
return cleanedAssemblyName.EndsWith(".Api.Client".CleanString()) | ||
|| cleanedAssemblyName.EndsWith(".Api.Clients".CleanString()); | ||
} | ||
|
||
private static bool IsRepositoryApiClientAssembly(Assembly assembly, string repositoryName) | ||
{ | ||
var cleanedAssemblyName = assembly.GetName().Name!.CleanString(); | ||
return cleanedAssemblyName.Equals($"{repositoryName}.Client".CleanString()) | ||
|| cleanedAssemblyName.Equals($"{repositoryName}.Clients".CleanString()); | ||
} | ||
|
||
private static bool IsValidServiceAssembly(Assembly serviceAssembly, IEnumerable<ApiDependency> dependencies) | ||
=> IsApiClient(serviceAssembly) == false | ||
|| dependencies.Any(dependency => IsRepositoryApiClientAssembly(serviceAssembly, dependency.Name)); | ||
|
||
private static bool IsValidDependency(DependencyBase dependency, IEnumerable<Assembly> serviceAssemblies) | ||
=> serviceAssemblies.Any(serviceAssembly => IsRepositoryApiClientAssembly(serviceAssembly, dependency.Name)); | ||
|
||
protected override IEnumerable<string> Validate(ApiDependency[] dependencies, ServiceImplementation serviceImplementation) | ||
=> Enumerable.Empty<string>() | ||
.Concat( | ||
serviceImplementation.ServiceAssemblies | ||
.Where(serviceAssembly => IsValidServiceAssembly(serviceAssembly, dependencies) == false) | ||
.Select(serviceAssembly => $"Assembly {serviceAssembly.GetName().Name} is not allowed by architecture") | ||
) | ||
.Concat( | ||
dependencies | ||
.Where(dependency => IsValidDependency(dependency, serviceImplementation.ServiceAssemblies) == false) | ||
.Select(dependency => $"Dependency {nameof(ApiDependency)}: {dependency} is not implemented in service") | ||
); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Validation.ApiDependencyValidators/Validation.ApiDependencyValidators.csproj
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,5 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Abstractions\Abstractions.csproj" /> | ||
</ItemGroup> | ||
</Project> |