Skip to content

Commit

Permalink
Added null argument checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdupont750 committed Dec 6, 2016
1 parent 9b788d3 commit d2cc4fe
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/Tact.Configuration/Extensions/ConfigurationExtensions.cs
Expand Up @@ -17,6 +17,9 @@ public static T Create<T>(this IConfiguration config)

public static object Create(this IConfiguration config, Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));

var value = Activator.CreateInstance(type);
Bind(config, type, value);
return value;
Expand All @@ -25,6 +28,9 @@ public static object Create(this IConfiguration config, Type type)
public static T CreateAndValidate<T>(this IConfiguration config)
where T : new()
{
if (config == null)
throw new ArgumentNullException(nameof(config));

var type = typeof(T);
var value = new T();
BindAndValidate(config, type, value);
Expand All @@ -33,6 +39,9 @@ public static T CreateAndValidate<T>(this IConfiguration config)

public static object CreateAndValidate(this IConfiguration config, Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));

var value = Activator.CreateInstance(type);
BindAndValidate(config, type, value);
return value;
Expand All @@ -46,13 +55,19 @@ public static void BindAndValidate<T>(IConfiguration config, object value)

public static void BindAndValidate(IConfiguration config, Type type, object value)
{
if (type == null)
throw new ArgumentNullException(nameof(type));

Bind(config, type, value);
var context = new ValidationContext(value);
context.ValidateObject();
}

private static void Bind(IConfiguration config, Type type, object value)
{
if (config == null)
throw new ArgumentNullException(nameof(config));

config.GetSection(type.Name).Bind(value);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Tact.Configuration/Extensions/ContainerExtensions.cs
Expand Up @@ -33,6 +33,9 @@ public static void ConfigureByAttribute<T>(this IContainer container, IConfigura
public static void ConfigureByAttribute<T>(this IContainer container, IConfiguration configuration, params Type[] types)
where T : IRegisterConfigurationAttribute
{
if(container == null)
throw new ArgumentNullException(nameof(container));

ILog logger;
container.TryResolve(out logger);

Expand Down
Expand Up @@ -17,6 +17,9 @@ public static class ValidationContextExtensions

public static void ValidateObject(this ValidationContext context, bool validateAllProperties = true)
{
if (context == null)
throw new ArgumentNullException(nameof(context));

var instance = context.ObjectInstance;
var type = instance.GetType();

Expand Down
6 changes: 6 additions & 0 deletions src/Tact/Extensions/CollectionExtensions.cs
Expand Up @@ -38,6 +38,12 @@ public static class CollectionExtensions
Func<TInput, int, CancellationToken, Task<TOutput>> func,
int? maxParallelization = null)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));

if (func == null)
throw new ArgumentNullException(nameof(func));

var results = new TOutput[collection.Count];

await collection
Expand Down
51 changes: 51 additions & 0 deletions src/Tact/Extensions/ContainerExtensions.cs
Expand Up @@ -36,6 +36,9 @@ public static void InitializeByAttribute<T>(this IContainer container, params As
public static void InitializeByAttribute<T>(this IContainer container, params Type[] types)
where T : IInitializeAttribute
{
if (container == null)
throw new ArgumentNullException(nameof(container));

ILog logger;
container.TryResolve(out logger);

Expand Down Expand Up @@ -96,6 +99,9 @@ public static void RegisterByAttribute<T>(this IContainer container, params Type
where TRegister : IRegisterAttribute
where TCondition : IRegisterConditionAttribute
{
if (container == null)
throw new ArgumentNullException(nameof(container));

ILog logger;
container.TryResolve(out logger);

Expand Down Expand Up @@ -141,6 +147,9 @@ public static void RegisterPerResolve<T>(this IContainer container, Func<IResolv

public static void RegisterPerResolve(this IContainer container, Type type, Func<IResolver, object> factory = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

if (factory == null)
{
container.RegisterPerResolve(type, type);
Expand All @@ -160,6 +169,9 @@ public static void RegisterPerResolve<T>(this IContainer container, string key,

public static void RegisterPerResolve(this IContainer container, Type type, string key, Func<IResolver, object> factory = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

if (factory == null)
{
container.RegisterPerResolve(type, type, key);
Expand All @@ -180,6 +192,9 @@ public static void RegisterPerResolve(this IContainer container, Type type, stri

public static void RegisterPerResolve(this IContainer container, Type fromType, Type toType, string key = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

toType.EnsureSingleCostructor();
var lifetimeManager = new PerResolveLifetimeManager(toType, container);

Expand All @@ -202,6 +217,9 @@ public static void RegisterPerScope<T>(this IContainer container, Func<IResolver

public static void RegisterPerScope(this IContainer container, Type type, Func<IResolver, object> factory = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

if (factory == null)
{
container.RegisterPerScope(type, type);
Expand All @@ -221,6 +239,9 @@ public static void RegisterPerScope<T>(this IContainer container, string key, Fu

public static void RegisterPerScope(this IContainer container, Type type, string key, Func<IResolver, object> factory = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

if (factory == null)
{
container.RegisterPerScope(type, type, key);
Expand All @@ -241,6 +262,9 @@ public static void RegisterPerScope(this IContainer container, Type type, string

public static void RegisterPerScope(this IContainer container, Type fromType, Type toType, string key = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

toType.EnsureSingleCostructor();
var lifetimeManager = new PerScopeLifetimeManager(toType, container);

Expand All @@ -256,6 +280,9 @@ public static void RegisterPerScope(this IContainer container, Type fromType, Ty

public static void RegisterInstance<T>(this IContainer container, T value, string key = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

var type = typeof(T);

if (string.IsNullOrWhiteSpace(key))
Expand All @@ -266,6 +293,9 @@ public static void RegisterInstance<T>(this IContainer container, T value, strin

public static void RegisterInstance(this IContainer container, Type type, object value, string key = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

var lifetimeManager = new InstanceLifetimeManager(value, container);

if (string.IsNullOrWhiteSpace(key))
Expand All @@ -287,6 +317,9 @@ public static void RegisterSingleton<T>(this IContainer container, Func<IResolve

public static void RegisterSingleton(this IContainer container, Type type, Func<IResolver, object> factory = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

if (factory == null)
{
container.RegisterSingleton(type, type);
Expand All @@ -306,6 +339,9 @@ public static void RegisterSingleton<T>(this IContainer container, string key, F

public static void RegisterSingleton(this IContainer container, Type type, string key, Func<IResolver, object> factory = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

if (factory == null)
{
container.RegisterSingleton(type, type, key);
Expand All @@ -326,6 +362,9 @@ public static void RegisterSingleton(this IContainer container, Type type, strin

public static void RegisterSingleton(this IContainer container, Type fromType, Type toType, string key = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

toType.EnsureSingleCostructor();
var lifetimeManager = new SingletonLifetimeManager(toType, container);

Expand All @@ -348,6 +387,9 @@ public static void RegisterTransient<T>(this IContainer container, Func<IResolve

public static void RegisterTransient(this IContainer container, Type type, Func<IResolver, object> factory = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

if (factory == null)
{
container.RegisterTransient(type, type);
Expand All @@ -367,6 +409,9 @@ public static void RegisterTransient<T>(this IContainer container, string key, F

public static void RegisterTransient(this IContainer container, Type type, string key, Func<IResolver, object> factory = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

if (factory == null)
{
container.RegisterTransient(type, type, key);
Expand All @@ -387,6 +432,9 @@ public static void RegisterTransient(this IContainer container, Type type, strin

public static void RegisterTransient(this IContainer container, Type fromType, Type toType, string key = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

toType.EnsureSingleCostructor();
var lifetimeManager = new TransientLifetimeManager(toType, container);

Expand All @@ -409,6 +457,9 @@ public static void RegisterTransient(this IContainer container, Type fromType, T

public static void RegisterProxy(this IContainer container, Type fromType, Type toType, string fromKey = null, string toKey = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));

var lifetimeManager = new ProxyLifetimeManager(toType, fromKey, container);

if (string.IsNullOrWhiteSpace(toKey))
Expand Down
6 changes: 6 additions & 0 deletions src/Tact/Extensions/EnumerableExtensions.cs
Expand Up @@ -39,6 +39,12 @@ public static class EnumerableExtensions
Func<T, int, CancellationToken, Task> func,
int? maxParallelization = null)
{
if (enumerable == null)
throw new ArgumentNullException(nameof(enumerable));

if (func == null)
throw new ArgumentNullException(nameof(func));

var exceptions = new ConcurrentQueue<Exception>();
var maxCount = maxParallelization ?? Environment.ProcessorCount;
var tasks = new List<Task>(maxCount);
Expand Down
20 changes: 18 additions & 2 deletions src/Tact/Extensions/ReaderWriterLockSlimExtensions.cs
Expand Up @@ -12,13 +12,21 @@ public static IDisposable UseReadLock(this ReaderWriterLockSlim lockSlim, int mi

public static IDisposable UseReadLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout)
{
if (lockSlim == null)
throw new ArgumentNullException(nameof(lockSlim));

var result = lockSlim.TryEnterReadLock(timeout);
if (!result) throw new TimeoutException("Unable to obtain a read lock");
if (!result)
throw new TimeoutException("Unable to obtain a read lock");

return new ReaderWriterLockSlimWrapper(lockSlim, false);
}

public static IDisposable UseReadLock(this ReaderWriterLockSlim lockSlim)
{
if (lockSlim == null)
throw new ArgumentNullException(nameof(lockSlim));

lockSlim.EnterReadLock();
return new ReaderWriterLockSlimWrapper(lockSlim, false);
}
Expand All @@ -30,13 +38,21 @@ public static IDisposable UseWriteLock(this ReaderWriterLockSlim lockSlim, int m

public static IDisposable UseWriteLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout)
{
if (lockSlim == null)
throw new ArgumentNullException(nameof(lockSlim));

var result = lockSlim.TryEnterWriteLock(timeout);
if (!result) throw new TimeoutException("Unable to obtain a write lock");
if (!result)
throw new TimeoutException("Unable to obtain a write lock");

return new ReaderWriterLockSlimWrapper(lockSlim, true);
}

public static IDisposable UseWriteLock(this ReaderWriterLockSlim lockSlim)
{
if (lockSlim == null)
throw new ArgumentNullException(nameof(lockSlim));

lockSlim.EnterWriteLock();
return new ReaderWriterLockSlimWrapper(lockSlim, true);
}
Expand Down
18 changes: 17 additions & 1 deletion src/Tact/Extensions/ResolverExtensions.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Tact.Practices;

Expand All @@ -8,12 +9,18 @@ public static class ResolverExtensions
{
public static T Resolve<T>(this IResolver resolver)
{
if (resolver == null)
throw new ArgumentNullException(nameof(resolver));

var type = typeof(T);
return (T) resolver.Resolve(type);
}

public static bool TryResolve<T>(this IResolver resolver, out T result)
{
if (resolver == null)
throw new ArgumentNullException(nameof(resolver));

var type = typeof(T);
object objResult;
if (resolver.TryResolve(type, out objResult))
Expand All @@ -28,12 +35,18 @@ public static bool TryResolve<T>(this IResolver resolver, out T result)

public static T Resolve<T>(this IResolver resolver, string key)
{
if (resolver == null)
throw new ArgumentNullException(nameof(resolver));

var type = typeof(T);
return (T) resolver.Resolve(type, key);
}

public static bool TryResolve<T>(this IResolver resolver, string key, out T result)
{
if (resolver == null)
throw new ArgumentNullException(nameof(resolver));

var type = typeof(T);
object objResult;
if (resolver.TryResolve(type, key, out objResult))
Expand All @@ -48,6 +61,9 @@ public static bool TryResolve<T>(this IResolver resolver, string key, out T resu

public static IEnumerable<T> ResolveAll<T>(this IResolver resolver)
{
if (resolver == null)
throw new ArgumentNullException(nameof(resolver));

var type = typeof(T);
return resolver.ResolveAll(type).Cast<T>();
}
Expand Down
11 changes: 10 additions & 1 deletion src/Tact/Extensions/SemaphoreSlimExtensions.cs
Expand Up @@ -13,14 +13,23 @@ public static Task<IDisposable> UseAsync(this SemaphoreSlim semaphore, int milli

public static async Task<IDisposable> UseAsync(this SemaphoreSlim semaphore, TimeSpan timeout, CancellationToken cancelToken = default(CancellationToken))
{
if (semaphore == null)
throw new ArgumentNullException(nameof(semaphore));

var result = await semaphore.WaitAsync(timeout, cancelToken).ConfigureAwait(false);
if (!result) throw new TimeoutException("Unable to obtain a lock");
if (!result)
throw new TimeoutException("Unable to obtain a lock");

return new SemaphoreSlimWrapper(semaphore);
}

public static async Task<IDisposable> UseAsync(this SemaphoreSlim semaphore, CancellationToken cancelToken = default(CancellationToken))
{
if (semaphore == null)
throw new ArgumentNullException(nameof(semaphore));

await semaphore.WaitAsync(cancelToken).ConfigureAwait(false);

return new SemaphoreSlimWrapper(semaphore);
}

Expand Down

0 comments on commit d2cc4fe

Please sign in to comment.