我目前正在减少我的应用程序框架中的一些重复项,我想知道人们对以下扩展方法有何看法?
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ThrowExtensions
{
public static T ThrowIfNull<T>(this T instance) where T : class
{
Contract.Ensures(Contract.Result<T>() != null);
if (instance == null)
{
throw new ArgumentNullException("instance", string.Format("Object reference of type '{0}' not set to an instance of an object.", typeof(T).FullName));
}
return instance;
}
}
考虑以下示例。
我知道扩展一切都不是一个好习惯,如果可能的话我应该避免它,但在这种情况下,它似乎很合理,虽然不漂亮。
protected bool Contains<TEntity>(TEntity entity) where TEntity : class
{
Contract.Requires(entity != null);
ObjectStateEntry entry;
bool exist = ((IObjectContextAdapter)_context).ObjectContext.ThrowIfNull().ObjectStateManager.ThrowIfNull().TryGetObjectStateEntry(entity, out entry);
return exist;
}
编辑:我也在考虑用 Try() 更改 ThrowIfNull 或更适合此上下文的东西作为框架中的约定,但我真的很喜欢对此的看法,如果你有更好的选择,我很高兴听到它,谢谢!
更新:到目前为止,这就是我最终的结果。
namespace EasyFront.Framework.Diagnostics.Contracts
{
using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ContractsExtensions
{
/// <summary>
/// Calls the code wrapped by the delegate when the subject is not pointing to null, otherwise, <see
/// cref="ArgumentNullException" /> is thrown.
/// </summary>
/// <remarks>
/// Eyal Shilony, 01/08/2012.
/// </remarks>
/// <typeparam name="TSubject"> The type of the subject to operate on. </typeparam>
/// <typeparam name="TResult"> The type of the result to return from the call. </typeparam>
/// <param name="subject"> The subject to operate on. </param>
/// <param name="func"> The function that should be invoked when the subject is not pointing to null. </param>
/// <returns> The result of the invoked function. </returns>
public static TResult SafeCall<TSubject, TResult>(this TSubject subject, Func<TSubject, TResult> func)
where TSubject : class
{
Contract.Requires(func != null);
if (subject == null)
{
ThrowArgumentNull<TSubject>();
}
return func(subject);
}
/// <summary>
/// Calls the code wrapped by the delegate when the subject is not pointing to null,
/// otherwise, <see cref="ArgumentNullException" /> is thrown.
/// </summary>
/// <remarks>
/// Eyal Shilony, 01/08/2012.
/// </remarks>
/// <typeparam name="TSubject"> The type of the subject to operate on. </typeparam>
/// <param name="subject"> The subject to operate on. </param>
/// <param name="func"> The function that should be invoked when the subject is not pointing to null. </param>
public static void SafeCall<TSubject>(this TSubject subject, Action<TSubject> func)
where TSubject : class
{
Contract.Requires(func != null);
if (subject == null)
{
ThrowArgumentNull<TSubject>();
}
func(subject);
}
/// <summary>
/// Ensures that the subject is not pointing to null and returns it, otherwise, <see cref="ArgumentNullException" /> is thrown.
/// </summary>
/// <remarks>
/// Eyal Shilony, 01/08/2012.
/// </remarks>
/// <typeparam name="TSubject"> The type of the subject to operate on. </typeparam>
/// <param name="subject"> The subject to operate on. </param>
/// <returns> The subject. </returns>
public static TSubject SafeReturn<TSubject>(this TSubject subject)
where TSubject : class
{
Contract.Ensures(Contract.Result<TSubject>() != null);
if (subject == null)
{
ThrowArgumentNull<TSubject>();
}
return subject;
}
private static void ThrowArgumentNull<TSubject>() where TSubject : class
{
// ReSharper disable NotResolvedInText
throw new ArgumentNullException("subject", string.Format("Object reference of type '{0}' not set to an instance of an object.", typeof(TSubject).FullName));
// ReSharper restore NotResolvedInText
}
}
}
这是我整理的优缺点列表。
优点:
- 消除了复杂空检查的冗余。
- 与 NullReferenceException 相比,提供有关错误的更多信息。
- 一个集中的地方,您可以控制错误的行为,例如在生产构建中,您可以决定要使用异常,在调试构建中,您可以选择使用断言或记录传播的异常。
- 代码可以更具可读性和优雅。
缺点:
- 表现?
- 陡峭的学习曲线,特别是对于那些对委托、lambda 运算符或函数式编程感到不舒服的人。
- 清晰度降低。
这是我必须处理的一些代码。
var entity = Builder.Entity<User>();
entity.HasKey(u => u.Id);
entity.Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
entity.Property(u => u.DisplayName).IsRequired();
entity.Property(u => u.DisplayName).HasMaxLength(20);
entity.Property(u => u.Username).HasMaxLength(50);
entity.Property(u => u.Password).HasMaxLength(100);
entity.Property(u => u.Salt).HasMaxLength(100);
entity.Property(u => u.IP).HasMaxLength(20);
entity.Property(u => u.CreatingDate);
entity.Property(u => u.LastActivityDate);
entity.Property(u => u.LastLockoutDate);
entity.Property(u => u.LastLoginDate);
entity.Property(u => u.LastPasswordChangedDate);