Created
December 8, 2012 00:15
-
-
Save kijanawoodard/4237737 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class FooEditRequestModel | |
| ... | |
| public class FooEditInputModel : FooEditRequestModel | |
| .... | |
| public class FooEditViewModel : FooEditInputModel, IRedirectable | |
| .... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void ImportantMethod(string value) | |
| { | |
| if (value == null) | |
| throw new ArgumentNullException(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void DoSomething(Foo foo) | |
| { | |
| var thing = foo == null ? null : foo.Thing; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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