Xamarin.Forms(UWP、Droid 和 iOS)的 Breeze#?
Breeze# for Xamarin.Forms (UWP, Droid & iOS)?
在 their website, Xamarin appears as one of their clients, but I'm unable to install the package Breeze.Sharp 中,它也被标记为 Xamarin。
它确实安装到 PCL 项目中,但要使其正常工作,我需要将它安装到所有平台项目中。当我尝试这样做时,出现以下错误:
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).
我是这样解决的
1. 创建新的便携式 Class 库
2. 添加 BreezeSharp 作为新的 Portable Class Library(Nuget)
的参考
3. 添加新的 Class 库作为您特定平台项目的参考(Android、iOS)。在我的例子中只有 Android.
4. 在便携式图书馆中。创建静态 class 例如:Configs.
5. Android 项目 -> MainActivity.OnCreate
Configs.ModelAssembly = typeof(Customer).Assembly;
- 在便携式 Class 库中实现 Breeze 的数据服务。
例如:
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;
}
}
- 如果需要,请创建 CustomerService
public class CustomerDataService : BaseDataService<Customer>
{
public CustomerDataService(IConstants constants) : base("Customers")
{
}
}
在我从 Breeze# 的发明者 IdeaBlade 那里收到的一封电子邮件中,他说虽然 Breeze# 没有消亡,但与 BreezeJS 相比,它的社区关注度要低得多,因此他们更愿意将资源投入到JS 库。
现在,我将使用 TrackableEntities by @AnthonySneed。虽然不如 Breeze# 全面,但也能很好地完成部分工作。
在 their website, Xamarin appears as one of their clients, but I'm unable to install the package Breeze.Sharp 中,它也被标记为 Xamarin。
它确实安装到 PCL 项目中,但要使其正常工作,我需要将它安装到所有平台项目中。当我尝试这样做时,出现以下错误:
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).
我是这样解决的
1. 创建新的便携式 Class 库
2. 添加 BreezeSharp 作为新的 Portable Class Library(Nuget)
的参考
3. 添加新的 Class 库作为您特定平台项目的参考(Android、iOS)。在我的例子中只有 Android.
4. 在便携式图书馆中。创建静态 class 例如:Configs.
5. Android 项目 -> MainActivity.OnCreate
Configs.ModelAssembly = typeof(Customer).Assembly;
- 在便携式 Class 库中实现 Breeze 的数据服务。 例如:
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;
}
}
- 如果需要,请创建 CustomerService
public class CustomerDataService : BaseDataService<Customer>
{
public CustomerDataService(IConstants constants) : base("Customers")
{
}
}
在我从 Breeze# 的发明者 IdeaBlade 那里收到的一封电子邮件中,他说虽然 Breeze# 没有消亡,但与 BreezeJS 相比,它的社区关注度要低得多,因此他们更愿意将资源投入到JS 库。
现在,我将使用 TrackableEntities by @AnthonySneed。虽然不如 Breeze# 全面,但也能很好地完成部分工作。