Last active
October 17, 2018 12:30
-
-
Save relyky/4f7b618c9323234f3ead to your computer and use it in GitHub Desktop.
C# Singleton Mechanism 實作練習
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
/// <summary> | |
/// Single mechanism | |
/// 使用 static 靜態宣告實作 | |
/// 此案例為:設定取用系統組態 | |
/// </summary> | |
internal class MyCfg2 | |
{ | |
private MyCfg2() { } | |
//# configuration properties | |
public static string SourceDir | |
{ | |
get; | |
set; | |
} | |
public static string TargetDir | |
{ | |
get; | |
set; | |
} | |
public static AnalysisCfg Analysis | |
{ | |
get; | |
set; | |
} | |
/// 模擬第二層的組態設定 | |
internal class AnalysisCfg | |
{ | |
//private AnalysisCfg() {} // sealed this constructor. | |
public AnalysisCfg(string mode, string start_pos) | |
{ | |
this.Mode = mode; | |
this.StartPos = start_pos; | |
} | |
public string Mode { get; protected set; } | |
public string StartPos { get; protected set; } | |
} | |
} | |
//## 應用 ========================================================= | |
//# get value | |
MyCfg2.SourceDir = "the source dir"; | |
MyCfg2.TargetDir = "the target dir"; | |
MyCfg2.Analysis = new MyCfg2.AnalysisCfg("my mode", "begin"); | |
//# get value | |
string source_dir = MyCfg2.SourceDir; | |
string target_dir = MyCfg2.TargetDir; | |
MyCfg2.AnalysisCfg analysis2 = MyCfg2.Analysis; |
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
/// <summary> | |
/// Single mechanism & Fluent interface | |
/// 加入fluent interface 讓coding時有親和力一些。 | |
/// 此案例為:設定取用系統組態 | |
/// </summary> | |
internal class MyCfg | |
{ | |
#region Singleton Mechanism | |
static MyCfg _instance = new MyCfg(); | |
private MyCfg() { } | |
public static MyCfg Define { | |
get { return _instance; } | |
} | |
#endregion | |
#region configuration preperties | |
private string _sourceDir; | |
private string _targetDir; | |
private AnalysisCfg _analysis; | |
/// <summary> | |
/// 模擬第二層的組態設定 | |
/// </summary> | |
internal class AnalysisCfg | |
{ | |
//private AnalysisCfg() {} // sealed this constructor. | |
public AnalysisCfg(string mode, string start_pos) | |
{ | |
this.Mode = mode; | |
this.StartPos = start_pos; | |
} | |
public string Mode { get; protected set; } | |
public string StartPos { get; protected set; } | |
} | |
#endregion | |
#region handling - get cfg | |
public static string SourceDir | |
{ | |
get { return _instance._sourceDir; } | |
} | |
public static string TargetDir | |
{ | |
get { return _instance._targetDir; } | |
} | |
public static AnalysisCfg Analysis | |
{ | |
get { return _instance._analysis; } | |
} | |
#endregion | |
#region handling - set cfg | |
public MyCfg SetSourceDir(string source_dir) | |
{ | |
_instance._sourceDir = source_dir; | |
return this; | |
} | |
public MyCfg SetTargetDir(string target_dir) | |
{ | |
_instance._targetDir = target_dir; | |
return this; | |
} | |
public MyCfg SetAnalysis(AnalysisCfg analysis_cfg) | |
{ | |
_instance._analysis = analysis_cfg; | |
return this; | |
} | |
#endregion | |
} | |
//## 應用 ========================================================= | |
//# set value | |
MyCfg.Define | |
.SetSourceDir("the source dir") | |
.SetTargetDir("the target dir") | |
.SetAnalysis(new MyCfg.AnalysisCfg("my mode", "begin")); | |
//# get value | |
string source_dir = MyCfg.SourceDir; | |
string target_dir = MyCfg.TargetDir; | |
MyCfg.AnalysisCfg analysis = MyCfg.Analysis; |
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
/// <summary> | |
/// singleton lazy 建構版 | |
/// </summary> | |
public class FooSingleton | |
{ | |
#region Singleton | |
private readonly static Lazy<FooSingleton> _instance = new Lazy<FooSingleton>( | |
() => new FooSingleton()); | |
public static FooSingleton Instance | |
{ | |
get | |
{ | |
return _instance.Value; | |
} | |
} | |
#endregion | |
... | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Runtime.CompilerServices; | |
using System.Web; | |
namespace MPB.MTNET.IM.Web.Helpers | |
{ | |
public class Common2 | |
{ | |
#region Singleton Mechanism | |
private static Common2 _singleton = new Common2(); | |
private Common2() { } | |
public static Common2 Singleton | |
{ | |
get { return _singleton; } | |
} | |
#endregion | |
//## properties | |
private string _defaultClassName = "System"; | |
/// <summary> | |
/// 指定預設的 logging 類別名稱(功能大分類)。 | |
/// </summary> | |
public static string DefaultClassName { | |
get { | |
return _singleton._defaultClassName; | |
} | |
set { | |
_singleton._defaultClassName = value; | |
} | |
} | |
/// <summary> | |
/// helper function : logging | |
/// </summary> | |
/// <param name="message">訊息</param> | |
/// <param name="desc">進一步資訊,如:stack trace 等利於除錯的資訊</param> | |
/// <param name="methodName">CallerMemberName</param> | |
public static void LogMessage(string message, string desc = null, [CallerMemberName] string methodName = "") | |
{ | |
LogMessage(message, desc, methodName, _singleton._defaultClassName); | |
} | |
/// <summary> | |
/// helper function : logging | |
/// </summary> | |
/// <param name="className">類別名稱、功能大分類</param> | |
/// <param name="methodName">成員函式名稱、功能次分類</param> | |
/// <param name="message">訊息</param> | |
/// <param name="desc">進一步資訊,如:stack trace 等利於除錯的資訊</param> | |
public static void LogMessage(string message, string desc, string methodName, string className) | |
{ | |
try | |
{ | |
#if DEBUG | |
Debug.WriteLine(string.Format("LogMessage >> {0}.{1} {2} {3}", className, methodName, message, desc)); | |
#endif | |
// write system log data table | |
using (YourDBEntities ctx = new YourDBEntities()) | |
{ | |
DateTime now = DateTime.Now; | |
string sql = @"INSERT INTO [dbo].[your_system_log] | |
([class_name] | |
,[method_name] | |
,[error_message] | |
,[error_desc] | |
,[log_date] | |
,[log_time]) | |
VALUES | |
(@p0,@p1,@p2,@p3,@p4,@p5)"; | |
// 只是INSERT,直接跑SQL比較快。 | |
ctx.Database.ExecuteSqlCommand(sql, className, methodName, message, desc, now.ToString("yyyyMMdd"), now.ToString("HHmmss")); | |
} | |
} | |
catch | |
{ | |
// log 就算失敗系統也要能續續運轉。 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment