inherited form
an protected DoubleLockInstance singelton with private empty and protected parameter constructor
inherited form
an abstract singelton template with protected constructor
an protected DoubleLockInstance singelton with private empty and protected parameter constructor
inherited form
an abstract singelton template with protected constructor
Application settings example:
BaseSets for basic settings as abstract singelton base
using System;
using System.Reflection;
namespace work.darkstar.blog
{
/// <summary>
/// abstract class BaseSets
/// </summary>
[Serializable]
public abstract class BaseSets
{
public string AssemblyName { get => Assembly.GetExecutingAssembly().GetName().ToString(); }
public string Copyright { get => "GNU LIGHT GENERAL PUBLIC LICENSE 2.0 LGPL"; }
public virtual string AssemblyCom { get => "darkstar.work"; }
public virtual int AppId { get; protected set; }
public virtual string AppName { get; protected set; }
/// <summary>
/// protected empty constructor for inheritable singelton
/// </summary>
protected BaseSets() { }
/// <summary>
/// protected parameterized constructor for inheritable singelton
/// </summary>
protected BaseSets(int appId, string appName)
{
AppId = appId; AppName = appName;
}
}
}
using System;
using System.Reflection;
namespace work.darkstar.blog
{
/// <summary>
/// abstract class BaseSets
/// </summary>
[Serializable]
public abstract class BaseSets
{
public string AssemblyName { get => Assembly.GetExecutingAssembly().GetName().ToString(); }
public string Copyright { get => "GNU LIGHT GENERAL PUBLIC LICENSE 2.0 LGPL"; }
public virtual string AssemblyCom { get => "darkstar.work"; }
public virtual int AppId { get; protected set; }
public virtual string AppName { get; protected set; }
/// <summary>
/// protected empty constructor for inheritable singelton
/// </summary>
protected BaseSets() { }
/// <summary>
/// protected parameterized constructor for inheritable singelton
/// </summary>
protected BaseSets(int appId, string appName)
{
AppId = appId; AppName = appName;
}
}
}
AppSets for application settings as instancable singelton default app sets (settings):
using Microsoft.Win32;
using System;
using System.Reflection;
using System.Windows.Forms;
namespace work.darkstar.blog
{
public interface IAppSetsDomain : IAppDomainSetup
{
System.AppDomain AppCurrentDomain { get; }
}
/// <summary>
/// application settings singelton
/// </summary>
[Serializable] public class AppSets : BaseSets, IAppDomainSetup
{
private static AppSets _appSets;
private static object _initLock, _sndLock;
protected static AppSets DoubleLockInstance {
get {
_sndLock = new System.Object();
lock (_sndLock) {
if (_initLock != null) _initLock = null;
if (_initLock == null) _initLock = new System.Object();
lock (_initLock) {
if (_appSets == null)
_appSets = new AppSets();
}
return _appSets;
}
}
}
public string CodeBase { get => Assembly.GetExecutingAssembly().CodeBase; }
public string BaseDirectory { get => AppDomain.CurrentDomain.BaseDirectory; }
public string AppDataPath { get => Application.CommonAppDataPath; }
public RegistryKey AppDataRegistry { get => Application.CommonAppDataRegistry; }
#region implementing interface IAppSetsDomain, IAppDomainSetup public AppDomain AppCurrentDomain { get => AppDomain.CurrentDomain; } public string ApplicationBase { get ; set; } public string ApplicationName{ get ; set; }
public string CachePath{ get ; set; } public string ConfigurationFile{ get ; set; }
public string DynamicBase { get ; set; }
public string LicenseFile { get ; set; }
public string PrivateBinPath { get ; set; }
public string PrivateBinPathProbe { get ; set; }
public string ShadowCopyDirectories { get ; set; }
public string [] ShadowCopyDirectoryArray {
get => ShadowCopyDirectories.Split(';'); } public bool FilesShadowCopy { get ; set; } public string ShadowCopyFiles { get => FilesShadowCopy.ToString().ToLower(); set { FilesShadowCopy = Boolean.Parse(value); } } public bool FilesShadowCopy{ get ; set; } public string ShadowCopyFiles{ get => FilesShadowCopy.ToString() ; set; } #endregion implementing interface IAppSetsDomain, IAppDomainSetup
/// <summary>
/// static constructor
/// </summary>
static AppSets() {
_initLock = new System.Object();
lock (_initLock) { _appSets = new AppSets(); }
}
/// <summary>
/// private empty constructor
/// </summary>
private AppSets() {
AppId = AppDomain.CurrentDomain.Id;
AppName = Assembly.GetExecutingAssembly().FullName;
}
/// <summary>
/// protected parameter constructor
/// </summary>
protected AppSets(int appId, string appName) : base(appId, appName) { }
}
}
using Microsoft.Win32;
using System;
using System.Reflection;
using System.Windows.Forms;
namespace work.darkstar.blog
{
public interface IAppSetsDomain : IAppDomainSetup
{
System.AppDomain AppCurrentDomain { get; }
}
/// <summary>
/// application settings singelton
/// </summary>
[Serializable] public class AppSets : BaseSets, IAppDomainSetup
{
private static AppSets _appSets;
private static object _initLock, _sndLock;
protected static AppSets DoubleLockInstance {
get {
_sndLock = new System.Object();
lock (_sndLock) {
if (_initLock != null) _initLock = null;
if (_initLock == null) _initLock = new System.Object();
lock (_initLock) {
if (_appSets == null)
_appSets = new AppSets();
}
return _appSets;
}
}
}
public string CodeBase { get => Assembly.GetExecutingAssembly().CodeBase; }
public string BaseDirectory { get => AppDomain.CurrentDomain.BaseDirectory; }
public string AppDataPath { get => Application.CommonAppDataPath; }
public RegistryKey AppDataRegistry { get => Application.CommonAppDataRegistry; }
#region implementing interface IAppSetsDomain, IAppDomainSetup public AppDomain AppCurrentDomain { get => AppDomain.CurrentDomain; } public string ApplicationBase { get ; set; } public string ApplicationName{ get ; set; }
public string CachePath{ get ; set; } public string ConfigurationFile{ get ; set; }
public string DynamicBase { get ; set; }
public string LicenseFile { get ; set; }
public string PrivateBinPath { get ; set; }
public string PrivateBinPathProbe { get ; set; }
public string ShadowCopyDirectories { get ; set; }
public string [] ShadowCopyDirectoryArray {
get => ShadowCopyDirectories.Split(';'); } public bool FilesShadowCopy { get ; set; } public string ShadowCopyFiles { get => FilesShadowCopy.ToString().ToLower(); set { FilesShadowCopy = Boolean.Parse(value); } } public bool FilesShadowCopy{ get ; set; } public string ShadowCopyFiles{ get => FilesShadowCopy.ToString() ; set; } #endregion implementing interface IAppSetsDomain, IAppDomainSetup
/// <summary>
/// static constructor
/// </summary>
static AppSets() {
_initLock = new System.Object();
lock (_initLock) { _appSets = new AppSets(); }
}
/// <summary>
/// private empty constructor
/// </summary>
private AppSets() {
AppId = AppDomain.CurrentDomain.Id;
AppName = Assembly.GetExecutingAssembly().FullName;
}
/// <summary>
/// protected parameter constructor
/// </summary>
protected AppSets(int appId, string appName) : base(appId, appName) { }
}
}
Sealed MyAppSets for application specialized appSets as Lazy<T> singelton:
using System;
using System.IO;
using System.Security;
/* ... */
using Microsoft.Win32;
/* ... */
using Windows.Forms;
namespace work.darkstar.blog
{
/// <summary>
/// my application settings singelton
/// </summary>
[Serializable]
public sealed class MyAppSets : AppSets
{
/// <summary>
/// private static readonly Lazy<T> self containing private real singelton unique instance
/// </summary>
private static readonly Lazy<MyAppSets> _instance =
new Lazy<MyAppSets>(() => new MyAppSets(AppDomain.CurrentDomain.Id, "LazyApp"));
/// <summary>
/// static instance getter for Singeltion
/// </summary>
public static MyAppSets Instance { get => _instance.Value; }
public string UserAppDataPath { get => Application.UserAppDataPath; }
public RegistryKey UserAppDataRegistry { get => Application.UserAppDataRegistry; }
/// <summary>
/// private constructor with partameters for sealed unique singelton
/// </summary>
private MyAppSets(int appId, string appName) : base(appId, appName) { }
/// <summary>
/// Gets name value pair for application registry key saved in registry scope for current user
/// </summary>
/// <param name="regName">registry name identifier</param>
/// <param name="subKey">subKey in scope of current user</param>
/// <returns>object value</returns>
/// <exception cref="ApplicationException">application exception with detailed inner exception</exception>
public object GetUserRegValuey(string regName, string subKey = null) {
object o = null;
RegistryKey key = null;
Exception ex = null;
try {
key = (subKey == null) ? UserAppDataRegistry
: UserAppDataRegistry.OpenSubKey(subKey, false);
o = key.GetValue(regName);
}
catch (SecurityException sex) { ex = sex; }
catch (ObjectDisposedException odEx) { ex = odEx; }
catch (UnauthorizedAccessException uaEx) { ex = uaEx; }
catch (IOException ioeEx) { ex = ioeEx; }
finally {
if (key != null && subKey != null)
key.Close();
if (ex != null) throw (new ApplicationException("Error accessing registy key: " +
$"{UserAppDataRegistry}\t name: {regName}\t subkey: {subKey}", ex));
}
return o;
}
/// <summary>
/// Set Value for UserAppDataRegistry
/// </summary>
/// <param name="regName">registry name </param>
/// <param name="value"value to set></param>
/// <param name="subKey">subKey</param>
/// <returns>void means nothing</returns>
/// <exception cref="ApplicationException">application exception with detailed inner exception</exception>
public void SetUserReg(string regName, object value, string subKey = null) {
RegistryKey key = null;
Exception ex = null;
try {
key = (subKey == null) ? UserAppDataRegistry
: UserAppDataRegistry.OpenSubKey(subKey, true);
key.SetValue(regName, value);
} catch (Exception anyEx) {
ex = new ApplicationException($"Error setting value=" +
$"{value} for name={regName} inside registry key: {key.Name}",
anyEx);
} finally {
if (key != null && subKey != null)
key.Close();
if (ex != null) throw ex;
}
}
}
}
using System;
using System.IO;
using System.Security;
/* ... */
using Microsoft.Win32;
/* ... */
using Windows.Forms;
namespace work.darkstar.blog
{
/// <summary>
/// my application settings singelton
/// </summary>
[Serializable]
public sealed class MyAppSets : AppSets
{
/// <summary>
/// private static readonly Lazy<T> self containing private real singelton unique instance
/// </summary>
private static readonly Lazy<MyAppSets> _instance =
new Lazy<MyAppSets>(() => new MyAppSets(AppDomain.CurrentDomain.Id, "LazyApp"));
/// <summary>
/// static instance getter for Singeltion
/// </summary>
public static MyAppSets Instance { get => _instance.Value; }
public string UserAppDataPath { get => Application.UserAppDataPath; }
public RegistryKey UserAppDataRegistry { get => Application.UserAppDataRegistry; }
/// <summary>
/// private constructor with partameters for sealed unique singelton
/// </summary>
private MyAppSets(int appId, string appName) : base(appId, appName) { }
/// <summary>
/// Gets name value pair for application registry key saved in registry scope for current user
/// </summary>
/// <param name="regName">registry name identifier</param>
/// <param name="subKey">subKey in scope of current user</param>
/// <returns>object value</returns>
/// <exception cref="ApplicationException">application exception with detailed inner exception</exception>
public object GetUserRegValuey(string regName, string subKey = null) {
object o = null;
RegistryKey key = null;
Exception ex = null;
try {
key = (subKey == null) ? UserAppDataRegistry
: UserAppDataRegistry.OpenSubKey(subKey, false);
o = key.GetValue(regName);
}
catch (SecurityException sex) { ex = sex; }
catch (ObjectDisposedException odEx) { ex = odEx; }
catch (UnauthorizedAccessException uaEx) { ex = uaEx; }
catch (IOException ioeEx) { ex = ioeEx; }
finally {
if (key != null && subKey != null)
key.Close();
if (ex != null) throw (new ApplicationException("Error accessing registy key: " +
$"{UserAppDataRegistry}\t name: {regName}\t subkey: {subKey}", ex));
}
return o;
}
/// <summary>
/// Set Value for UserAppDataRegistry
/// </summary>
/// <param name="regName">registry name </param>
/// <param name="value"value to set></param>
/// <param name="subKey">subKey</param>
/// <returns>void means nothing</returns>
/// <exception cref="ApplicationException">application exception with detailed inner exception</exception>
public void SetUserReg(string regName, object value, string subKey = null) {
RegistryKey key = null;
Exception ex = null;
try {
key = (subKey == null) ? UserAppDataRegistry
: UserAppDataRegistry.OpenSubKey(subKey, true);
key.SetValue(regName, value);
} catch (Exception anyEx) {
ex = new ApplicationException($"Error setting value=" +
$"{value} for name={regName} inside registry key: {key.Name}",
anyEx);
} finally {
if (key != null && subKey != null)
key.Close();
if (ex != null) throw ex;
}
}
}
}
Accessing MyAppSets singelton inside any entity, framework, helper, [...] class
using System;
/* */
using work.darkstar.blog;
public class MyEntity : IAppSetsDomain {
/* ... */
/* [Inject] */
/* ... */
public AppDomain AppCurrentDomain {
get => MyAppSets.Instance.AppCurrentDomain;
set => MyAppSets.Instance.AppCurrentDomain = value;
}
/* ... */
}
Keine Kommentare:
Kommentar veröffentlichen