我应该紧密耦合,单身,还是松散耦合,没有单身? Azure服务,Xamarin表单

时间:2018-01-14 02:49:07

标签: xamarin.forms azure-sql-database loose-coupling singleton-methods tightly-coupled-code

我正在制作一个Xamarin Forms Cross平台应用。 我一直试图找出如何让我的应用程序与Azure后端集成的最佳方式。 我遇到了一个问题。我已经深入研究了尽可能松散耦合的程序。但后来我被告知要将Azure服务类创建为单例,以阻止多个创建并破坏后端连接

因为我不能使用单例接口来使我的应用程序松散耦合。我必须选择。我认为我应该使用接口,以便松散耦合。但我对后端开发知之甚少。是否会创建我的AzureServices数据库类的多个实例?

感谢您的帮助。

以下是我的AzureServices

的格式示例
namespace CoffeeCups
{

public class AzureService
{

public MobileServiceClient Client { get; set; } = null;

 IMobileServiceSyncTable<CupOfCoffee> coffeeTable;

public static bool UseAuth { get; set; } = false;

        public async Task Initialize()
        {
            if (Client?.SyncContext?.IsInitialized ?? false)
                return;


            var appUrl = "https://chattestjbapp.azurewebsites.net";


            //Create our client

            Client = new MobileServiceClient(appUrl);


            //InitialzeDatabase for path
            var path = "syncstore.db";
            path = Path.Combine(MobileServiceClient.DefaultDatabasePath, path);

            //setup our local sqlite store and intialize our table
            var store = new MobileServiceSQLiteStore(path);

            //Define table
            store.DefineTable<CupOfCoffee>();


            //Initialize SyncContext
            await Client.SyncContext.InitializeAsync(store);

            //Get our sync table that will call out to azure
            coffeeTable = Client.GetSyncTable<CupOfCoffee>();


        }

        public async Task SyncCoffee()
        {
            try
            {
                if (!CrossConnectivity.Current.IsConnected)
                    return;

                await coffeeTable.PullAsync("allCoffee", coffeeTable.CreateQuery());

                await Client.SyncContext.PushAsync();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Unable to sync coffees, that is alright as we have offline capabilities: " + ex);
            }

        }

        public async Task<IEnumerable<CupOfCoffee>> GetCoffees()
        {
            //Initialize & Sync
            await Initialize();
            await SyncCoffee();

            return await coffeeTable.OrderBy(c => c.DateUtc).ToEnumerableAsync(); ;

        }

        public async Task<CupOfCoffee> AddCoffee(bool atHome, string location)
        {
            await Initialize();

            var coffee = new CupOfCoffee
            {
                DateUtc = DateTime.UtcNow,
                MadeAtHome = atHome,
                OS = Device.RuntimePlatform,
                Location = location ?? string.Empty
            };

            await coffeeTable.InsertAsync(coffee);

            await SyncCoffee();
            //return coffee
            return coffee;
        }



        public async Task<bool> LoginAsync()
        {

            await Initialize();

            var provider = MobileServiceAuthenticationProvider.Twitter;
            var uriScheme = "coffeecups";



            var user = await Client.LoginAsync(Forms.Context, provider, uriScheme);


            if (user == null)
            {
                Settings.AuthToken = string.Empty;
                Settings.UserId = string.Empty;
                Device.BeginInvokeOnMainThread(async () =>
                {
                    await App.Current.MainPage.DisplayAlert("Login Error", "Unable to login, please try again", "OK");
                });
                return false;
            }
            else
            {
                Settings.AuthToken = user.MobileServiceAuthenticationToken;
                Settings.UserId = user.UserId;
            }

            return true;
        }    
    }
}

0 个答案:

没有答案