我想知道是否有人可以就以下问题提供一些建议。我们目前正在开发基于 RIA .NET 服务的 Silverlight 4 应用程序。应用程序中的一个屏幕允许用户输入搜索字符串,2 秒不活动后,请求将提交到我们的域服务。这一切都可以通过 Rx 很好地处理。
现在,在原始搜索返回之前可以执行第二次搜索。第二个请求也有可能在第一个请求之前返回。
实际上,我只是想找出人们使用什么模式和方法来管理对正确请求的正确响应。
您是否在请求中使用某种操作标识符?
您是否为每个请求创建域服务的新实例?
是否可以将请求的已完成事件与监视 textchange 事件的 Rx 可观察对象联系起来?
任何引导都会有帮助,
戴夫
I was wondering if someone could provide some advice on the following problem. We are currently developing a Silverlight 4 application based on RIA .NET Services. One of the screens in the application allows users to type in a search string and after 2 seconds of inactivity the request is submitted to our domain service. This is all handles nicely with Rx.
Now currently it is possible for a second search to be executed before the original has returned. Its also possible that the second request could return before the first.
Really I'm just trying to find out what patterns and approaches people are using to manage the correct response to the correct request.
Are you using some sort of operation identifier in your requests?
Are you creating new instances of your domain services for each request?
Is there away to tie the completed event of a request to the Rx observable monitoring the textchange event?
Any steer would be helpful really,
Dave
发布评论
评论(4)
你应该很容易解决这个问题。
如果我假设您有一个可观察的字符串来启动搜索,并且您有一个域服务,在给定字符串时返回一个
Result
对象,那么这就是您需要的代码:神奇之处在于
Switch
扩展方法,“切换”到从IObservable>
返回的最新可观察值 - 是的,它是一个嵌套的可观察值。当新的
searchText
传入时,查询将返回根据传入搜索文本创建的新IObservable
。然后,Switch
切换results
observable 以使用最新的 observable,并忽略任何先前创建的 observable。因此,结果是仅观察最新的搜索结果,而忽略任何先前的结果。
希望这是有道理的。 :-)
It should be quite easy for you to solve this problem.
If I assume you have an observable of string that initiates the search and that you have a domain service that returns a
Result
object when given the string then this is the kind of code you need:The magic is in the
Switch
extension method which "switches" to the latest observable returned from theIObservable<IObservable<Result>>
- yes, it is a nested observable.When a new
searchText
comes in, the query returns a newIObservable<Result>
created from the incoming search text. TheSwitch
then switches theresults
observable to use this latest observable and just ignores any previously created observables.So the result is that only the latest search results are observed and any previous results are ignored.
Hopefully that makes sense. :-)
Erik Meijer 在此解决了这个问题(大约 30 分钟后):http://channel9.msdn.com /活动/MIX/MIX10/FTL01
他在大约 36 分钟后解释了 Switch 的声明。
Erik Meijer addresses this here (after about 30 minutes): http://channel9.msdn.com/Events/MIX/MIX10/FTL01
He explains the Switch statement after about 36 minutes.
IMO 最简单的方法是为请求指定一个主题,在将任何请求分派到 WCF 之前通知您该主题。然后,不要订阅从已完成事件创建的可观察对象,而是订阅
CompletedEventObservable.TakeUntil(RequestsSubject)
。这样,您将永远不会收到对先前请求的响应的通知。The simples way IMO is to have a subject for requests that you notify before any request is dispatched to WCF. Then rather than subscribing to observable created from the completed event subscribe to
CompletedEventObservable.TakeUntil(RequestsSubject)
. This way you will never be notified with the response to the previous request.查看 rxx http://rxx.codeplex.com/
它有大量额外的东西可以帮助,特别是就你而言,我认为动态对象和可观察对象道具可能会让你的生活更轻松。
Check out rxx http://rxx.codeplex.com/
It has tons of extra stuff that will help, particularly in your case, I think Dynamic Objects and observable object props might be something that will make your life easier.