重新构建对象模型以适应异步数据获取
我有一组与某些现实世界相对应的建模对象 概念。
TradeDrug、GenericDrug、TradePackage、DrugForm
我试图提供的简单对象模型的基础是一个复杂的 使用数字代码表示关系的医学术语 和概念,都可以通过 REST 服务访问 - 我正在尝试 使用对象包装器隐藏一些复杂性。
举一个具体的例子,
我可以调用
TradeDrug d = Searcher.FindTradeDrug("Zoloft") or
TradeDrug d = new TradeDrug(34)
其中 34 可能是 Zoloft 的代码。这将咨询远程 服务器查找有关 Zoloft 的一些详细信息。然后我可能会打电话
GenericDrug generic = d.EquivalentGeneric()
System.Out.WriteLine(generic.ActiveIngredient().Name)
来取回仿制药舍曲林作为一个对象(再次通过 对拥有所有这些药物的远程服务器的后台 REST 调用 详细信息),然后也许可以找到它的成分。
该模型运行良好并且正在某些应用中使用 涉及数据处理。
然而最近我想做 使用并显示这些的 silverlight 应用程序 对象。 silverlight环境只允许异步 REST/Web 服务调用。我对如何制作没有任何问题 异步调用 - 但我遇到了问题 设计应该适合我的对象构造。
目前我的对象的构造函数做了一些 REST 同步调用。
public TradeDrug(int code)
{
form = restclient.FetchForm(code)
name = restclient.FetchName(code)
etc..
}
如果我必须使用异步“事件”或“操作”才能使用 Silverlight Web 客户端(我知道 silverlight 可以 被迫成为同步客户端,但我感兴趣 异步方法),有人有指导吗 或如何构建我的对象的最佳实践。
我可以将操作回调传递给构造函数
public TradeDrug(int code, Action<TradeDrug> constructCompleted)
{
}
,但这允许用户拥有 TradeDrug 在我想要构造的对象之前的对象实例 实际上已经完成了。它也不支持 “事件”异步模式,因为对象不存在 添加事件直到它被构造。
扩展该方法可能是一个工厂对象本身 具有对象的异步接口
factory.GetTradeDrugAsync(code, completedaction)
或具有 GetTradeDrugCompleted 事件吗?
有人有什么建议吗?有谁知道吗 新的反应式框架如何适应任何 解决方案?
I have a modeled a set of objects that correspond with some real world
concepts.
TradeDrug, GenericDrug, TradePackage, DrugForm
Underlying the simple object model I am trying to provide is a complex
medical terminology that uses numeric codes to represent relationships
and concepts, all accessible via a REST service - I am trying to
hide away some of that complexity with an object wrapper.
To give a concrete example
I can call
TradeDrug d = Searcher.FindTradeDrug("Zoloft") or
TradeDrug d = new TradeDrug(34)
where 34 might be the code for Zoloft. This will consult a remote
server to find out some details about Zoloft. I might then call
GenericDrug generic = d.EquivalentGeneric()
System.Out.WriteLine(generic.ActiveIngredient().Name)
in order to get back the generic drug sertraline as an object (again via a
background REST call to the remote server that has all these drug
details), and then perhaps find its ingredient.
This model works fine and is being used in some applications
that involve data processing.
Recently however I wanted to do
a silverlight application that used and displayed these
objects. The silverlight environment only allows asynchronous
REST/web service calls. I have no problems with how to make the
asychhronous calls - but I am having trouble with what
the design should be for my object construction.
Currently the constructors for my objects do some
REST calls sychronously.
public TradeDrug(int code)
{
form = restclient.FetchForm(code)
name = restclient.FetchName(code)
etc..
}
If I have to use async 'events' or 'actions' in order to use
the Silverlight web client (I know silverlight can be
forced to be a synchronous client but I am interested in
asychronous approaches), does anyone have an guidance
or best practice for how to structure my objects.
I can pass in an action callback to the constructor
public TradeDrug(int code, Action<TradeDrug> constructCompleted)
{
}
but this then allows the user to have a TradeDrug
object instance before what I want to construct
is actually finished. It also doesn't support an
'event' async pattern because the object doesn't exist
to add the event to until it is constructed.
Extending that approach might be a factory object that itself
has an asynchronous interface to objects
factory.GetTradeDrugAsync(code, completedaction)
or with a GetTradeDrugCompleted event?
Does anyone have any recommendations? Does anyone know
how the new Reactive framework might fit in with any
solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会放弃公共构造函数并使用异步工厂方法。它不需要是一个单独的工厂对象,它可以只是 TradeDrug 本身的静态方法,类似于:
基于事件的异步模式描述如下:http://msdn.microsoft.com/en-us/library/wewwczdw.aspx
I would ditch the public constructors and go with async factory methods. It need not be a separate factory object, it could just be static methods on TradeDrug itself, something like:
The event-based async pattern is described here: http://msdn.microsoft.com/en-us/library/wewwczdw.aspx