Skip to content

Commit

Permalink
Merge pull request #584 from johelvisguzman/issue582
Browse files Browse the repository at this point in the history
Fixed exceptions for netstandard2_0
  • Loading branch information
johelvisguzman authored Feb 25, 2020
2 parents c2e1a65 + c580c75 commit a3a0bfc
Show file tree
Hide file tree
Showing 24 changed files with 288 additions and 152 deletions.
6 changes: 5 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ branches:
#---------------------------------#
cache:
- packages

# scripts that run after cloning repository
install:
- cmd: choco install dotnetcore-sdk --pre

#---------------------------------#
# build configuration #
Expand All @@ -35,7 +39,7 @@ platform: Any CPU
# build Configuration, i.e. Debug, Release, etc.
configuration: Release

os: Visual Studio 2017
image: Visual Studio 2017

before_build:
- dotnet --info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ internal static class DbDataReaderExtensions
{
public static T GetValue<T>(this DbDataReader reader, int ordinal)
{
var t = typeof(T);
var value = reader.GetValue(ordinal);

if (value == null || value == DBNull.Value)
{
return default(T);
}
else
else if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
return (T)value;
t = Nullable.GetUnderlyingType(t);
}

return (T)Convert.ChangeType(value, t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<Reference Include="System.Configuration" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="10.0.3" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public RepositoryOptionsBuilder([NotNull] IRepositoryOptions options)

#region Public Methods

#if !NETSTANDARD1_3
#if NETFULL
/// <summary>
/// Configures the repository options with the data from the <paramref name="fileName"/>; otherwise, it will configure using the default App.config.
/// </summary>
Expand Down Expand Up @@ -104,16 +104,14 @@ public virtual RepositoryOptionsBuilder UseConfiguration([CanBeNull] string file

return this;
}
#endif

#if NETSTANDARD
#else
/// <summary>
/// Configures the repository options using the specified configuration.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns>The same builder instance.</returns>
/// <remarks>Any element that is defined in the config file can be resolved using the <see cref="RepositoryDependencyResolver"/>.</remarks>
public virtual RepositoryOptionsBuilder UseConfiguration([NotNull] Microsoft.Extensions.Configuration.IConfigurationRoot configuration)
public virtual RepositoryOptionsBuilder UseConfiguration([NotNull] Microsoft.Extensions.Configuration.IConfigurationRoot configuration)
{
Guard.NotNull(configuration, nameof(configuration));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.2" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
</ItemGroup>
Expand Down
65 changes: 29 additions & 36 deletions src/DotNetToolkit.Repository/Extensions/Internal/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ internal static class TypeExtensions
/// <returns>The default value of the specified type.</returns>
public static object GetDefault([NotNull] this Type type)
{
Guard.NotNull(type, nameof(type));

return type.GetTypeInfo().IsValueType ? Activator.CreateInstance(type) : null;
return type == null ? null : (type.GetTypeInfo().IsValueType ? Activator.CreateInstance(type) : null);
}

/// <summary>
Expand All @@ -30,9 +28,7 @@ public static object GetDefault([NotNull] this Type type)
/// <returns><c>true</c> if the specified type is a <see cref="ICollection{T}"/>; otherwise, <c>false</c>.</returns>
public static bool IsGenericCollection([NotNull] this Type type)
{
Guard.NotNull(type, nameof(type));

return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(ICollection<>);
return type != null && type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(ICollection<>);
}

/// <summary>
Expand All @@ -42,9 +38,7 @@ public static bool IsGenericCollection([NotNull] this Type type)
/// <returns><c>true</c> if the specified type is nullable; otherwise, <c>false</c>.</returns>
public static bool IsNullableType([NotNull] this Type type)
{
Guard.NotNull(type, nameof(type));

return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
return type != null && type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}

/// <summary>
Expand All @@ -54,7 +48,7 @@ public static bool IsNullableType([NotNull] this Type type)
/// <returns><c>true</c> if the specified type is enumerable; otherwise, <c>false</c>.</returns>
public static bool IsEnumerable([NotNull] this Type type)
{
return typeof(IEnumerable).IsAssignableFrom(Guard.NotNull(type, nameof(type)));
return type != null && typeof(IEnumerable).IsAssignableFrom(Guard.NotNull(type, nameof(type)));
}

/// <summary>
Expand All @@ -65,12 +59,12 @@ public static bool IsEnumerable([NotNull] this Type type)
/// <returns><c>true</c> if specified type implements the specified interface type; otherwise, <c>false</c>.</returns>
public static bool ImplementsInterface([NotNull] this Type type, [NotNull] Type interfaceType)
{
Guard.NotNull(type, nameof(type));
Guard.NotNull(interfaceType, nameof(interfaceType));

return interfaceType.IsAssignableFrom(type) ||
type.IsGenericType(interfaceType) ||
type.GetTypeInfo().ImplementedInterfaces.Any(@interface => IsGenericType(@interface, interfaceType));
return type != null && interfaceType != null &&
(
interfaceType.IsAssignableFrom(type) ||
type.IsGenericType(interfaceType) ||
type.GetTypeInfo().ImplementedInterfaces.Any(@interface => IsGenericType(@interface, interfaceType))
);
}

/// <summary>
Expand All @@ -81,10 +75,7 @@ public static bool ImplementsInterface([NotNull] this Type type, [NotNull] Type
/// <returns><c>true</c> if the specified type is a generic type of the specified interface type; otherwise, <c>false</c>.</returns>
public static bool IsGenericType([NotNull] this Type type, [NotNull] Type genericType)
{
Guard.NotNull(type, nameof(type));
Guard.NotNull(genericType, nameof(genericType));

return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == genericType;
return type != null && genericType != null && type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == genericType;
}

/// <summary>
Expand All @@ -95,7 +86,8 @@ public static bool IsGenericType([NotNull] this Type type, [NotNull] Type generi
/// <returns>The converted result.</returns>
public static object ConvertTo([NotNull] this Type type, [CanBeNull] string value)
{
Guard.NotNull(type, nameof(type));
if (type == null)
return null;

object Result = null;

Expand Down Expand Up @@ -167,7 +159,8 @@ public static object ConvertTo([NotNull] this Type type, [CanBeNull] string valu
/// <returns>The new instance of the specified type.</returns>
public static object InvokeConstructor([NotNull] this Type type, [CanBeNull] Dictionary<string, string> keyValues)
{
Guard.NotNull(type, nameof(type));
if (type == null)
return null;

if (keyValues == null || keyValues.Count == 0)
return Activator.CreateInstance(type);
Expand Down Expand Up @@ -220,20 +213,20 @@ select pi
{
// Try to get all the values for the parameters we already have,
// and set the rest to their default value
var args = matchedCtorParams.Value.Select(pi =>
{
// If we find a matching parameter, then delete it from the collection,
// that way we don't try to initialize a property that has the same name
if (kvs.ContainsKey(pi.Name))
{
kvs.TryGetValue(pi.Name, out var value);
kvs.Remove(pi.Name);

return pi.ParameterType.ConvertTo(value);
}

return pi.ParameterType.GetDefault();
}).ToArray();
var args = matchedCtorParams.Value.Select(pi =>
{
// If we find a matching parameter, then delete it from the collection,
// that way we don't try to initialize a property that has the same name
if (kvs.ContainsKey(pi.Name))
{
kvs.TryGetValue(pi.Name, out var value);
kvs.Remove(pi.Name);

return pi.ParameterType.ConvertTo(value);
}

return pi.ParameterType.GetDefault();
}).ToArray();

obj = matchedCtorParams.Key.Invoke(args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !NETSTANDARD1_3
#if NETFULL

namespace DotNetToolkit.Repository.Internal.ConfigFile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ internal class ConfigurationSection : ConfigFile.IConfigurationSection
private const string DefaultContextFactorySectionKey = "defaultContextFactory";
private const string LoggingProviderSectionKey = "loggingProvider";
private const string CachingProviderSectionKey = "cachingProvider";
private const string MappingProviderSectionKey = "mappingProvider";
private const string InterceptorCollectionSectionKey = "interceptors";
private const string ParameterCollectionSectionKey = "parameters";
private const string TypeKey = "type";
private const string ExpiryKey = "expiry";

private readonly IConfigurationSection _root;

Expand Down Expand Up @@ -77,6 +77,12 @@ public ICacheProvider GetCachingProvider()
if (section != null)
{
var value = GetTypedValue<ICacheProvider>(section);
var expiry = ExtractExpiry(section);

if (expiry != null)
{
value.Expiry = expiry;
}

return value;
}
Expand All @@ -97,7 +103,7 @@ public IReadOnlyDictionary<Type, Func<IRepositoryInterceptor>> GetInterceptors()
{
var type = ExtractType(subSection, isRequired: true);

interceptorsDict.Add(type, () => GetTypedValue<IRepositoryInterceptor>(section, type));
interceptorsDict.Add(type, () => GetTypedValue<IRepositoryInterceptor>(subSection, type));
}
}
}
Expand Down Expand Up @@ -138,6 +144,20 @@ private static Type ExtractType([NotNull] IConfigurationSection section, bool is
return Type.GetType(value, throwOnError: true);
}

private static TimeSpan? ExtractExpiry([NotNull] IConfigurationSection section)
{
Guard.NotNull(section, nameof(section));

var keyValues = ExtractParameters(section);

if (keyValues.ContainsKey(ExpiryKey))
{
return TimeSpan.Parse(keyValues[ExpiryKey]);
}

return null;
}

private static KeyValuePair<string, string> ExtractKeyValue([NotNull] IConfigurationSection section)
{
Guard.NotNull(section, nameof(section));
Expand Down
8 changes: 0 additions & 8 deletions src/DotNetToolkit.Repository/Utility/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ internal static class Guard
[ContractAnnotation("value:null => halt")]
public static T NotNull<T>([ValidatedNotNull] [NoEnumeration] T value, [InvokerParameterName] string parameterName)
{
#if NETSTANDARD2_0
if (typeof(T).IsNullableType() && value == null)
#else
if (ReferenceEquals(value, null))
#endif
{
NotEmpty(parameterName, nameof(parameterName));

Expand Down Expand Up @@ -74,11 +70,7 @@ public static ICollection<T> NotEmpty<T>([ValidatedNotNull] [NoEnumeration] ICol
[ContractAnnotation("value:null => halt")]
public static T EnsureNotNull<T>([ValidatedNotNull] [NoEnumeration] T value, string message) where T : class
{
#if NETSTANDARD2_0
if (typeof(T).IsNullableType() && value == null)
#else
if (ReferenceEquals(value, null))
#endif
throw new InvalidOperationException(message);

return value;
Expand Down
Loading

0 comments on commit a3a0bfc

Please sign in to comment.