1

我正在制作一个 Xamarin Forms 跨平台应用程序。我一直在试图弄清楚如何让我的应用程序以最佳方式与 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;
        }    
    }
}
4

0 回答 0