Last active
July 13, 2016 13:26
-
-
Save VincentH-Net/848d9f9943a5ffa17bc3f1e8b0b505ba to your computer and use it in GitHub Desktop.
Backend For FrontEnd API for Xamarin Apps
This file contains 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.Threading.Tasks; | |
using PropertyChanged; | |
/// <summary> | |
/// See "Consuming the BFF API" in http://vincenth.net/blog/archive/2016/07/13/building-bff-backend-for-frontend-apis-for-xamarin-mobile-apps.aspx | |
/// </summary> | |
namespace MyApp.ApiModels | |
{ | |
[ImplementPropertyChanged] // This will make all properties in all partial Contact classes data-bindable | |
public partial class Contact : ViewModelBase | |
{ | |
public RelayCommand SaveCommand { get; private set; } | |
async private Task Save() { } // Implement save... | |
} | |
public abstract class ViewModelBase { } // E.g. implement INotifyPropertyChanged if you want something other than the Fody generated default ... | |
public class RelayCommand { } // Implement ICommand ... | |
} |
This file contains 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 Newtonsoft.Json; | |
/// <summary> | |
/// See "BFF API Design Artifacts" in http://vincenth.net/blog/archive/2016/07/13/building-bff-backend-for-frontend-apis-for-xamarin-mobile-apps.aspx | |
/// </summary> | |
namespace MyApp.ApiModels | |
{ | |
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] // When we extend this class to a viewmodel in the client, we do not want to serialize all additional properties (e.g. commands) | |
public partial class Contact | |
{ | |
[JsonProperty] | |
public Guid Id { get; set; } | |
[JsonProperty] | |
public string Name { get; set; } | |
// More properties ... | |
} | |
// More API model classes ... | |
} | |
namespace MyApp.Api | |
{ | |
using ApiModels; | |
public class ApiDesignData : IApi | |
{ | |
public ApiResult<Contact> GetContact(Guid contactId) | |
{ | |
return ApiResult<Contact>.FromValue(new Contact | |
{ | |
Id = new Guid("54AEB38C-0820-4BAA-B27B-054CEE4F1199"), | |
Name = "Hubert Wolfeschlegelsteinhausenbergerdorff" | |
}); | |
} | |
public ApiResult SaveContact(Contact contact) | |
{ | |
return new ApiResult(); | |
} | |
} | |
public interface IApi | |
{ | |
ApiResult<Contact> GetContact(Guid contactId); | |
ApiResult SaveContact(Contact contact); | |
// More API methods ... | |
} | |
public class ApiResult | |
{ | |
public string Error { get; protected set; } | |
public ApiResult(string error = null) { Error = error; } | |
} | |
public class ApiResult<T> : ApiResult | |
{ | |
public T Value { get; private set; } | |
public static ApiResult<T> FromValue(T value) { return new ApiResult<T> { Value = value }; } | |
public static ApiResult<T> FromError(string error) { return new ApiResult<T> { Error = error }; } | |
} | |
} |
This file contains 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
[ImplementPropertyChanged] | |
public class Person | |
{ | |
public string GivenNames { get; set; } | |
public string FamilyName { get; set; } | |
public string FullName { get { return string.Format("{0} {1}", GivenNames, FamilyName); } } | |
// PropertyChanged will automatically be raised for FullName whenever GivenNames or FamilyName change | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment