在 Silverlight 中保持控制和数据松散耦合?
我有单独的库(Controls.DLL)和我的自定义控件。 我有另一个库(Model.dll)和我的数据访问代码。
某些控件确实需要访问数据。我想让这些库保持松散耦合。基本上,我想在不引用 Model.dll 的情况下访问数据,
正确的方法是什么?当然,我认为 Binding 是一种可行的方法。但它不仅仅是绑定到数据,我还需要对我的模型执行操作(检索数据、分页、过滤)。我需要研究我的模型包含的元数据。
例如,我有一个具有“FirstName”、“LastName”等属性的 Customer 类。但我希望这些属性显示在我的控件内,并带有“First Name”、“Last Name”等标题。这是一个原始的例子,但它在某种程度上表明了我的观点。
我的另一个想法是在数据端拥有“提供者”来输出 XML,而在控制端我将解析此 XML。但我该如何去寻找方法呢?
另一个想法是反思。这样我就可以将对象传递给 Control。但我不太擅长反射,并且不确定我是否可以实现诸如获取属性/属性之类的目标。获取和执行方法?这听起来像是使用接口进行编码的完美方法,但接口需要存在于某个地方,因此某些东西必须引用某些东西。
那么,像这样松散地编写代码的最佳方法是什么?
I have separate library (Controls.DLL) with my custom Controls.
I have another library (Model.dll) with my data access code.
Some controls do need access to data. I'd like to keep those libraries loosely coupled. Basically, I'd like to acces data without referencing Model.dll
What is the proper way to do it? Naturally, I think Binding is a way to go. But it's not just binding to data, I also need to execute actions against my model (retreive data, paging, filtering). And I need to study metadata that my model contains.
For example, I have Customer class with properties like "FirstName", "LastName", etc. But I want those to be displayed inside my control with captions like "First Name", "Last Name". This is primitive example but it kind of shows my point.
My other idea was to have "providers" on data side that would spit out XML and on control side I will parse this XML. But how do I go about methods?
Another idea is to go with reflection. This way I would just pass object to Control. But I'm not as good with reflection and not sure if I can achieve things like: getting properties/attributes. Getting and executing methods? This sounds like a perfect thing to code with Interfaces but Interface need to live somewhere and therefore something have to reference something.
So, what is the best way to code loosely like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 MVVM 模式(模型-视图-视图模型)。
基本上,ViewModel 包装了 Model,并通过属性绑定向用户界面(View)公开数据访问和数据操作命令。
通过搜索 MVVM,您将找到大量文档、教程等。或者查看这个 StackOverflow 答案来帮助您入门。
更新:
MVVM 允许您分离自定义控件和数据。听起来您想根据模型中的数据动态生成界面组件。您可以在 MVVM 中执行此操作(当然,这不是唯一的方法)。视图模型可以根据模型动态生成集合。视图模型可以包括用于将原始数据转换为显示数据的方法。这意味着您的模型或控件(视图)都不需要知道如何
做这个。
根据数据的性质,您可以选择让“通用”视图模型反映属性名称,以按程序生成显示名称(如原始示例中所示),或者您可以选择为模型中的特定数据编写特定的视图模型。这将取决于您的数据的性质。无论哪种方式,您的自定义控件(在视图中)都与模型保持分离。
更新 2:
您的视图模型不需要与视图(控件)位于同一程序集中。您甚至可以将它们放入第三个程序集中(如所述这里)。当然,这鼓励您更严格地遵循 MVVM 并确保您没有从 ViewModel 到 View 的依赖关系,但这是一件好事。 此处提供了一些有关将视图连接到视图模型的相关问题的更多提示。
Check out the MVVM pattern (Model-View-ViewModel).
Basically, a ViewModel wraps a Model, and exposes data access and data manipulation commands to the user interface (the View) via property bindings.
You'll find heaps of documentation, tutorials etc. by searching for MVVM. Or check out this StackOverflow answer to get you started.
Update:
MVVM allows you to separate custom controls and data. It sounds like you want to dynamically generate interface components based upon data in your model. You can do this in MVVM (it's not the only way, of course). The view model can dynamically generate collections based upon the model. The view model can include methods for converting raw data to display data. This means neither your model or controls (view) need to know how to
do this.
Depending on the nature of your data, you may choose to have `generic' view models reflect over property names to procedurally generate display names (as in your primitive example), or you may choose to write specific view models for specific data in your model. That will depend upon the nature of your data. Either way, your custom controls (in the view) remain decoupled from the model.
Update 2:
Your view models do not need to live in the same assembly as the view (controls). You can even put them in a third assembly (as described here). Of course that encourages you to follow MVVM more strictly and make sure you have no dependencies from ViewModel to View, but that's a good thing. There are a few more hints on the issues related to hooking up views to view models you may encounter here.