Back to Catalog
Why do you have to have this article
This article mainly realizes the RTX message producer, and completes the overall design way, before the design time message producer Global use single production mode, namely a project uses the email to use the SMS, this design method and the actual does not match, although is in the performance best (uses the singleton model, Maintenance of a producer, using a strategy model for the implementation of the producer, using the factory model to produce specific producers, the actual project should be able to automatically select the message producer, of course, for the performance of the program, we must also use a singleton model to produce specific producers, this single case, in this procedure, the use of specific types, Specific single example of the method, that is, e-mail to maintain a single case, SMS self-maintenance singleton, so that the program can meet the actual needs, but also improve their performance!
V Message Component
Sixth message Component ~ Cont.
A powerful design
A powerful producer
// <summary> ///message producer///the specific message producer is a singleton, such as EMAIL,SMS,RTX, etc./// </summary> Public classMessagefactory {/// <summary> ///Message Factory/// </summary> Public StaticImessagemanager GetService (MessageType MessageType) {Switch(messageType) { CaseMessagetype.email:returnemailmessagemanager.instance; Casemessagetype.sms:returnsmsmessagemanager.instance; CaseMessagetype.rtx:returnrtxmessagemanager.instance; default: Throw NewNotImplementedException ("message producer not recognized ..."); } } }
Implementation of the RTX producer
/// <summary> ///RTX Messaging Service/// </summary> Internal classRtxmessagemanager:imessagemanager {#regionSingletonPrivate Static ObjectLockobj =New Object(); Public StaticRtxmessagemanager Instance; StaticRtxmessagemanager () {Lock(lockobj) {if(Instance = =NULL) Instance=NewRtxmessagemanager (); } } PrivateRtxmessagemanager () {}#endregion Private stringEncodingstring (stringstr) { returnHttputility.urlencode (str, encoding.getencoding ("GB2312")); } Private stringRtxurl = system.configuration.configurationmanager.appsettings["Rtxapi"] ??"http://192.168.1.8:8012/sendnotifynew.cgi?"; #regionImessagemanager Members Public voidSend (stringRecipientstringSubjectstringbody) {Send (Newlist<string>{recipient}, subject, body); } Public voidSend (ienumerable<string> Recipients,stringSubjectstringbody) { Try { varDIC =Newdictionary<string,string>() { {"title", encodingstring (subject)}, {"msg", encodingstring (Body)}, {"receiver", Encodingstring (string. Join (",", recipients)}, {"Delaytime","0"} }; StringBuilder Str=NewStringBuilder (); foreach(varIteminchdic) {str. Append (item. Key+"="+ Item. Value +"&"); } NewHttpClient (). Getasync (Rtxurl +Str. ToString ()). Wait (); } Catch(Exception ex) {Logger.Core.LoggerFactory.Instance.Logger_Info (ex). Message); } } Public voidSend (ienumerable<string> Recipients,stringSubjectstringBodyBOOLIsAsync) { if(IsAsync) Task.run (()={Send (recipients, subject, body); }); ElseSend (recipients, subject, body); } #endregion }
Back to Catalog
Core components of my heart (pluggable AOP) ~ Message Components ~ Perfect article