Skip to content

Instantly share code, notes, and snippets.

@micdenny
Created May 9, 2016 10:57
Show Gist options
  • Save micdenny/1154fe02b4c28faa5b4904aa7bc2e6c2 to your computer and use it in GitHub Desktop.
Save micdenny/1154fe02b4c28faa5b4904aa7bc2e6c2 to your computer and use it in GitHub Desktop.
EasyNetQ Avoid Static Factory
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using EasyNetQ;
using EasyNetQ.ConnectionString;
using EasyNetQ.Consumer;
using EasyNetQ.Interception;
using EasyNetQ.Loggers;
using EasyNetQ.Producer;
using EasyNetQ.Scheduling;
namespace EasyNetQAvoidStaticFactory
{
class Program
{
static void Main(string[] args)
{
var app = new Application();
app.Run();
}
}
public class Application
{
public void Run()
{
//var bus = RabbitHutch.CreateBus("host=localhost", x => x.Register<IEasyNetQLogger>(s => new ConsoleLogger()));
//var bus = CreateBus("host=localhost");
var bus = new RabbitHutch().CreateBus("host=localhost", x => x.Register<IEasyNetQLogger>(s => new ConsoleLogger()));
Console.ReadLine();
bus.Dispose();
}
// this is probably the smallest method you can create to instantiate an IBus
// otherwise you can copy and paste the RabbitHutch+Preconditions
// removing all the static keyword as follow
private IBus CreateBus(string connectionString)
{
var container = new DefaultServiceProvider();
container
.Register<IContainer>(_ => container)
.Register<IEasyNetQLogger, ConsoleLogger>() // <---- use NullLogger or custom loger if you want to suppress the console logger
.Register<ISerializer, JsonSerializer>()
.Register<IConventions, Conventions>()
.Register<IEventBus, EventBus>()
.Register<ITypeNameSerializer, TypeNameSerializer>()
.Register<ICorrelationIdGenerationStrategy, DefaultCorrelationIdGenerationStrategy>()
.Register<IMessageSerializationStrategy, DefaultMessageSerializationStrategy>()
.Register<IMessageDeliveryModeStrategy, MessageDeliveryModeStrategy>()
.Register<ITimeoutStrategy, TimeoutStrategy>()
.Register<IClusterHostSelectionStrategy<ConnectionFactoryInfo>, RandomClusterHostSelectionStrategy<ConnectionFactoryInfo>>()
.Register<IProduceConsumeInterceptor, DefaultInterceptor>()
.Register<IConsumerDispatcherFactory, ConsumerDispatcherFactory>()
.Register<IPublishExchangeDeclareStrategy, PublishExchangeDeclareStrategy>()
.Register<IConsumerErrorStrategy, DefaultConsumerErrorStrategy>()
.Register<IErrorMessageSerializer, DefaultErrorMessageSerializer>()
.Register<IHandlerRunner, HandlerRunner>()
.Register<IInternalConsumerFactory, InternalConsumerFactory>()
.Register<IConsumerFactory, ConsumerFactory>()
.Register<IConnectionFactory, ConnectionFactoryWrapper>()
.Register<IPersistentChannelFactory, PersistentChannelFactory>()
.Register<IClientCommandDispatcherFactory, ClientCommandDispatcherFactory>()
.Register<IPublishConfirmationListener, PublishConfirmationListener>()
.Register<IHandlerCollectionFactory, HandlerCollectionFactory>()
.Register<IAdvancedBus, RabbitAdvancedBus>()
.Register<IRpc, Rpc>()
.Register<ISendReceive, SendReceive>()
.Register<IScheduler, ExternalScheduler>()
.Register<IBus, RabbitBus>();
var connectionStringParser = new ConnectionStringParser();
var connectionConfiguration = connectionStringParser.Parse(connectionString);
connectionConfiguration.Validate();
container.Register(_ => connectionConfiguration);
container.Register(_ => new AdvancedBusEventHandlers());
return container.Resolve<IBus>();
}
}
/// <summary>
/// Static methods to create EasyNetQ core APIs.
/// </summary>
public class RabbitHutch
{
private Func<IContainer> createContainerInternal = () => new DefaultServiceProvider();
private Preconditions Preconditions = new Preconditions();
/// <summary>
/// Set the container creation function. This allows you to replace EasyNetQ's default internal
/// IoC container. Note that all components should be registered as singletons. EasyNetQ will
/// also call Dispose on components that are no longer required.
/// </summary>
public void SetContainerFactory(Func<IContainer> createContainer)
{
Preconditions.CheckNotNull(createContainer, "createContainer");
createContainerInternal = createContainer;
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// The RabbitMQ broker is defined in the connection string named 'rabbit'.
/// </summary>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus()
{
return CreateBus(c => { });
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// The RabbitMQ broker is defined in the connection string named 'rabbit'.
/// </summary>
/// <param name="registerServices">
/// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
/// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(Action<IServiceRegister> registerServices)
{
return CreateBus(AdvancedBusEventHandlers.Default, registerServices);
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// The RabbitMQ broker is defined in the connection string named 'rabbit'.
/// </summary>
/// <param name="advancedBusEventHandlers">
/// An <see cref="AdvancedBusEventHandlers"/> instance which is used to add handlers
/// to the events of the newly created <see cref="IBus.Advanced"/>.
/// As <see cref="RabbitAdvancedBus"/> attempts to connect during instantiation, specifying a <see cref="AdvancedBusEventHandlers"/>
/// before instantiation is the only way to catch the first <see cref="AdvancedBusEventHandlers.Connected"/> event.
/// </param>
/// <param name="registerServices">
/// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
/// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(AdvancedBusEventHandlers advancedBusEventHandlers, Action<IServiceRegister> registerServices)
{
var rabbitConnectionString = ConfigurationManager.ConnectionStrings["rabbit"];
if (rabbitConnectionString == null)
{
throw new EasyNetQException(
"Could not find a connection string for RabbitMQ. " +
"Please add a connection string in the <ConnectionStrings> section" +
"of the application's configuration file. For example: " +
"<add name=\"rabbit\" connectionString=\"host=localhost\" />");
}
return CreateBus(rabbitConnectionString.ConnectionString, advancedBusEventHandlers, registerServices);
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// The RabbitMQ broker is defined in the connection string named 'rabbit'.
/// </summary>
/// <param name="advancedBusEventHandlers">
/// An <see cref="AdvancedBusEventHandlers"/> instance which is used to add handlers
/// to the events of the newly created <see cref="IBus.Advanced"/>.
/// As <see cref="RabbitAdvancedBus"/> attempts to connect during instantiation, specifying a <see cref="AdvancedBusEventHandlers"/>
/// before instantiation is the only way to catch the first <see cref="AdvancedBusEventHandlers.Connected"/> event.
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(AdvancedBusEventHandlers advancedBusEventHandlers)
{
return CreateBus(advancedBusEventHandlers, c => { });
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// </summary>
/// <param name="connectionString">
/// The EasyNetQ connection string. Example:
/// host=192.168.1.1;port=5672;virtualHost=MyVirtualHost;username=MyUsername;password=MyPassword;requestedHeartbeat=10
///
/// The following default values will be used if not specified:
/// host=localhost;port=5672;virtualHost=/;username=guest;password=guest;requestedHeartbeat=10
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(string connectionString)
{
Preconditions.CheckNotNull(connectionString, "connectionString");
return CreateBus(connectionString, AdvancedBusEventHandlers.Default);
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// </summary>
/// <param name="connectionString">
/// The EasyNetQ connection string. Example:
/// host=192.168.1.1;port=5672;virtualHost=MyVirtualHost;username=MyUsername;password=MyPassword;requestedHeartbeat=10
///
/// The following default values will be used if not specified:
/// host=localhost;port=5672;virtualHost=/;username=guest;password=guest;requestedHeartbeat=10
/// </param>
/// <param name="advancedBusEventHandlers">
/// An <see cref="AdvancedBusEventHandlers"/> instance which is used to add handlers
/// to the events of the newly created <see cref="IBus.Advanced"/>.
/// As <see cref="RabbitAdvancedBus"/> attempts to connect during instantiation, specifying a <see cref="AdvancedBusEventHandlers"/>
/// before instantiation is the only way to catch the first <see cref="AdvancedBusEventHandlers.Connected"/> event.
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(string connectionString, AdvancedBusEventHandlers advancedBusEventHandlers)
{
Preconditions.CheckNotNull(connectionString, "connectionString");
Preconditions.CheckNotNull(advancedBusEventHandlers, "advancedBusEventHandlers");
return CreateBus(connectionString, advancedBusEventHandlers, x => { });
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// </summary>
/// <param name="connectionString">
/// The EasyNetQ connection string. Example:
/// host=192.168.1.1;port=5672;virtualHost=MyVirtualHost;username=MyUsername;password=MyPassword;requestedHeartbeat=10
///
/// The following default values will be used if not specified:
/// host=localhost;port=5672;virtualHost=/;username=guest;password=guest;requestedHeartbeat=10
/// </param>
/// <param name="registerServices">
/// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
/// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(string connectionString, Action<IServiceRegister> registerServices)
{
Preconditions.CheckNotNull(connectionString, "connectionString");
Preconditions.CheckNotNull(registerServices, "registerServices");
return CreateBus(connectionString, AdvancedBusEventHandlers.Default, registerServices);
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// </summary>
/// <param name="connectionString">
/// The EasyNetQ connection string. Example:
/// host=192.168.1.1;port=5672;virtualHost=MyVirtualHost;username=MyUsername;password=MyPassword;requestedHeartbeat=10
///
/// The following default values will be used if not specified:
/// host=localhost;port=5672;virtualHost=/;username=guest;password=guest;requestedHeartbeat=10
/// </param>
/// <param name="advancedBusEventHandlers">
/// An <see cref="AdvancedBusEventHandlers"/> instance which is used to add handlers
/// to the events of the newly created <see cref="IBus.Advanced"/>.
/// As <see cref="RabbitAdvancedBus"/> attempts to connect during instantiation, specifying a <see cref="AdvancedBusEventHandlers"/>
/// before instantiation is the only way to catch the first <see cref="AdvancedBusEventHandlers.Connected"/> event.
/// </param>
/// <param name="registerServices">
/// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
/// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(string connectionString, AdvancedBusEventHandlers advancedBusEventHandlers, Action<IServiceRegister> registerServices)
{
Preconditions.CheckNotNull(connectionString, "connectionString");
Preconditions.CheckNotNull(registerServices, "registerServices");
Preconditions.CheckNotNull(advancedBusEventHandlers, "advancedBusEventHandlers");
var connectionStringParser = new ConnectionStringParser();
var connectionConfiguration = connectionStringParser.Parse(connectionString);
return CreateBus(connectionConfiguration, advancedBusEventHandlers, registerServices);
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// </summary>
/// <param name="hostName">
/// The RabbitMQ broker.
/// </param>
/// <param name="hostPort">
/// The RabbitMQ broker port.
/// </param>
/// <param name="virtualHost">
/// The RabbitMQ virtualHost.
/// </param>
/// <param name="username">
/// The username to use to connect to the RabbitMQ broker.
/// </param>
/// <param name="password">
/// The password to use to connect to the RabbitMQ broker.
/// </param>
/// <param name="requestedHeartbeat">
/// The initially requested heartbeat interval, in seconds; zero for none.
/// </param>
/// <param name="registerServices">
/// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
/// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(
string hostName,
ushort hostPort,
string virtualHost,
string username,
string password,
ushort requestedHeartbeat,
Action<IServiceRegister> registerServices)
{
Preconditions.CheckNotNull(hostName, "hostName");
Preconditions.CheckNotNull(virtualHost, "virtualHost");
Preconditions.CheckNotNull(username, "username");
Preconditions.CheckNotNull(password, "password");
Preconditions.CheckNotNull(registerServices, "registerServices");
return CreateBus(hostName, hostPort, virtualHost, username, password, requestedHeartbeat, AdvancedBusEventHandlers.Default, registerServices);
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// </summary>
/// <param name="hostName">
/// The RabbitMQ broker.
/// </param>
/// <param name="hostPort">
/// The RabbitMQ broker port.
/// </param>
/// <param name="virtualHost">
/// The RabbitMQ virtualHost.
/// </param>
/// <param name="username">
/// The username to use to connect to the RabbitMQ broker.
/// </param>
/// <param name="password">
/// The password to use to connect to the RabbitMQ broker.
/// </param>
/// <param name="requestedHeartbeat">
/// The initially requested heartbeat interval, in seconds; zero for none.
/// </param>
/// <param name="advancedBusEventHandlers">
/// An <see cref="AdvancedBusEventHandlers"/> instance which is used to add handlers
/// to the events of the newly created <see cref="IBus.Advanced"/>.
/// As <see cref="RabbitAdvancedBus"/> attempts to connect during instantiation, specifying a <see cref="AdvancedBusEventHandlers"/>
/// before instantiation is the only way to catch the first <see cref="AdvancedBusEventHandlers.Connected"/> event.
/// </param>
/// <param name="registerServices">
/// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
/// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(
string hostName,
ushort hostPort,
string virtualHost,
string username,
string password,
ushort requestedHeartbeat,
AdvancedBusEventHandlers advancedBusEventHandlers,
Action<IServiceRegister> registerServices)
{
Preconditions.CheckNotNull(hostName, "hostName");
Preconditions.CheckNotNull(virtualHost, "virtualHost");
Preconditions.CheckNotNull(username, "username");
Preconditions.CheckNotNull(password, "password");
Preconditions.CheckNotNull(advancedBusEventHandlers, "advancedBusEventHandlers");
Preconditions.CheckNotNull(registerServices, "registerServices");
var connectionConfiguration = new ConnectionConfiguration
{
Hosts = new List<HostConfiguration>
{
new HostConfiguration { Host = hostName, Port = hostPort }
},
Port = hostPort,
VirtualHost = virtualHost,
UserName = username,
Password = password,
RequestedHeartbeat = requestedHeartbeat
};
return CreateBus(connectionConfiguration, advancedBusEventHandlers, registerServices);
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// </summary>
/// <param name="connectionConfiguration">
/// An <see cref="ConnectionConfiguration"/> instance.
/// </param>
/// <param name="registerServices">
/// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
/// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(ConnectionConfiguration connectionConfiguration, Action<IServiceRegister> registerServices)
{
Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
Preconditions.CheckNotNull(registerServices, "registerServices");
return CreateBus(connectionConfiguration, AdvancedBusEventHandlers.Default, registerServices);
}
/// <summary>
/// Creates a new instance of <see cref="RabbitBus"/>.
/// </summary>
/// <param name="connectionConfiguration">
/// An <see cref="ConnectionConfiguration"/> instance.
/// </param>
/// <param name="advancedBusEventHandlers">
/// An <see cref="AdvancedBusEventHandlers"/> instance which is used to add handlers
/// to the events of the newly created <see cref="IBus.Advanced"/>.
/// As <see cref="RabbitAdvancedBus"/> attempts to connect during instantiation, specifying a <see cref="AdvancedBusEventHandlers"/>
/// before instantiation is the only way to catch the first <see cref="AdvancedBusEventHandlers.Connected"/> event.
/// </param>
/// <param name="registerServices">
/// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
/// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
/// </param>
/// <returns>
/// A new <see cref="RabbitBus"/> instance.
/// </returns>
public IBus CreateBus(ConnectionConfiguration connectionConfiguration, AdvancedBusEventHandlers advancedBusEventHandlers, Action<IServiceRegister> registerServices)
{
Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
Preconditions.CheckNotNull(advancedBusEventHandlers, "advancedBusEventHandlers");
Preconditions.CheckNotNull(registerServices, "registerServices");
var container = createContainerInternal();
if (container == null)
{
throw new EasyNetQException("Could not create container. " +
"Have you called SetContainerFactory(...) with a function that returns null?");
}
connectionConfiguration.Validate();
container.Register(_ => connectionConfiguration);
container.Register(_ => advancedBusEventHandlers);
registerServices(container);
ComponentRegistration.RegisterServices(container);
return container.Resolve<IBus>();
}
}
/// <summary>
/// Collection of precondition methods for qualifying method arguments.
/// </summary>
internal class Preconditions
{
/// <summary>
/// Ensures that <paramref name="value"/> is not null.
/// </summary>
/// <param name="value">
/// The value to check, must not be null.
/// </param>
/// <param name="name">
/// The name of the parameter the value is taken from, must not be
/// blank.
/// </param>
/// <exception cref="ArgumentNullException">
/// Thrown if <paramref name="value"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="name"/> is blank.
/// </exception>
public void CheckNotNull<T>(T value, string name) where T : class
{
CheckNotNull(value, name, string.Format("{0} must not be null", name));
}
/// <summary>
/// Ensures that <paramref name="value"/> is not null.
/// </summary>
/// <param name="value">
/// The value to check, must not be null.
/// </param>
/// <param name="name">
/// The name of the parameter the value is taken from, must not be
/// blank.
/// </param>
/// <param name="message">
/// The message to provide to the exception if <paramref name="value"/>
/// is null, must not be blank.
/// </param>
/// <exception cref="ArgumentNullException">
/// Thrown if <paramref name="value"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="name"/> or <paramref name="message"/> are
/// blank.
/// </exception>
public void CheckNotNull<T>(T value, string name, string message) where T : class
{
if (value == null)
{
CheckNotBlank(name, "name", "name must not be blank");
CheckNotBlank(message, "message", "message must not be blank");
throw new ArgumentNullException(name, message);
}
}
/// <summary>
/// Ensures that <paramref name="value"/> is not blank.
/// </summary>
/// <param name="value">
/// The value to check, must not be blank.
/// </param>
/// <param name="name">
/// The name of the parameter the value is taken from, must not be
/// blank.
/// </param>
/// <param name="message">
/// The message to provide to the exception if <paramref name="value"/>
/// is blank, must not be blank.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="value"/>, <paramref name="name"/>, or
/// <paramref name="message"/> are blank.
/// </exception>
public void CheckNotBlank(string value, string name, string message)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("name must not be blank", "name");
}
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentException("message must not be blank", "message");
}
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException(message, name);
}
}
/// <summary>
/// Ensures that <paramref name="value"/> is not blank.
/// </summary>
/// <param name="value">
/// The value to check, must not be blank.
/// </param>
/// <param name="name">
/// The name of the parameter the value is taken from, must not be
/// blank.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="value"/> or <paramref name="name"/> are
/// blank.
/// </exception>
public void CheckNotBlank(string value, string name)
{
CheckNotBlank(value, name, string.Format("{0} must not be blank", name));
}
/// <summary>
/// Ensures that <paramref name="collection"/> contains at least one
/// item.
/// </summary>
/// <param name="collection">
/// The collection to check, must not be null or empty.
/// </param>
/// <param name="name">
/// The name of the parameter the collection is taken from, must not be
/// blank.
/// </param>
/// <param name="message">
/// The message to provide to the exception if <paramref name="collection"/>
/// is empty, must not be blank.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="collection"/> is empty, or if
/// <paramref name="value"/> or <paramref name="name"/> are blank.
/// </exception>
public void CheckAny<T>(IEnumerable<T> collection, string name, string message)
{
if (collection == null || !collection.Any())
{
CheckNotBlank(name, "name", "name must not be blank");
CheckNotBlank(message, "message", "message must not be blank");
throw new ArgumentException(message, name);
}
}
/// <summary>
/// Ensures that <paramref name="value"/> is true.
/// </summary>
/// <param name="value">
/// The value to check, must be true.
/// </param>
/// <param name="name">
/// The name of the parameter the value is taken from, must not be
/// blank.
/// </param>
/// <param name="message">
/// The message to provide to the exception if <paramref name="collection"/>
/// is false, must not be blank.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="value"/> is false, or if <paramref name="name"/>
/// or <paramref name="message"/> are blank.
/// </exception>
public void CheckTrue(bool value, string name, string message)
{
if (!value)
{
CheckNotBlank(name, "name", "name must not be blank");
CheckNotBlank(message, "message", "message must not be blank");
throw new ArgumentException(message, name);
}
}
/// <summary>
/// Ensures that <paramref name="value"/> is false.
/// </summary>
/// <param name="value">
/// The value to check, must be false.
/// </param>
/// <param name="name">
/// The name of the parameter the value is taken from, must not be
/// blank.
/// </param>
/// <param name="message">
/// The message to provide to the exception if <paramref name="collection"/>
/// is true, must not be blank.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="value"/> is true, or if <paramref name="name"/>
/// or <paramref name="message"/> are blank.
/// </exception>
public void CheckFalse(bool value, string name, string message)
{
if (value)
{
CheckNotBlank(name, "name", "name must not be blank");
CheckNotBlank(message, "message", "message must not be blank");
throw new ArgumentException(message, name);
}
}
public void CheckShortString(string value, string name)
{
CheckNotNull(value, name);
if (value.Length > 255)
{
throw new ArgumentException(string.Format("Argument '{0}' must be less than or equal to 255 characters.", name));
}
}
public void CheckTypeMatches(Type expectedType, object value, string name, string message)
{
if (!expectedType.IsAssignableFrom(value.GetType()))
{
CheckNotBlank(name, "name", "name must not be blank");
CheckNotBlank(message, "message", "message must not be blank");
throw new ArgumentException(message, name);
}
}
public void CheckLess(TimeSpan value, TimeSpan maxValue, string name)
{
if (value < maxValue)
return;
throw new ArgumentOutOfRangeException(name, string.Format("Arguments {0} must be less than maxValue", name));
}
public void CheckNull<T>(T value, string name)
{
if (value == null)
return;
throw new ArgumentException(string.Format("{0} must not be null", name), name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment