Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kijanawoodard/4237737 to your computer and use it in GitHub Desktop.

Select an option

Save kijanawoodard/4237737 to your computer and use it in GitHub Desktop.
public class FooEditRequestModel
...
public class FooEditInputModel : FooEditRequestModel
....
public class FooEditViewModel : FooEditInputModel, IRedirectable
....
public FooEditViewModel Execute(FooEditRequestModel request)
{
var foo = db.GetFooById(request.FooId);
var bars = db.GetBars();
var model = new FooEditViewModel() {FooId = foo.FooId, BarId = foo.BarId, Bars = bars};
return model;
}
public FooEditViewModel Execute(FooEditInputModel input)
{
var model = new FooEditViewModel();
try
{
db.UpdateBarId(input.FooId, input.BarId);
model.RedirectTo = FubuContinuation.RedirectTo<SomeOtherRequestModel>();
}
catch (MyValidationBusinessRuleOrDBExceptions e)
{
//Auto Mapper the properties needed for the request
var request = input.MapTo<FooEditRequestModel>();
model = Execute(request);
model = input.MapTo(model); //Auto Mapper the input data to rehydrate the view
}
return model;
}
public class FooEditRequestModel
{
public int FooId { get; set; }
}
public class FooEditViewModel : IRedirectable
{
public int FooId { get; set; }
[Required]
public int BarId { get; set; }
public IEnumerable<Bar> Bars { get; set; } //reference/lookup data
public FubuContinuation RedirectTo { get; set; }
}
public class FooEditInputModel
{
public int FooId { get; set; }
[Required]
public int BarId { get; set; }
}
public static void ImportantMethod(string value)
{
if (value == null)
throw new ArgumentNullException();
}
public static class ObjectExtensionMethods
{
public static void NullCheck<T>(this T foo)
{
NullCheck(foo, string.Empty);
}
public static void NullCheck<T>(this T foo, string variableName)
{
if (foo == null)
throw new ArgumentNullException(variableName);
}
public static void NullCheck<T>(this T foo, string variableName, string message)
{
if (foo == null)
throw new ArgumentNullException(variableName, message);
}
}
//usage:
public static void ImportantMethod(string value)
{
value.NullCheck();
}
public static void DoSomething(Foo foo)
{
var thing = foo == null ? null : foo.Thing;
}
public static class ObjectExtensionMethods
{
public static TResult NullOr<T, TResult>(this T foo, Func<T, TResult> func)
{
if (foo == null) return default(TResult);
return func(foo);
}
}
//usage:
public static void DoSomething(Foo foo)
{
var value = foo.NullOr(f => f.Property);
}
public static class IEnumerableExtender
{
public static IEnumerable<IEnumerable<T>> Partition<T, TResult>(
this IEnumerable<T> list, Func<T, TResult> partition)
{
var elements = list.Select(partition).Distinct();
foreach (var item in elements)
{
yield return list.Where(x => partition(x).Equals(item));
}
}
}
//usage:
var final = list.Partition(x => x.DivisionId );
private class Score
{
public int ScoreTypeId { get; set; }
public double Score { get; set; }
public int DivisionId { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment