Last active
December 19, 2015 09:59
-
-
Save khalidabuhakmeh/5936642 to your computer and use it in GitHub Desktop.
Allow you to set properties on a command, before it is executed. This will let you set Query parameters, paging, and etc.
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
| class Program | |
| { | |
| public static Dictionary<Type, object> Stubs = new Dictionary<Type, object> | |
| { | |
| { | |
| typeof (GetUserById), new StubGetUserById(x => new Person { Name = string.Format("{0} {1} {2}", x.FirstName, x.LastName, "Test") } ) | |
| } | |
| }; | |
| static void Main(string[] args) | |
| { | |
| var query = | |
| Command<GetUserById>(x => | |
| { | |
| x.FirstName = "Khalid"; | |
| x.LastName = "Abuhakmeh"; | |
| }).Execute(); | |
| Console.WriteLine(query.Name); | |
| Console.ReadLine(); | |
| } | |
| private static TCommand Command<TCommand>(Action<TCommand> init = null) | |
| where TCommand : class | |
| { | |
| // pretend this is some fancy IoC Container | |
| var command = Stubs[typeof(TCommand)] as TCommand; | |
| if (init != null) | |
| init(command); | |
| return command; | |
| } | |
| public class GetUserById | |
| { | |
| public virtual Person Execute() | |
| { | |
| return new Person { Name = string.Format("{0} {1}", FirstName, LastName) }; | |
| } | |
| public string LastName { get; set; } | |
| public string FirstName { get; set; } | |
| } | |
| public class StubGetUserById : GetUserById | |
| { | |
| private readonly Func<GetUserById, Person> _result; | |
| public StubGetUserById(Func<GetUserById, Person> result) | |
| { | |
| _result = result; | |
| } | |
| public override Person Execute() | |
| { | |
| return _result(this); | |
| } | |
| } | |
| public class Person | |
| { | |
| public string Name { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment