/// <summary> /// Gets an instance of <see cref="IWrapperProvider"/> for the supplied /// type. /// </summary> /// <param name="wrapperProviderFactories">A list of <see cref="IWrapperProviderFactory"/>.</param> /// <param name="wrapperProviderContext">The <see cref="WrapperProviderContext"/>.</param> /// <returns>An instance of <see cref="IWrapperProvider"/> if there is a wrapping provider for the /// supplied type, else null.</returns> public static IWrapperProvider GetWrapperProvider( this IEnumerable<IWrapperProviderFactory> wrapperProviderFactories, WrapperProviderContext wrapperProviderContext) { if (wrapperProviderFactories == null) { throw new ArgumentNullException(nameof(wrapperProviderFactories)); } if (wrapperProviderContext == null) { throw new ArgumentNullException(nameof(wrapperProviderContext)); } foreach (var wrapperProviderFactory in wrapperProviderFactories) { var wrapperProvider = wrapperProviderFactory.GetProvider(wrapperProviderContext); if (wrapperProvider != null) { return wrapperProvider; } } return null; }
/// <summary> /// Gets an <see cref="EnumerableWrapperProvider"/> for the provided context. /// </summary> /// <param name="context">The <see cref="WrapperProviderContext"/>.</param> /// <returns>An instance of <see cref="EnumerableWrapperProvider"/> if the declared type is /// an interface and implements <see cref="IEnumerable{T}"/>.</returns> public IWrapperProvider GetProvider(WrapperProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.IsSerialization) { // Example: IEnumerable<SerializableError> var declaredType = context.DeclaredType; var declaredTypeInfo = declaredType.GetTypeInfo(); // We only wrap interfaces types(ex: IEnumerable<T>, IQueryable<T>, IList<T> etc.) and not // concrete types like List<T>, Collection<T> which implement IEnumerable<T>. if (declaredType != null && declaredTypeInfo.IsInterface && declaredTypeInfo.IsGenericType) { var enumerableOfT = ClosedGenericMatcher.ExtractGenericInterface( declaredType, typeof(IEnumerable<>)); if (enumerableOfT != null) { var elementType = enumerableOfT.GenericTypeArguments[0]; var wrapperProviderContext = new WrapperProviderContext(elementType, context.IsSerialization); var elementWrapperProvider = _wrapperProviderFactories.GetWrapperProvider(wrapperProviderContext); return new EnumerableWrapperProvider(enumerableOfT, elementWrapperProvider); } } } return null; }
/// <summary> /// Gets an <see cref="EnumerableWrapperProvider"/> for the provided context. /// </summary> /// <param name="context">The <see cref="WrapperProviderContext"/>.</param> /// <returns>An instance of <see cref="EnumerableWrapperProvider"/> if the declared type is /// an interface and implements <see cref="IEnumerable{T}"/>.</returns> public IWrapperProvider?GetProvider(WrapperProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.IsSerialization) { // Example: IEnumerable<SerializableError> var declaredType = context.DeclaredType; // We only wrap interfaces types(ex: IEnumerable<T>, IQueryable<T>, IList<T> etc.) and not // concrete types like List<T>, Collection<T> which implement IEnumerable<T>. if (declaredType != null && declaredType.IsInterface && declaredType.IsGenericType) { var enumerableOfT = ClosedGenericMatcher.ExtractGenericInterface( declaredType, typeof(IEnumerable <>)); if (enumerableOfT != null) { var elementType = enumerableOfT.GenericTypeArguments[0]; var wrapperProviderContext = new WrapperProviderContext(elementType, context.IsSerialization); var elementWrapperProvider = _wrapperProviderFactories.GetWrapperProvider(wrapperProviderContext); return(new EnumerableWrapperProvider(enumerableOfT, elementWrapperProvider)); } } } return(null); }
/// <summary> /// Gets an instance of <see cref="IWrapperProvider"/> for the supplied /// type. /// </summary> /// <param name="wrapperProviderFactories">A list of <see cref="IWrapperProviderFactory"/>.</param> /// <param name="wrapperProviderContext">The <see cref="WrapperProviderContext"/>.</param> /// <returns>An instance of <see cref="IWrapperProvider"/> if there is a wrapping provider for the /// supplied type, else null.</returns> public static IWrapperProvider?GetWrapperProvider( this IEnumerable <IWrapperProviderFactory> wrapperProviderFactories, WrapperProviderContext wrapperProviderContext) { if (wrapperProviderFactories == null) { throw new ArgumentNullException(nameof(wrapperProviderFactories)); } if (wrapperProviderContext == null) { throw new ArgumentNullException(nameof(wrapperProviderContext)); } foreach (var wrapperProviderFactory in wrapperProviderFactories) { var wrapperProvider = wrapperProviderFactory.GetProvider(wrapperProviderContext); if (wrapperProvider != null) { return(wrapperProvider); } } return(null); }
public IWrapperProvider GetProvider(WrapperProviderContext context) { if (context.DeclaredType == typeof(Person)) { return(new PersonWrapperProvider()); } return(null); }
public IWrapperProvider GetProvider(WrapperProviderContext context) { if (context.DeclaredType == typeof(Person)) { return new PersonWrapperProvider(); } return null; }
public void GetProvider_ReturnsNull_IfTypeDoesNotMatch() { // Arrange var providerFactory = new ProblemDetailsWrapperProviderFactory(); var context = new WrapperProviderContext(typeof(SerializableError), isSerialization: true); // Act var provider = providerFactory.GetProvider(context); // Assert Assert.Null(provider); }
public void GetProvider_ReturnsNull_ForCustomProblemDetails() { // Arrange var providerFactory = new ProblemDetailsWrapperProviderFactory(); var instance = new CustomProblemDetails(); var context = new WrapperProviderContext(instance.GetType(), isSerialization: true); // Act var provider = providerFactory.GetProvider(context); // Assert Assert.Null(provider); }
/// <summary> /// Creates an instance of <see cref="SerializableErrorWrapperProvider"/> if the provided /// <paramref name="context"/>'s <see cref="WrapperProviderContext.DeclaredType"/> is /// <see cref="Microsoft.AspNetCore.Mvc.SerializableError"/>. /// </summary> /// <param name="context">The <see cref="WrapperProviderContext"/>.</param> /// <returns> /// An instance of <see cref="SerializableErrorWrapperProvider"/> if the provided <paramref name="context"/>'s /// <see cref="WrapperProviderContext.DeclaredType"/> is /// <see cref="Microsoft.AspNetCore.Mvc.SerializableError"/>; otherwise <c>null</c>. /// </returns> public IWrapperProvider?GetProvider(WrapperProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.DeclaredType == typeof(SerializableError)) { return(new SerializableErrorWrapperProvider()); } return(null); }
public IWrapperProvider?GetProvider(WrapperProviderContext context) { if (context.DeclaredType == typeof(ProblemDetails)) { return(new WrapperProvider(typeof(ProblemDetailsWrapper), p => new ProblemDetailsWrapper((ProblemDetails)p !))); } if (context.DeclaredType == typeof(ValidationProblemDetails)) { return(new WrapperProvider(typeof(ValidationProblemDetailsWrapper), p => new ValidationProblemDetailsWrapper((ValidationProblemDetails)p !))); } return(null); }
/// <summary> /// Creates an instance of <see cref="SerializableErrorWrapperProvider"/> if the provided /// <paramref name="context"/>'s <see cref="WrapperProviderContext.DeclaredType"/> is /// <see cref="Microsoft.AspNetCore.Mvc.SerializableError"/>. /// </summary> /// <param name="context">The <see cref="WrapperProviderContext"/>.</param> /// <returns> /// An instance of <see cref="SerializableErrorWrapperProvider"/> if the provided <paramref name="context"/>'s /// <see cref="WrapperProviderContext.DeclaredType"/> is /// <see cref="Microsoft.AspNetCore.Mvc.SerializableError"/>; otherwise <c>null</c>. /// </returns> public IWrapperProvider GetProvider(WrapperProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.DeclaredType == typeof(SerializableError)) { return new SerializableErrorWrapperProvider(); } return null; }
public void DoesNot_CreateWrapperProvider_ForConcrete_EnumerableOfTImplementations( Type declaredType, object objectToBeWrapped) { // Arrange var wrapperProviderFactories = GetWrapperProviderFactories(); var enumerableWrapperProviderFactory = new EnumerableWrapperProviderFactory(wrapperProviderFactories); var wrapperProviderContext = new WrapperProviderContext(declaredType, isSerialization: true); // Act var wrapperProvider = enumerableWrapperProviderFactory.GetProvider(wrapperProviderContext); // Assert Assert.Null(wrapperProvider); }
public void GetProvider_ReturnsWrapper_ForValidationProblemDetails() { // Arrange var providerFactory = new ProblemDetailsWrapperProviderFactory(); var instance = new ValidationProblemDetails(); var context = new WrapperProviderContext(instance.GetType(), isSerialization: true); // Act var provider = providerFactory.GetProvider(context); // Assert var result = provider.Wrap(instance); var wrapper = Assert.IsType <ValidationProblemDetailsWrapper>(result); Assert.Same(instance, wrapper.ProblemDetails); }
public void Creates_WrapperProvider_EnumerableOfTInterface( Type declaredType, object objectToBeWrapped, Type expectedWrappingType) { // Arrange var wrapperProviderFactories = GetWrapperProviderFactories(); var enumerableWrapperProviderFactory = new EnumerableWrapperProviderFactory(wrapperProviderFactories); var wrapperProviderContext = new WrapperProviderContext(declaredType, isSerialization: true); // Act var wrapperProvider = enumerableWrapperProviderFactory.GetProvider(wrapperProviderContext); // Assert Assert.NotNull(wrapperProvider); Assert.Equal(expectedWrappingType, wrapperProvider.WrappingType); }