Breeze# for Xamarin.Forms (UWP, Droid & iOS)?

283 views Asked by At

In their website, Xamarin appears as one of their clients, but I'm unable to install the package Breeze.Sharp, which is also tagged with Xamarin.

It does install into the PCL project, but for it to work I need to install it into all of the platform projects. When I try to do so, I get the following errors:

iOS/Android:

Could not install package 'Microsoft.AspNet.WebApi.Client 5.2.3'. You are trying to install this package into a project that targets 'Xamarin.iOS,Version=v1.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

UWP:

Package Breeze.Sharp 0.6.0.9 is not compatible with uap10.0 (UAP,Version=v10.0) / win10-x86-aot. Package Breeze.Sharp 0.6.0.9 supports: net (.NETFramework,Version=v0.0) One or more packages are incompatible with UAP,Version=v10.0 (win10-x86-aot).

2

There are 2 answers

0
Shimmy Weitzhandler On BEST ANSWER

In an email I got from IdeaBlade, the inventor of Breeze#, he said that while the Breeze# is not dead, it has drown much less community attention compared to BreezeJS, thus they prefer to invest their resources in the JS library.

For now, I'm just gonna use TrackableEntities by @AnthonySneed. Although not as comprehensive as Breeze#, does part of the job nicely.

1
Leonardo Neninger On

I resolve in this way.
1. Create New Portable Class Library
2. Add BreezeSharp as reference of the new Portable Class Library(Nuget)
3. Add the New Class Library as reference of your specific platform projects(Android, iOS). In my case only Android.
4. In Portable Library. Create a static class ex: Configs.
5. Android project -> MainActivity.OnCreate

Configs.ModelAssembly = typeof(Customer).Assembly;
  1. Implement Breeze's DataService in the Portable Class Library. ex:

public abstract class BaseDataService<T> where T : BaseEntity
    {
        public static string Metadata { get; protected set; }
        public string EntityName { get; protected set; }
        public string EntityResourceName { get; protected set; }
        public EntityManager EntityManager { get; set; }
        public string DefaultTargetMethod { get; protected set; }
        static BaseDataService()
        {
            Constants = ConstantsFactory.Get;
            try
            {
                var metadata = GetMetadata();
                metadata.Wait();
                Metadata = metadata.Result;

            }
            catch (Exception ex)
            {
                var b = 0;
            }
        }


        public BaseDataService(string resourceName, string targetMethod = null)
        {
            var modelType = typeof(Customer);
            Configuration.Instance.ProbeAssemblies(ConstantsFactory.BusinessAssembly);
            try
            {
                this.EntityName = typeof(T).FullName;
                this.EntityResourceName = resourceName;

                this.DefaultTargetMethod = (string.IsNullOrWhiteSpace(targetMethod) ? "GetAll" : targetMethod);

                var dataService = new DataService($"{ConstantsFactory.Get.BreezeHostUrl}{this.EntityResourceName}", new CustomHttpClient());
                dataService.HasServerMetadata = false;
                var metadataStore = new MetadataStore();
                var namingConvention = NamingConvention.CamelCaseProperties; /*metadataStore.NamingConvention;*/ /*NamingConvention.Default;*/// new NamingConvention()
                namingConvention = namingConvention.WithClientServerNamespaceMapping(
                    new Dictionary<string, string> { { "Business.DomainModels", "DomainModel.Models" } }
                    );

                metadataStore.NamingConvention = namingConvention;

                metadataStore.ImportMetadata(Metadata, true);

                this.EntityManager = new EntityManager(dataService, metadataStore);
                this.EntityManager.MetadataStore.AllowedMetadataMismatchTypes = MetadataMismatchTypes.AllAllowable;
                // Attach an anonymous handler to the MetadataMismatch event
                this.EntityManager.MetadataStore.MetadataMismatch += (s, e) =>
                {
                    // Log the mismatch
                    var message = string.Format("{0} : Type = {1}, Property = {2}, Allow = {3}",
                                                e.MetadataMismatchType, e.StructuralTypeName, e.PropertyName, e.Allow);

                    // Disallow missing navigation properties on the TodoItem entity type
                    if (e.MetadataMismatchType == MetadataMismatchTypes.MissingCLRNavigationProperty &&
                        e.StructuralTypeName.StartsWith("TodoItem"))
                    {
                        e.Allow = false;
                    }
                };
            }
            catch (Exception ex)
            {
                var b = 0;
            }
        }

        public async Task<List<T>> GetAll(string targetMethod = null)
        {
            var internalTargetMethod = (string.IsNullOrWhiteSpace(targetMethod) ? this.DefaultTargetMethod : targetMethod);
            var query = new EntityQuery<T>(internalTargetMethod);
            var qr = await this.EntityManager.ExecuteQuery(query);
            var result = qr.ToList();
            return result;
        }

        public void Delete(T entity)
        {
            entity.EntityAspect.Delete();
        }


        private static async Task<string> GetMetadata()
        {
            var client = new HttpClient();
            var metadata = await client.GetStringAsync(ConstantsFactory.Get.MetadataUrl).ConfigureAwait(false);
            var ret = JsonConvert.DeserializeObject<MetadataModel>(metadata);
            return ret.metadata;
        }


    }

  1. Create CustomerService if you need as

public class CustomerDataService : BaseDataService<Customer>
    {
        public CustomerDataService(IConstants constants) : base("Customers")
        {
            
        }
    }