Skip to content

Commit

Permalink
Merge pull request #3 from piotr-lasota/contracts-type-narrowing
Browse files Browse the repository at this point in the history
Contracts type narrowing
  • Loading branch information
spetz authored Dec 18, 2022
2 parents 05612a2 + e919922 commit 829eb28
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/Modular.Abstractions/Contracts/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using Modular.Abstractions.Messaging;

namespace Modular.Abstractions.Contracts;

public abstract class Contract<T> : IContract where T : class
public abstract class Contract<T> : IContract where T : class, IMessage
{
private readonly ISet<string> _required = new HashSet<string>();
public Type Type { get; } = typeof(T);
Expand Down
22 changes: 14 additions & 8 deletions src/Modular.Infrastructure/Contracts/ContractRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ContractRegistry(IModuleRegistry moduleRegistry, ILogger<ContractRegistry
_logger = logger;
}

public IContractRegistry Register<T>() where T : class
public IContractRegistry Register<T>() where T : class, IContract
{
var contract = GetContractType<T>();
_contracts.Add(contract);
Expand Down Expand Up @@ -78,7 +78,7 @@ private void ValidatePaths()
throw new ContractException($"Request registration was not found for path: '{path}'.");
}

_logger.LogTrace($"Validating the contracts for path: '{path}'...");
_logger.LogTrace("Validating the contracts for path: {Path}", path);
if (requestType != typeof(Empty))
{
ValidateContract(requestType, path);
Expand All @@ -89,7 +89,7 @@ private void ValidatePaths()
ValidateContract(responseType, path);
}

_logger.LogTrace($"Validated the contracts for path: '{path}'.");
_logger.LogTrace("Validated the contracts for path: {Path}", path);
}
}

Expand Down Expand Up @@ -129,8 +129,11 @@ private void ValidateContract(Type contractType, string path = null)
throw new ContractException($"Contract: '{contractName}' was not found in module: '{module}'.");
}

_logger.LogTrace($"Validating the contract for: '{contractName}', " +
$"from module: '{contractModule}', original module: '{module}'...");
_logger.LogTrace(
"Validating the contract for: '{ContractName}', from module: '{ContractModule}', original module: '{Module}'",
contractName,
contractModule,
module);

var originalContract = FormatterServices.GetUninitializedObject(originalType);
var originalContractType = originalContract.GetType();
Expand All @@ -144,8 +147,11 @@ private void ValidateContract(Type contractType, string path = null)
contractModule, path);
}

_logger.LogTrace($"Successfully validated the contract for: '{contractName}', " +
$"from module: '{contractModule}', original module: '{module}'.");
_logger.LogTrace(
"Successfully validated the contract for: '{ContractName}', from module: '{ContractModule}', original module: '{Module}'",
contractName,
contractModule,
module);
}

private static void ValidateProperty(PropertyInfo localProperty, PropertyInfo originalProperty,
Expand Down Expand Up @@ -211,7 +217,7 @@ private static PropertyInfo GetProperty(Type type, string name, string contractN
}
}

private class Empty
private class Empty : IMessage
{
}

Expand Down
3 changes: 2 additions & 1 deletion src/Modular.Infrastructure/Contracts/IContractRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Collections.Generic;
using System.Reflection;
using Modular.Abstractions.Contracts;

namespace Modular.Infrastructure.Contracts;

public interface IContractRegistry
{
IContractRegistry Register<T>() where T : class;
IContractRegistry Register<T>() where T : class, IContract;

IContractRegistry RegisterPath(string path);

Expand Down

0 comments on commit 829eb28

Please sign in to comment.