面向 Windows Phone 应用和 Windows Store 应用的 Windows Azure Mobile Services 的存储库模式






4.47/5 (4投票s)
使用 Windows Azure Mobile Services 实现仓库模式,适用于 Windows Phone 应用和 Windows Store 应用。
引言
Windows Azure Mobile Services 是一个非常棒且易于使用的工具。 在本文中,我将向您展示如何使用仓库模式与 Mobile Services SDK。
如果您熟悉 Entity Framework,那么这种模式在许多项目和示例中都有使用。 您可以在这里找到定义:http://msdn.microsoft.com/en-us/library/ff649690.aspx 或在这里 https://codeproject.org.cn/Articles/526874/Repositorypluspattern-2cplusdoneplusright。
在本示例中,我们将使用可移植类库来与 Windows Phone、Windows 8 和 .NET 应用共享我们的数据访问层。
最终,我们将能够在我们的服务/视图模型中注入我们的仓库,并使我们的代码可测试。
Using the Code
首先,我们将使用可移植类库模板创建三个项目
MyNamespace.Models
MyNamespace.Data
MyNamespace.Data.MobileService
MyNamespace.Models
这个项目包含我们的数据模型。 它只是一个简单的类,用于描述我们的实体
public class ToDoItem
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
MyNamespace.Data
这个项目包含仓库的契约(接口)。
首先,我们将创建基本接口,称为 IRepository
。 它包含所有 CRUD 基本操作。
public interface IRepository<T>
{
Task<IEnumerable<T>> GetAllAsync();
Task CreateAsync(T entity);
Task UpdateAsync(T entity);
Task RemoveAsync(T entity);
}
如上所述,仓库模式需要为每个数据模型提供一个仓库接口。 这里是模型 ToDoItem
的契约:
public interface IToDoItemRepository : IRepository<ToDoItem>
{
}
我们将在接口中添加自定义查询(如果需要)。
MyNamespace.Data.MobileService
该项目包含我们接口的 Mobile Services 实现。
首先,您需要使用 nuget 将移动服务引用添加到您的项目。 我建议使用最新的预发布版本 (https://nuget.net.cn/packages/WindowsAzure.MobileServices/0.3.2-alpha)。
我们将创建一个基本仓库,其中包含所有 CRUD 基本操作。
public abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected IMobileServiceClient MobileServiceClient { get; set; }
protected BaseRepository(IMobileServiceClient mobileServiceClient)
{
if (mobileServiceClient == null) throw new ArgumentNullException("mobileServiceClient");
this.MobileServiceClient = mobileServiceClient;
}
protected virtual IMobileServiceTable<TEntity> GetTable()
{
return this.MobileServiceClient.GetTable<TEntity>();
}
public virtual Task<IEnumerable<TEntity>> GetAllAsync()
{
return GetTable().ToEnumerableAsync();
}
public virtual Task CreateAsync(TEntity entity)
{
return GetTable().InsertAsync(entity);
}
public virtual Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate)
{
return GetTable().Where(predicate).ToEnumerableAsync();
}
public virtual Task UpdateAsync(TEntity entity)
{
return GetTable().UpdateAsync(entity);
}
public virtual Task RemoveAsync(TEntity entity)
{
return GetTable().DeleteAsync(entity);
}
}
现在,我们准备编写 IToDoItemRepository
的实现。
public class ToDoItemRepository : BaseRepository<ToDoItem>, IToDoItemRepository
{
public ToDoItemRepository(IMobileServiceClient mobileServiceClient) : base(mobileServiceClient)
{
}
}
此实现是基础。 如果您有自定义查询,您可以简单地将您的方法添加到仓库中。
如何使用我的仓库?
我们的仓库模式现在已经实现,我们可以轻松地在 Windows Phone 项目中使用它。
在我的 WP 项目中,我非常喜欢使用 Caliburn.Micro 和 Autofac。 您可以添加 Caliburn.Autofac
的 nuget 引用。 现在您可以注册仓库和 MobileServiceClient
。
public class Bootstrapper : AutofacBootstrapper
{
protected override void ConfigureContainer(ContainerBuilder builder)
{
// register MobileServiceClient
builder.RegisterType<MobileServiceClient>()
.AsImplementedInterfaces()
.WithParameter("applicationUrl", "MyAppUrl")
.WithParameter("applicationKey", "MyAppKey")
.InstancePerLifetimeScope();
// register ToDoItemRepository
builder.RegisterType<ToDoItemRepository>().AsImplementedInterfaces().InstancePerLifetimeScope();
base.ConfigureContainer(builder);
}
}
您的仓库现在已注册,您可以在 ViewModels
中调用它们。
public class MainViewModel : Screen
{
private readonly IToDoItemRepository _toDoItemRepository;
private List<ToDoItem> _toDoItems;
public MainViewModel(IToDoItemRepository toDoItemRepository)
{
_toDoItemRepository = toDoItemRepository;
}
protected override void OnActivate()
{
LoadData();
}
private async void LoadData()
{
// create an entry for the test
await _toDoItemRepository.CreateAsync(new ToDoItem {Title = "my title",
Description = "my description"});
// load all data
ToDoItems = new List<ToDoItem>(await _toDoItemRepository.GetAllAsync());
}
public List<ToDoItem> ToDoItems
{
get { return _toDoItems; }
set
{
if (Equals(value, _toDoItems)) return;
_toDoItems = value;
NotifyOfPropertyChange(() => ToDoItems);
}
}
}
结论
就这样,使用 Azure Mobile Services 的仓库模式已经实现。 借助可移植类库,我们的数据访问层可以完全与 Win8 / .NET 应用共享。
在未来的文章中,我们将了解如何创建单元测试来测试视图模型。
尽情享用!