幸福框架:可扩展的应用程序 “启动引导” 框架
背景
应用程序引导经常要做如下这些事情:
-
- 动态加载DLL(或各种插件)。
- 注册IOC容器。
- 设置其他全局配置。
尤其以注册IOC容器的代码最烦人,看一个我昨天写的烦人代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Threading; 7 8 using Microsoft.Practices.ServiceLocation; 9 using Microsoft.Practices.Unity; 10 11 using Happy.Event; 12 using Happy.Event.Offline; 13 14 namespace Happy.Event.Demo.Simple 15 { 16 class Program 17 { 18 static readonly string _QueueName = "Happy.Event.Demo.Simple"; 19 20 static void Main(string[] args) 21 { 22 InitUnity(); 23 24 var writeQueue = OfflineEventQueueFactory.CreateMSMQOfflineEventQueue(_QueueName); 25 EventPublisher.Current.AddService(writeQueue); 26 27 EventPublisher.Current.Publish(new TestEvent()); 28 29 new Thread(() => 30 { 31 var readQueue = OfflineEventQueueFactory.CreateMSMQOfflineEventQueue(_QueueName); 32 var offlineEventProcessor = new OfflineEventProcessor(readQueue); 33 34 offlineEventProcessor.Start(); 35 }) 36 .Start(); 37 38 Console.ReadLine(); 39 } 40 41 static void InitUnity() 42 { 43 var container = new UnityContainer(); 44 45 ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container)); 46 47 container.RegisterType<ISyncEventSubscriber<TestEvent>, TestSyncEventSubscriber1>("TestSyncEventSubscriber1"); 48 container.RegisterType<ISyncEventSubscriber<TestEvent>, TestSyncEventSubscriber2>("TestSyncEventSubscriber2"); 49 container.RegisterType<IAsyncEventSubscriber<TestEvent>, TestAsyncEventSubscriber1>("TestAsyncEventSubscriber1"); 50 container.RegisterType<IAsyncEventSubscriber<TestEvent>, TestAsyncEventSubscriber2>("TestAsyncEventSubscriber2"); 51 container.RegisterType<IOfflineEventSubscriber<TestEvent>, TestOfflineEventSubscriber1>("TestOfflineEventSubscriber1"); 52 container.RegisterType<IOfflineEventSubscriber<TestEvent>, TestOfflineEventSubscriber2>("TestOfflineEventSubscriber2"); 53 } 54 } 55 }
是不是很糟糕,看着不爽。这也是我写这篇文章的目的。
新框架原理
约定胜于配置
用新框架重新昨天的配置代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Threading; 7 8 using Happy.Bootstrap; 9 using Happy.Event; 10 using Happy.Event.Offline; 11 12 namespace Happy.Event.Demo.Simple 13 { 14 class Program 15 { 16 static readonly string _QueueName = "Happy.Event.Demo.Simple"; 17 18 static void Main(string[] args) 19 { 20 InitApp(); 21 22 InitOfflineEventProcessor(); 23 24 InitEventPublisher(); 25 26 EventPublisher.Current.Publish(new TestEvent()); 27 } 28 29 static void InitApp() 30 { 31 BootstrapService 32 .Current 33 .UseRegisterServiceByConventionPlug() 34 .UseEventSubscriberRegister() 35 .Done() 36 .Start(); 37 } 38 39 private static void InitEventPublisher() 40 { 41 var writeQueue = OfflineEventQueueFactory.CreateMSMQOfflineEventQueue(_QueueName); 42 EventPublisher.Current.AddService(writeQueue); 43 } 44 45 private static void InitOfflineEventProcessor() 46 { 47 new Thread(() => 48 { 49 var readQueue = OfflineEventQueueFactory.CreateMSMQOfflineEventQueue(_QueueName); 50 var offlineEventProcessor = new OfflineEventProcessor(readQueue); 51 52 offlineEventProcessor.Start(); 53 }) 54 .Start(); 55 } 56 } 57 }
这样是不是看着舒服多了。
备注
代码示例,可以直接去http://happy.codeplex.com/SourceControl/latest这里下载最新的源代码。