Skip to content

Commit a3a0bfc

Browse files
Merge pull request #584 from johelvisguzman/issue582
Fixed exceptions for netstandard2_0
2 parents c2e1a65 + c580c75 commit a3a0bfc

File tree

24 files changed

+288
-152
lines changed

24 files changed

+288
-152
lines changed

appveyor.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ branches:
2424
#---------------------------------#
2525
cache:
2626
- packages
27+
28+
# scripts that run after cloning repository
29+
install:
30+
- cmd: choco install dotnetcore-sdk --pre
2731

2832
#---------------------------------#
2933
# build configuration #
@@ -35,7 +39,7 @@ platform: Any CPU
3539
# build Configuration, i.e. Debug, Release, etc.
3640
configuration: Release
3741

38-
os: Visual Studio 2017
42+
image: Visual Studio 2017
3943

4044
before_build:
4145
- dotnet --info

src/DotNetToolkit.Repository.AdoNet/Internal/DbDataReaderExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ internal static class DbDataReaderExtensions
77
{
88
public static T GetValue<T>(this DbDataReader reader, int ordinal)
99
{
10+
var t = typeof(T);
1011
var value = reader.GetValue(ordinal);
1112

1213
if (value == null || value == DBNull.Value)
1314
{
1415
return default(T);
1516
}
16-
else
17+
else if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
1718
{
18-
return (T)value;
19+
t = Nullable.GetUnderlyingType(t);
1920
}
21+
22+
return (T)Convert.ChangeType(value, t);
2023
}
2124
}
2225
}

src/DotNetToolkit.Repository.AzureStorageBlob/DotNetToolkit.Repository.AzureStorageBlob.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<Reference Include="System.Configuration" />
1414
</ItemGroup>
1515

16+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
17+
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
18+
</ItemGroup>
19+
1620
<ItemGroup>
1721
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="10.0.3" />
1822
</ItemGroup>

src/DotNetToolkit.Repository/Configuration/Options/RepositoryOptionsBuilder.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public RepositoryOptionsBuilder([NotNull] IRepositoryOptions options)
6969

7070
#region Public Methods
7171

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

105105
return this;
106106
}
107-
#endif
108-
109-
#if NETSTANDARD
107+
#else
110108
/// <summary>
111109
/// Configures the repository options using the specified configuration.
112110
/// </summary>
113111
/// <param name="configuration">The configuration.</param>
114112
/// <returns>The same builder instance.</returns>
115113
/// <remarks>Any element that is defined in the config file can be resolved using the <see cref="RepositoryDependencyResolver"/>.</remarks>
116-
public virtual RepositoryOptionsBuilder UseConfiguration([NotNull] Microsoft.Extensions.Configuration.IConfigurationRoot configuration)
114+
public virtual RepositoryOptionsBuilder UseConfiguration([NotNull] Microsoft.Extensions.Configuration.IConfigurationRoot configuration)
117115
{
118116
Guard.NotNull(configuration, nameof(configuration));
119117

src/DotNetToolkit.Repository/DotNetToolkit.Repository.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
</ItemGroup>
3737

3838
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
39-
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
4039
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.2" />
4140
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
4241
</ItemGroup>

src/DotNetToolkit.Repository/Extensions/Internal/TypeExtensions.cs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ internal static class TypeExtensions
1818
/// <returns>The default value of the specified type.</returns>
1919
public static object GetDefault([NotNull] this Type type)
2020
{
21-
Guard.NotNull(type, nameof(type));
22-
23-
return type.GetTypeInfo().IsValueType ? Activator.CreateInstance(type) : null;
21+
return type == null ? null : (type.GetTypeInfo().IsValueType ? Activator.CreateInstance(type) : null);
2422
}
2523

2624
/// <summary>
@@ -30,9 +28,7 @@ public static object GetDefault([NotNull] this Type type)
3028
/// <returns><c>true</c> if the specified type is a <see cref="ICollection{T}"/>; otherwise, <c>false</c>.</returns>
3129
public static bool IsGenericCollection([NotNull] this Type type)
3230
{
33-
Guard.NotNull(type, nameof(type));
34-
35-
return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(ICollection<>);
31+
return type != null && type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(ICollection<>);
3632
}
3733

3834
/// <summary>
@@ -42,9 +38,7 @@ public static bool IsGenericCollection([NotNull] this Type type)
4238
/// <returns><c>true</c> if the specified type is nullable; otherwise, <c>false</c>.</returns>
4339
public static bool IsNullableType([NotNull] this Type type)
4440
{
45-
Guard.NotNull(type, nameof(type));
46-
47-
return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
41+
return type != null && type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
4842
}
4943

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

6054
/// <summary>
@@ -65,12 +59,12 @@ public static bool IsEnumerable([NotNull] this Type type)
6559
/// <returns><c>true</c> if specified type implements the specified interface type; otherwise, <c>false</c>.</returns>
6660
public static bool ImplementsInterface([NotNull] this Type type, [NotNull] Type interfaceType)
6761
{
68-
Guard.NotNull(type, nameof(type));
69-
Guard.NotNull(interfaceType, nameof(interfaceType));
70-
71-
return interfaceType.IsAssignableFrom(type) ||
72-
type.IsGenericType(interfaceType) ||
73-
type.GetTypeInfo().ImplementedInterfaces.Any(@interface => IsGenericType(@interface, interfaceType));
62+
return type != null && interfaceType != null &&
63+
(
64+
interfaceType.IsAssignableFrom(type) ||
65+
type.IsGenericType(interfaceType) ||
66+
type.GetTypeInfo().ImplementedInterfaces.Any(@interface => IsGenericType(@interface, interfaceType))
67+
);
7468
}
7569

7670
/// <summary>
@@ -81,10 +75,7 @@ public static bool ImplementsInterface([NotNull] this Type type, [NotNull] Type
8175
/// <returns><c>true</c> if the specified type is a generic type of the specified interface type; otherwise, <c>false</c>.</returns>
8276
public static bool IsGenericType([NotNull] this Type type, [NotNull] Type genericType)
8377
{
84-
Guard.NotNull(type, nameof(type));
85-
Guard.NotNull(genericType, nameof(genericType));
86-
87-
return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == genericType;
78+
return type != null && genericType != null && type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == genericType;
8879
}
8980

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

10092
object Result = null;
10193

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

172165
if (keyValues == null || keyValues.Count == 0)
173166
return Activator.CreateInstance(type);
@@ -220,20 +213,20 @@ select pi
220213
{
221214
// Try to get all the values for the parameters we already have,
222215
// and set the rest to their default value
223-
var args = matchedCtorParams.Value.Select(pi =>
224-
{
225-
// If we find a matching parameter, then delete it from the collection,
226-
// that way we don't try to initialize a property that has the same name
227-
if (kvs.ContainsKey(pi.Name))
228-
{
229-
kvs.TryGetValue(pi.Name, out var value);
230-
kvs.Remove(pi.Name);
231-
232-
return pi.ParameterType.ConvertTo(value);
233-
}
234-
235-
return pi.ParameterType.GetDefault();
236-
}).ToArray();
216+
var args = matchedCtorParams.Value.Select(pi =>
217+
{
218+
// If we find a matching parameter, then delete it from the collection,
219+
// that way we don't try to initialize a property that has the same name
220+
if (kvs.ContainsKey(pi.Name))
221+
{
222+
kvs.TryGetValue(pi.Name, out var value);
223+
kvs.Remove(pi.Name);
224+
225+
return pi.ParameterType.ConvertTo(value);
226+
}
227+
228+
return pi.ParameterType.GetDefault();
229+
}).ToArray();
237230

238231
obj = matchedCtorParams.Key.Invoke(args);
239232
}

src/DotNetToolkit.Repository/Internal/ConfigFile/ConfigurationSection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !NETSTANDARD1_3
1+
#if NETFULL
22

33
namespace DotNetToolkit.Repository.Internal.ConfigFile
44
{

src/DotNetToolkit.Repository/Internal/ConfigFile/Json/ConfigurationSection.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ internal class ConfigurationSection : ConfigFile.IConfigurationSection
2626
private const string DefaultContextFactorySectionKey = "defaultContextFactory";
2727
private const string LoggingProviderSectionKey = "loggingProvider";
2828
private const string CachingProviderSectionKey = "cachingProvider";
29-
private const string MappingProviderSectionKey = "mappingProvider";
3029
private const string InterceptorCollectionSectionKey = "interceptors";
3130
private const string ParameterCollectionSectionKey = "parameters";
3231
private const string TypeKey = "type";
32+
private const string ExpiryKey = "expiry";
3333

3434
private readonly IConfigurationSection _root;
3535

@@ -77,6 +77,12 @@ public ICacheProvider GetCachingProvider()
7777
if (section != null)
7878
{
7979
var value = GetTypedValue<ICacheProvider>(section);
80+
var expiry = ExtractExpiry(section);
81+
82+
if (expiry != null)
83+
{
84+
value.Expiry = expiry;
85+
}
8086

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

100-
interceptorsDict.Add(type, () => GetTypedValue<IRepositoryInterceptor>(section, type));
106+
interceptorsDict.Add(type, () => GetTypedValue<IRepositoryInterceptor>(subSection, type));
101107
}
102108
}
103109
}
@@ -138,6 +144,20 @@ private static Type ExtractType([NotNull] IConfigurationSection section, bool is
138144
return Type.GetType(value, throwOnError: true);
139145
}
140146

147+
private static TimeSpan? ExtractExpiry([NotNull] IConfigurationSection section)
148+
{
149+
Guard.NotNull(section, nameof(section));
150+
151+
var keyValues = ExtractParameters(section);
152+
153+
if (keyValues.ContainsKey(ExpiryKey))
154+
{
155+
return TimeSpan.Parse(keyValues[ExpiryKey]);
156+
}
157+
158+
return null;
159+
}
160+
141161
private static KeyValuePair<string, string> ExtractKeyValue([NotNull] IConfigurationSection section)
142162
{
143163
Guard.NotNull(section, nameof(section));

src/DotNetToolkit.Repository/Utility/Guard.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ internal static class Guard
1616
[ContractAnnotation("value:null => halt")]
1717
public static T NotNull<T>([ValidatedNotNull] [NoEnumeration] T value, [InvokerParameterName] string parameterName)
1818
{
19-
#if NETSTANDARD2_0
20-
if (typeof(T).IsNullableType() && value == null)
21-
#else
2219
if (ReferenceEquals(value, null))
23-
#endif
2420
{
2521
NotEmpty(parameterName, nameof(parameterName));
2622

@@ -74,11 +70,7 @@ public static ICollection<T> NotEmpty<T>([ValidatedNotNull] [NoEnumeration] ICol
7470
[ContractAnnotation("value:null => halt")]
7571
public static T EnsureNotNull<T>([ValidatedNotNull] [NoEnumeration] T value, string message) where T : class
7672
{
77-
#if NETSTANDARD2_0
78-
if (typeof(T).IsNullableType() && value == null)
79-
#else
8073
if (ReferenceEquals(value, null))
81-
#endif
8274
throw new InvalidOperationException(message);
8375

8476
return value;

0 commit comments

Comments
 (0)