【发布时间】:2011-06-09 13:48:35
【问题描述】:
我们目前正在设计一个使用 WPF 的系统,该系统将使用 Web 服务与其他系统进行通信。
我们正在尝试尽可能少的层和映射(通过坚持最简单可行的方法来降低成本)。
我们将使用 MVVM 模式。
所以我们必须有一个视图模型。
问题是我们可以使用从服务返回的对象作为模型对象,还是应该将它们映射到客户端中定义的模型对象?
【问题讨论】:
标签: wpf web-services architecture mvvm
我们目前正在设计一个使用 WPF 的系统,该系统将使用 Web 服务与其他系统进行通信。
我们正在尝试尽可能少的层和映射(通过坚持最简单可行的方法来降低成本)。
我们将使用 MVVM 模式。
所以我们必须有一个视图模型。
问题是我们可以使用从服务返回的对象作为模型对象,还是应该将它们映射到客户端中定义的模型对象?
【问题讨论】:
标签: wpf web-services architecture mvvm
虽然您可以在模型中使用从服务返回的数据对象,但有几个原因可以避免这种情况:
您应该考虑在模型中使用 Repository Pattern 将您的服务通信封装为一个或多个 Web 服务存储库。 Web 服务存储库允许您集中服务的访问逻辑,并为单元测试提供替代点,同时让您有机会缓存先前服务操作的结果。
存储库充当不同域中的数据和操作之间的桥梁。存储库向数据源发出适当的查询,然后将结果集映射到业务实体,通常使用Data Mapper 模式在表示之间进行转换。
您的视图模型将使用服务存储库来检索或保留信息,存储库处理对服务的调用以及将数据的服务表示映射到您最终将数据绑定到的模型特定类。
您可以更进一步,为服务存储库定义通用接口,使您可以围绕应用程序可能正在执行的基于 CRUD 的操作实现服务特定的存储库。
通用服务存储库接口示例:
/// <summary>
/// Describes a service repository that separates the logic that retrieves, persists and maps data to the
/// domain model from the business logic that acts on the domain model.
/// </summary>
/// <typeparam name="TChannel">The type of channel produced by the channel factory used by the repository.</typeparam>
/// <typeparam name="TMessage">The type of data contract to map to the domain entity of type <typeparamref name="T"/>.</typeparam>
/// <typeparam name="T">The type of domain entity mediated by the repository.</typeparam>
/// <typeparam name="TKey">The type of the key that uniquely identifies domain entities within the repository.</typeparam>
public interface IServiceRepository<TChannel, TMessage, T, TKey> : IRepository<T, TKey>
where T : class
{
/// <summary>
/// Occurs when the repository transitions from one state to another.
/// </summary>
event EventHandler<StateChangedEventArgs> StateChanged;
/// <summary>
/// Gets the configuration name used for the service endpoint.
/// </summary>
/// <value>
/// The name of the endpoint in the application configuration file that is used
/// to create a channel to the service endpoint.
/// </value>
string EndpointConfigurationName
{
get;
}
/// <summary>
/// Gets the current state of the service repository.
/// </summary>
/// <value>
/// The current <see cref="CommunicationState"/> of the service repository.
/// </value>
CommunicationState State
{
get;
}
}
通用存储库接口示例:
/// <summary>
/// Describes a repository that separates the logic that retrieves, persists and maps data to the domain model
/// from the business logic that acts on the domain model.
/// </summary>
/// <typeparam name="T">The type of domain entity mediated by the repository.</typeparam>
/// <typeparam name="TKey">The type of the key that uniquely identifies domain entities within the repository.</typeparam>
public interface IRepository<T, TKey>
where T : class
{
/// <summary>
/// Occurs when a repository action has been completed.
/// </summary>
event EventHandler<RepositoryActionCompletedEventArgs<T>> Completed;
/// <summary>
/// Occurs when a repository action fails to execute.
/// </summary>
event EventHandler<RepositoryActionFailedEventArgs<T>> Failed;
/// <summary>
/// Gets a value indicating if the repository has been disposed of.
/// </summary>
/// <value>
/// <see langword="true" /> if the repository has been disposed of; otherwise, <see langword="false" />.
/// </value>
bool IsDisposed
{
get;
}
/// <summary>
/// Adds a new <paramref name="entity"/> to the data source layer.
/// </summary>
/// <param name="entity">The entity of type <typeparamref name="T"/> to insert into the data source layer.</param>
/// <param name="callback">
/// The optional <see langword="delegate"/> method that will be executed after the <paramref name="entity"/> is insert into the data source layer.
/// </param>
/// <returns>
/// The <see cref="IRepository{T, Tkey}"/> object that this method was called on.
/// </returns>
/// <exception cref="ArgumentNullException">The <paramref name="entity"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</exception>
IRepository<T, TKey> Add(T entity, Action<T, Exception> callback = null);
/// <summary>
/// Retrieves all entities of type <typeparamref name="T"/> from the data source layer.
/// </summary>
/// <param name="callback">
/// The optional <see langword="delegate"/> method that will be executed after all entities of type <typeparamref name="T"/> are retrieved from the data source layer.
/// </param>
/// <returns>
/// The <see cref="IRepository{T, Tkey}"/> object that this method was called on.
/// </returns>
IRepository<T, TKey> Get(Action<IEnumerable<T>, Exception> callback = null);
/// <summary>
/// Retrieves an entity of type <typeparamref name="T"/> from the data source layer that
/// matches the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The unique identifier of the entity of type <typeparamref name="T"/> to retrieve from the data source layer.</param>
/// <param name="callback">
/// The optional <see langword="delegate"/> method that will be executed after an entity of type <typeparamref name="T"/> that matches the specified <paramref name="key"/> is retrieved from the data source layer.
/// </param>
/// <returns>
/// The <see cref="IRepository{T, Tkey}"/> object that this method was called on.
/// </returns>
IRepository<T, TKey> Get(TKey key, Action<T, Exception> callback = null);
/// <summary>
/// Removes an existing <paramref name="entity"/> from the data source layer.
/// </summary>
/// <param name="entity">An entity of type <typeparamref name="T"/> to delete from the data source layer.</param>
/// <param name="callback">
/// The optional <see langword="delegate"/> method that will be executed after the <paramref name="entity"/> is removed from the data source layer.
/// </param>
/// <returns>
/// The <see cref="IRepository{T, Tkey}"/> object that this method was called on.
/// </returns>
/// <exception cref="ArgumentNullException">The <paramref name="entity"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</exception>
IRepository<T, TKey> Remove(T entity, Action<T, Exception> callback = null);
/// <summary>
/// Updates an existing <paramref name="entity"/> within the data source layer.
/// </summary>
/// <param name="entity">The entity of type <typeparamref name="T"/> to update within the data source layer.</param>
/// <param name="callback">
/// The optional <see langword="delegate"/> method that will be executed after the <paramref name="entity"/> is updated within the data source layer.
/// </param>
/// <returns>
/// The <see cref="IRepository{T, Tkey}"/> object that this method was called on.
/// </returns>
/// <exception cref="ArgumentNullException">The <paramref name="entity"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</exception>
IRepository<T, TKey> Update(T entity, Action<T, Exception> callback = null);
}
【讨论】:
您不必创建自己的模型层,但是:如果服务发生更改,您将必须在引用该模型的所有层中反映该更改。如果你现在创建一个自己的模型层,你会更安全,但它的工作量更大。像往常一样:更多的工作最终会为你节省更多的工作。如果您拥有该服务并且您确定永远不会更改它(哈哈),您就不需要模型层。
当然,这取决于服务检索的对象是否完全符合您的需求。如果您使用大型对象的 1、2 个属性,它可能都不是一个很好的选择...
【讨论】:
是的,您可以使用 WCF 服务对象作为您的模型层(我们是),尽管就像 @fantasticfix 所说,如果您的 WCF 服务器对象发生更改,您必须寻找引用来修复它们。
另外,如果您要将整个模型暴露给视图并绑定到模型的属性,那么您需要确保 WCF 服务器对象实现 INotifyPropertyChanged。
【讨论】:
我将 MVVM 用于没有服务的 WPF 应用程序,并且使用来自同一解决方案的内存中 dll 服务检索模型。在这种获取模型的服务完全由我们控制的环境中,没有任何额外的层是有意义的。所以我们只有模型(由内存中的 dll 服务获取)、ViewModel 对象和视图。
但是,如果您使用的是 Web 服务,我认为该服务将独立于其使用而发展。如果您的服务合同明天发生变化,您将不希望您的应用程序中断或产生大量维护成本。因此,最佳实践是使用 WCF 模型在 UI 端重新创建您自己的模型,然后对其进行处理。因此,如果明天合约发生更改,更改很可能是围绕将 WCF 模型映射到您的 UI 模型的逻辑,而不是超越。这个额外的层是值得的。
如果您的服务在您的控制之下并且您的应用程序足够简单,您可以尝试使用服务模型作为 MVVM 模型。
不要尝试将 ViewModel 和 Model 合并为一个(通过在模型本身中实现 INotifyPropertyChange)。我试过这个,随着应用程序复杂性的增加,它很快就会变得一团糟。此外,这样您的模型将因所有命令实现而膨胀。它们应该只包含业务逻辑,而不要承担过多的 UI 逻辑责任。
【讨论】: