Skip to content

Instantly share code, notes, and snippets.

@tathamoddie
Created July 2, 2012 00:03
Show Gist options
  • Save tathamoddie/3030135 to your computer and use it in GitHub Desktop.
Save tathamoddie/3030135 to your computer and use it in GitHub Desktop.
public class PersistenceResult
{
readonly ModelStateDictionary modelState;
protected PersistenceResult(ModelStateDictionary modelState)
{
this.modelState = modelState;
}
public static PersistenceResult Success()
{
return new PersistenceResult(new ModelStateDictionary());
}
public static PersistenceResult Failure(ModelStateDictionary state)
{
return new PersistenceResult(state);
}
public ModelStateDictionary ModelState
{
get { return modelState; }
}
public bool IsValid
{
get { return modelState.IsValid; }
}
}
using System.Web.Mvc;
namespace MyStory.Logic.Services
{
public class PersistenceResult<TData> : PersistenceResult
{
readonly TData data;
protected PersistenceResult(ModelStateDictionary modelState, TData data)
: base(modelState)
{
this.data = data;
}
public TData Data
{
get { return data; }
}
public static PersistenceResult<TData> Success(TData data)
{
return new PersistenceResult<TData>(new ModelStateDictionary(), data);
}
public static PersistenceResult<TData> Failure(ModelStateDictionary state, TData data)
{
return new PersistenceResult<TData>(state, data);
}
}
}
public class PersistenceResult<TData> : PersistenceResult
{
readonly TData data;
protected PersistenceResult(ModelStateDictionary modelState, TData data)
: base(modelState)
{
this.data = data;
}
public TData Data
{
get { return data; }
}
public static PersistenceResult<TData> Success(TData data)
{
return new PersistenceResult<TData>(new ModelStateDictionary(), data);
}
public static PersistenceResult<TData> Failure(ModelStateDictionary state, TData data)
{
return new PersistenceResult<TData>(state, data);
}
}
public PersistenceResult<NodeReference> CreateUser(User user, NodeReference<Agency> agency)
{
var modelState = user.Validate();
if (userRepository.IsUsernameInUse(u.Username))
modelState.AddModelError<User>(u => u.Username, @"Username is already in use.");
if (!modelState.IsValid)
return PersistenceResult<NodeReference>.Failure(modelState, null);
var node = userRepository.Create(user, new UserBelongsTo(agency));
return PersistenceResult<NodeReference>.Success(node);
}
public static class ValidationHelpers
{
public static ModelStateDictionary Validate(this object target)
{
var state = new ModelStateDictionary();
Validate(target, state);
return state;
}
public static void Validate(this object target, ModelStateDictionary state)
{
var validationContext = new ValidationContext(target, null, null);
var validationResults = new List<ValidationResult>();
Validator.TryValidateObject(target, validationContext, validationResults);
foreach (var validationResult in validationResults)
{
state.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
}
}
public static void AddModelError<TModel>(
this ModelStateDictionary modelState,
Expression<Func<TModel, object>> expression,
string message)
{
var propertyName = ExpressionHelper.GetExpressionText(expression);
modelState.AddModelError(propertyName, message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment