Created
February 21, 2011 23:11
-
-
Save thecodejunkie/837889 to your computer and use it in GitHub Desktop.
Sample for how a nested closure could be used to configure a ServiceHost in Elasticity
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 | |
| { | |
| static void Main(string[] args) | |
| { | |
| var scheduler = | |
| new DefaultScheduler(); | |
| var host = new ServiceHost(scheduler, x => { | |
| x.JobRequestQueue = "foo"; | |
| x.TaskResponseQueue = "bar"; | |
| }); | |
| } | |
| } | |
| public class ServiceHost | |
| { | |
| private readonly IScheduler scheduler; | |
| private readonly ISchedulerConfiguration schedulerConfiguration; | |
| public ServiceHost(IScheduler scheduler, ISchedulerConfiguration schedulerConfiguration) | |
| { | |
| this.scheduler = scheduler; | |
| this.schedulerConfiguration = schedulerConfiguration; | |
| } | |
| public ServiceHost(IScheduler scheduler, Action<SchedulerConfigurationConfigurator> closure) | |
| { | |
| this.scheduler = scheduler; | |
| var configuration = | |
| new DefaultSchedulerConfiguration(); | |
| var configurator = | |
| new SchedulerConfigurationConfigurator(configuration); | |
| closure.Invoke(configurator); | |
| this.schedulerConfiguration = configuration; | |
| } | |
| public class SchedulerConfigurationConfigurator | |
| { | |
| private readonly ISchedulerConfiguration schedulerConfiguration; | |
| public SchedulerConfigurationConfigurator(ISchedulerConfiguration schedulerConfiguration) | |
| { | |
| this.schedulerConfiguration = schedulerConfiguration; | |
| } | |
| public string JobRequestQueue | |
| { | |
| set { this.schedulerConfiguration.JobRequestsQueue = value; } | |
| } | |
| public string TaskResponseQueue | |
| { | |
| set { this.schedulerConfiguration.JobRequestsQueue = value; } | |
| } | |
| } | |
| } | |
| public interface IScheduler | |
| { | |
| } | |
| class DefaultScheduler : IScheduler | |
| { | |
| } | |
| public interface ISchedulerConfiguration | |
| { | |
| string JobRequestsQueue { get; set; } | |
| string TaskResponseQueue { get; set; } | |
| } | |
| public class DefaultSchedulerConfiguration : ISchedulerConfiguration | |
| { | |
| public string JobRequestsQueue { get; set; } | |
| public string TaskResponseQueue { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment