Add envelope type handling and validation methods

Introduced new extension methods for the `IEnvelope` interface:
- Added `Type` method to retrieve and validate `EnvelopeTypeId`, throwing a `DataIntegrityException` for invalid values.
- Added `SetType` method to set the `EnvelopeTypeId` based on a given `EnvelopeType`.
- Added `WithType` method to enable setting `EnvelopeTypeId` with method chaining.

Included conditional compilation for .NET Framework compatibility and added necessary `using` directives for constants and exceptions.
This commit is contained in:
2026-11-13 09:53:21 +01:00
parent 3c80bc4d26
commit 67e435c3fa

View File

@@ -1,4 +1,11 @@
namespace EnvelopeGenerator.Domain.Interfaces
using EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Domain.Exceptions;
#if NETFRAMEWORK
using System;
using System.Linq;
#endif
namespace EnvelopeGenerator.Domain.Interfaces
{
public interface IEnvelope
{
@@ -27,5 +34,37 @@
{
return envelope.DocResult != null;
}
public static EnvelopeType Type(this IEnvelope envelope)
{
if (!Enum.IsDefined(typeof(EnvelopeType), envelope.EnvelopeTypeId))
{
#if NETFRAMEWORK
var values = Enum.GetValues(typeof(EnvelopeType)).Cast<EnvelopeType>();
#else
var values = Enum.GetValues<EnvelopeType>();
#endif
var validValues = string.Join(", ", values.Select(value => $"{(int)value} = {value}"));
throw new DataIntegrityException(
$"Invalid EnvelopeTypeId '{envelope.EnvelopeTypeId}' for envelope. " +
$"Valid {nameof(EnvelopeType)} values are: [{validValues}]. " +
"This indicates a data integrity issue: the envelope may have been " +
"created or updated with an invalid EnvelopeTypeId in the database.");
}
return (EnvelopeType)Enum.ToObject(typeof(EnvelopeType), envelope.EnvelopeTypeId);
}
public static void SetType(this IEnvelope envelope, EnvelopeType type)
{
envelope.EnvelopeTypeId = (int)type;
}
public static TEnvelope WithType<TEnvelope>(this TEnvelope envelope, EnvelopeType type) where TEnvelope : IEnvelope
{
envelope.SetType(type);
return envelope;
}
}
}