如何重构RIA数据服务调用到服务层?
我正在努力解决使用 RIA 服务和 API 的 Silverlight 4 应用程序中的一些基本 MVVM 设计原则。实体。下面是似乎可以正常工作的基本场景:
DataViewModel.cs
public DataViewModel : NotificationObject
private DataDomainContext _dataContext;
public DataViewModel()
{
_dataContext = new DataDomainContext();
if (!DesignerProperties.IsInDesignTool)
{
Data = _dataContext.Data;
dataContext.Load(_dataContext.GetDataQuery(), null, null);
}
}
private IEnumerable<DataEntity> _data;
public IEnumerable<DataEntity> Data // INPC property
{
get { return _data; }
set
{
if (value != _data)
{
_data = value;
PropertyChanged(this, new PropertyChangedEventArgs("Data"));
}
}
}
}
在我看来,DataGrid 单向绑定到 DataViewModel.Data,DataDomainContext 是在为该域编译域服务后公开的 RIA 域上下文。数据实体对象。
我想将视图模型与 DAL 解耦。我想要一个 DataService 类来负责向域上下文询问数据:
public DataViewModel : NotificationObject
private DataService _dataService;
public DataViewModel(DataService dataService)
{
_dataService = dataService;
if (!DesignerProperties.IsInDesignTool)
{
Data = _dataService.Data;
_dataService.GetData();
}
}
...
}
但是,我似乎无法正确处理。这很容易做到吗?我之前没有设计过带有回调的数据服务。我尝试通过 INPC 跨三个类链接数据属性,但 UI 中的 DataGrid 显示为空白。我还想转换为新类型 DataDto 的集合,因此我的表示层不耦合到后端。我尝试了这样的事情,但没有运气:
DataService.cs
public DataService : INotifyPropertyChanged
{
public DataService()
{
_dataContext = new DataDomainContext();
}
public event PropertyChangedEventHandler PropertyChanged;
public void GetData()
{
DataEntities = _domainContext.Data;
_dataContext.Load(_dataContext.GetDataQuery(), FinishedLoading, null);
}
private void FinishedLoading(...)
{
Data = DataEntities.Select(de => new DataDto(de));
}
public IEnumerable<DataDto> Data { ... } // INPC property, used for binding in ViewModel
public IEnumerable<DataEntity> DataEntities { ... } // INPC property
...
}
我在这里是否走在正确的轨道上?我是否错过了高层的任何内容,或者我只是没有掌握正确的细节?
编辑:
我最终能够弄清楚这一点。答案涉及通过 Action<> 将回调传递到数据服务/存储库调用中。调用的返回类型实际上是 void,事件参数用于传递结果。如果有人感兴趣,我很乐意发布一些工作代码,只需在评论中留下请求即可。
I'm struggling with some basic MVVM design principles in a Silverlight 4 app using RIA services & entities. Here's the basic scenario that seems to work OK:
DataViewModel.cs
public DataViewModel : NotificationObject
private DataDomainContext _dataContext;
public DataViewModel()
{
_dataContext = new DataDomainContext();
if (!DesignerProperties.IsInDesignTool)
{
Data = _dataContext.Data;
dataContext.Load(_dataContext.GetDataQuery(), null, null);
}
}
private IEnumerable<DataEntity> _data;
public IEnumerable<DataEntity> Data // INPC property
{
get { return _data; }
set
{
if (value != _data)
{
_data = value;
PropertyChanged(this, new PropertyChangedEventArgs("Data"));
}
}
}
}
A DataGrid in my view is bound one-way to DataViewModel.Data, and DataDomainContext is the RIA domain context exposed after compiling a domain service for the DataEntity object.
I want to decouple the view model from the DAL. I'd like a DataService class that will take care of asking the domain context for data:
public DataViewModel : NotificationObject
private DataService _dataService;
public DataViewModel(DataService dataService)
{
_dataService = dataService;
if (!DesignerProperties.IsInDesignTool)
{
Data = _dataService.Data;
_dataService.GetData();
}
}
...
}
However, I can't seem to get it right. Is this easily done? I haven't designed a data service with callbacks before. I tried chaining the Data properties via INPC across the three classes, but the DataGrid in the UI comes up blank. I'd also like to convert to a collection of a new type, DataDto, so my presentation layer is not coupled to the backend. I tried something like this without luck:
DataService.cs
public DataService : INotifyPropertyChanged
{
public DataService()
{
_dataContext = new DataDomainContext();
}
public event PropertyChangedEventHandler PropertyChanged;
public void GetData()
{
DataEntities = _domainContext.Data;
_dataContext.Load(_dataContext.GetDataQuery(), FinishedLoading, null);
}
private void FinishedLoading(...)
{
Data = DataEntities.Select(de => new DataDto(de));
}
public IEnumerable<DataDto> Data { ... } // INPC property, used for binding in ViewModel
public IEnumerable<DataEntity> DataEntities { ... } // INPC property
...
}
Am I even on the right track here? Am I missing anything from a high level or do I just not have the details right?
Edit:
I was able to eventually figure this out. The answer involves passing a callback into the data service/repository call via an Action<>. The return type of the call is actually void, and event args are used to deliver the results. I'm happy to post some working code if anyone is interested, just leave a request in the comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的方向是正确的,但在我看来如果您实际上试图将视图模型与数据服务分离,那么您的解决方案是不正确的。我现在正在开发一个与此非常相似的应用程序。不同的人对 mvvm 有不同的想法,这只是我从反复试验(使用 Visual Studio)中学到的个人方法:
首先创建 silverlight 应用程序项目并将其托管在 .web 项目中。
silverlight 项目将保存视图和视图模型。视图模型应该包含您的模型,而不是您的数据服务!但是,视图模型应该有数据服务的实例来设置模型。我的数据服务在哪里?我很高兴你问:)。添加另一个项目,即 WCF RIA 服务类库。这实际上是两个项目,一个 ria 服务(服务器端)dll 和一个相应的 silverlight(客户端)dll。您可以将实体框架或其他数据库访问代码添加到服务器端。之后,将域服务添加到服务器端项目中。首先构建它(重要),然后转到客户端 ria 服务 dll,并使用数据服务的方法创建一个数据服务类,如下所示:
您的数据服务不应该实现 Inotifyproperty 更改,因为这是视图模型的角色!
在您的 silverlight 项目中引用 ria 服务客户端 dll,并在您的 Web 主机项目中引用 ria 服务服务器端 dll
视图模型应该像这样调用这个数据服务:
如果您确实想将它们解耦,您可以更进一步并为数据服务实现一个接口。采用这种方法可以重复使用您的数据服务(如果您需要桌面应用程序或手机应用程序),我希望这有助于解决问题!
I think you're on the right track, but in my opinion your solution is not correct if you are, in fact, trying to decouple your view model from your data service. I am working on an app very similar to this right now. Different people have different ideas about mvvm, and this is just my personal approach that I have learned from trial and error (using visual studio):
Start off by creating silverlight app project and hosting it in a .web project.
The silverlight project will hold the views and view models. The view models should contain your models, not you're data service! However, the view models should have an instance of your data service to set the models. Where is my data service? I'm glad you asked :). Add another project, a WCF RIA Services Class Library. That is actually two projects, a ria service (server side) dll and a corresponding silverlight (client side) dll. You can add your entity framework or other database access code to the server side. After that, add a domain service to the server side project. Build it first (important) and then go to the client side ria service dll and create a data service class with your methods for the data service like so:
Your data service should not implement Inotifyproperty changed because that is the view models role!
Reference the ria service client side dll in your silverlight project, and also reference the ria service server side dll in your web host project
the view model should call this data service like so:
You can take it a step further and implement an interface for the data service if you really want to decouple them. Taking this approach allows for re-usability of your data service (in case you want a desktop app or phone app) I hope this helps clear things up!