如何将数据从 DB/XML 加载到 MVVM 中的模型

发布于 2024-12-13 18:05:20 字数 1025 浏览 4 评论 0原文

您好,我想知道如何正确地从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2024-12-20 18:05:20

出色地。您需要查找有关 DDD 和存储库模式的信息。简而言之,存储库是您访问某种类型的数据的方式。例如,您可以:

public interface ICustomerRepository
{
    IEnumerable<Customer> GetCustomers();
    Customer GetCustomerById(Guid aId);
}

public class Customer
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public class DbCustomerRepository : ICustomerRepository
{
    private DbConnection Connection { get; set; }

    public DbCustomerRepository(DbConnection aConnection)
    {
        Connection = aConnection;
    }

    public IEnumerable<Customer> GetCustomers()
    {
        // Do your query to DB here.
        yield break;
    }

    public Customer GetCustomerById(Guid aId)
    {
        // Do your query to DB here.
        return null;
    }
}

public class XmlCustomerRepository : ICustomerRepository
{
    private string FilePath { get; set; }

    public XmlCustomerRepository(string aFilePath)
    {
        FilePath = aFilePath;
    }

    public IEnumerable<Customer> GetCustomers()
    {
        // Find all customers in a XML-file.
        yield break;
    }

    public Customer GetCustomerById(Guid aId)
    {
        // Find a specific customer in a XML-file.
        return null;
    }
}

您需要在 ViewModel 构造函数中传递一个 ICustomerRepository。
公共类 CustomerListViewModel
{
私有 ICustomerRepository 存储库 { 获取;放;您

    public CustomerListViewModel(ICustomerRepository aRepository)
    {
        Repository = aRepository;
    }

    public ObservableCollection<Customer> Customers
    {
        get
        {
            if (mCustomers == null)
            {
                IEnumerable<Customer> lCustomers = Repository.GetCustomers();
                mCustomers = new ObservableCollection<Customer>(lCustomers);

            }

            return mCustomers;
        }
    }

    private ObservableCollection<Customer> mCustomers;
}

应该使用服务 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:

public interface ICustomerRepository
{
    IEnumerable<Customer> GetCustomers();
    Customer GetCustomerById(Guid aId);
}

public class Customer
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public class DbCustomerRepository : ICustomerRepository
{
    private DbConnection Connection { get; set; }

    public DbCustomerRepository(DbConnection aConnection)
    {
        Connection = aConnection;
    }

    public IEnumerable<Customer> GetCustomers()
    {
        // Do your query to DB here.
        yield break;
    }

    public Customer GetCustomerById(Guid aId)
    {
        // Do your query to DB here.
        return null;
    }
}

public class XmlCustomerRepository : ICustomerRepository
{
    private string FilePath { get; set; }

    public XmlCustomerRepository(string aFilePath)
    {
        FilePath = aFilePath;
    }

    public IEnumerable<Customer> GetCustomers()
    {
        // Find all customers in a XML-file.
        yield break;
    }

    public Customer GetCustomerById(Guid aId)
    {
        // Find a specific customer in a XML-file.
        return null;
    }
}

You need to pass in your ViewModel constructor an ICustomerRepository.
public class CustomerListViewModel
{
private ICustomerRepository Repository { get; set; }

    public CustomerListViewModel(ICustomerRepository aRepository)
    {
        Repository = aRepository;
    }

    public ObservableCollection<Customer> Customers
    {
        get
        {
            if (mCustomers == null)
            {
                IEnumerable<Customer> lCustomers = Repository.GetCustomers();
                mCustomers = new ObservableCollection<Customer>(lCustomers);

            }

            return mCustomers;
        }
    }

    private ObservableCollection<Customer> mCustomers;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文