如何将数据从 DB/XML 加载到 MVVM 中的模型
您好,我想知道如何正确地从 MVVM 中的数据库初始化模型。
我使用 Caliburn.Micro Framework,对于依赖项注入,我可以使用 MEF 或 Castle Windsor,作为 ORM,我可以使用 Entity Framework 或 NHibernate。
我有一些模型类,它可以是 POCO
public class SomeModel{}
我还有一些视图模型类,
public interface IViewModelA
{
ModelA SomeModel{get;set;}
}
public class ViewModelA : ScreenViewModel, IViewModelA
{
public ModelA SomeModel{get;set;}
}
我在视图上绑定 SomeModel 的属性。我认为这不是一个好的观点。
我想知道如何正确地将数据从存储库(XML、带有 ORM 的 DB)加载到模型。
在这里创建另一个类,例如 ModelManager init view models 属性?
这个示例是使用Caliburn + MEF
public interface IManager{}
[Export(typeof(IManager)]
public class Manager :Screen,IManager
{
//import dependencies with MEF
[Import]
public IViewModelA VMA{get;set;}
public void Init()
{
VMA.SomeModelA=//load from DB
}
}
或者什么是正确和常见的方法?
我希望看到一些来自现实世界的简单示例,其中包含 Caliburn.Micro + ORM(NHibernate/实体框架)+ IoC(MEF/Castle Windsor)。
谢谢
Hi I would like to know how correct initialize Model from DB in MVVM.
I use Caliburn.Micro Framework, for dependency injection I can use MEF or Castle Windsor and as ORM I can use Entity Framework or NHibernate.
I have some model class it can be POCO
public class SomeModel{}
Also I have some view model class
public interface IViewModelA
{
ModelA SomeModel{get;set;}
}
public class ViewModelA : ScreenViewModel, IViewModelA
{
public ModelA SomeModel{get;set;}
}
I bind properties of SomeModel on view. I think this is not good view.
I would like to know how correct load data from repository (XML, DB-with ORM) to Model.
Create some another class for example ModelManager init view models properties here?
This sample is with Caliburn + MEF
public interface IManager{}
[Export(typeof(IManager)]
public class Manager :Screen,IManager
{
//import dependencies with MEF
[Import]
public IViewModelA VMA{get;set;}
public void Init()
{
VMA.SomeModelA=//load from DB
}
}
Or what is correct and common way?
I would like see some simple sample from real world with Caliburn.Micro + ORM (NHibernate/ Entity Framework)+ IoC (MEF/Castle Windsor).
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
出色地。您需要查找有关 DDD 和存储库模式的信息。简而言之,存储库是您访问某种类型的数据的方式。例如,您可以:
您需要在 ViewModel 构造函数中传递一个 ICustomerRepository。
公共类 CustomerListViewModel
{
私有 ICustomerRepository 存储库 { 获取;放;您
应该使用服务 ICustomerRepository 到具体组件(例如 DbCustomerRepository)的映射来配置您的 IoC 容器,并将您的 CustomerListViewModel 注册为 self。
但这只是故事的开始。您需要将更改的数据存储在数据源(DB、XML 等)中。当然,您可以在存储库中添加一种方法来保存数据,但这违反了 SOLID 原则。最好使用 DDD 中的工作单元模式。在某些时候,您将需要自定义 Customer 来为显示方式添加一些附加属性和方法。填充 free 以创建围绕 Customer 的包装器 - CustomerViewModel。
类似的事情。
Well. You need to find information about DDD and Repository Pattern. In short a Repository is way you can access you data of some type. For instance you can have:
You need to pass in your ViewModel constructor an ICustomerRepository.
public class CustomerListViewModel
{
private ICustomerRepository Repository { get; set; }
You should configure your IoC-container with mapping of service ICustomerRepository to a concrete Component (for example DbCustomerRepository) as well as register your CustomerListViewModel as self.
But it is just a beginning of the story. You need to store your changed data in the data source (DB, XML etc.). Of course you can add a method on repository to save data but it is against S.O.L.I.D. principles. Better to use the Unit of Work pattern from DDD. In some point you will need to customize Customer to add some additional properties and methods for the display means. Fill free to create a wrapper around Customer - CustomerViewModel.
Something like that.