使用单例模式时,我的公共类应该返回私有还是公共实例?

goo*_*ate 1 c# garbage-collection xamarin.ios xamarin.android automatic-ref-counting

我有一个像这样定义的单例:

public partial class MoonDataManager
{

    static MoonDataManager _singletonInstance;
    public static MoonDataManager SingletonInstance
    {
        get
        {
            return _singletonInstance;
        }
        private set
        {
            _singletonInstance = value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我有一个安全地创建实例的函数:

   public static async Task<MoonDataManager> CreateSingletonAsync()
    {
            _singletonInstance = new MoonDataManager();
Run Code Online (Sandbox Code Playgroud)

我是不是该:

return  _singletonInstance;  (field) 
Run Code Online (Sandbox Code Playgroud)

要么

return SingletonInstance;  (property)
Run Code Online (Sandbox Code Playgroud)

我关心垃圾收集,特别是在Xamarin的iOS或Android中.

如果在C#中有这样的命名模式,请告诉我是否偏离了标准.


更新:

现在我觉得我真的陷入了线程和异步方法的困境.以下是对象及其目标:

  • MoonDataManager:RegisterTable<Models.IssuerKey>每个表运行一次.这是一种基本上运行的通用方法(new MobileServiceSQLiteStore).DefineTable<T>()

  • OfflineStore:这是一个MobileServiceSQLiteStore.

  • MobileClient:这是一个MobileServiceClient.

  • MoonDataManager依赖关系:MoonDataManager要求OfflineStore和MobileClient完成初始化.具体来说,它执行MobileServiceClient.SyncContext.InitializeAsync(OfflineStore)

我不确定如何理解这种依赖关系的意义...或者如何使代码看起来很好,并且是线程安全的.

这是代码的新迭代:

private readonly Lazy<MobileServiceClient> lazyMobileClient = 
        new Lazy<MobileServiceClient>(() => new MobileServiceClient(Constants.ApplicationURL), true); // true for thread safety
    public  MobileServiceClient MobileClient { get { return lazyMobileClient.Value; } }


    private readonly Lazy< MobileServiceSQLiteStore> offlineDB =
        new Lazy<MobileServiceSQLiteStore>(() =>  new MobileServiceSQLiteStore(Constants.OfflineDBName), true ); // true for thread safety
    private MobileServiceSQLiteStore OfflineStore { get { return offlineDB.Value; } }

    private static readonly Lazy<MoonDataManager> lazy =  new Lazy<MoonDataManager>(() => new MoonDataManager(), true); // true for thread safety
    public static MoonDataManager Instance { get { return lazy.Value; } }

    private MoonDataManager()
    {

             MoonDataManager.Instance.RegisterTable<Models.IssuerKey>();

            // Initialize file sync
            // todo: investigate FileSyncTriggerFactory overload. 
            //Was present on Mar 30, 2016 Channel9  https://channel9.msdn.com/events/Build/2016/P408
            MoonDataManager.Instance.MobileClient.InitializeFileSyncContext
                           (new IssuerKeyFileSyncHandler(Instance),   Instance.OfflineStore);

            // NOTE THE ASYNC METHOD HERE (won't compile)
            await MoonDataManager.Instance.MobileClient
                                 .SyncContext.InitializeAsync(MoonDataManager.Instance.OfflineStore,
                                StoreTrackingOptions.NotifyLocalAndServerOperations);

    }
Run Code Online (Sandbox Code Playgroud)

Cod*_*shi 5

对于.NET 4或更高版本,您可以使用Lazy<T>它并像这样创建它.

public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton(), true); // true for thread safety

    public static Singleton Instance { get { return lazy.Value; } }

    private Singleton()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

只有在第一次访问时才会创建它,并且它是线程安全的.