绑定EF4与Caliburn.Micro:我应该将我的实体公开为ViewModel的属性吗? [英] Binding EF4 with Caliburn.Micro: Should I expose my Entity as a property of the ViewModel?

查看:162
本文介绍了绑定EF4与Caliburn.Micro:我应该将我的实体公开为ViewModel的属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Caliburn.Micro我想知道将EF4实体暴露为ViewModel的一种属性的优缺点(一种技术讨论这里 和这里)。这允许我避免为每个字段写入getter和setter(请参见下面的OneCustomer)。缺点是我需要在XAML中写入所有的绑定语句(下面的LastName不在ViewModel中但是需要XAML绑定)。如果我坚持使用每个字段的属性填充我的ViewModel的规定技术(如下面的FirstName),我最终必须编写一吨额外的代码,所以NotifyOfProperyChange将被调用。应用程序将会相当大。

b
$ b

  private MyEntities _context = new MyEntities(); 
private BindableCollection< Customer> _custBindableCollection;
私人客户_oneCustomer;
private string _firstName;

public void Load()
{
_custBindableCollection = new BindableCollection< Customer>(_ context.Customers.Where(row => row.CustomerType ==FOO)) ;
AllCustomers = _custBindableCollection;
_oneCustomer = _custBindableCollection.FirstOrDefault();
FirstName = _oneCustomer.FirstName;
OneCustomer = _oneCustomer;
}

public BindableCollection< Customer> AllCustomers
{
get {return _custBindableCollection;}
set {_custBindableCollection = value;
NotifyOfPropertyChange(()=> AllCustomers);}
}

public Customer OneCustomer
{
get {return _oneCustomer;}
设置{_oneCustomer = value;
NotifyOfPropertyChange(()=> OneCustomer);}
}

public string FirstName
{
get {return _firstName; }
set {
_firstName = value;
_oneCustomer.FirstName = value;
NotifyOfPropertyChange(()=> FirstName);
NotifyOfPropertyChange(()=> CanSaveChanges);
}
}

public void SaveChanges()
{_context.SaveChanges(); }

public bool CanSaveChanges {get {return IsValid;

在我的视图中

 < StackPanel> 
< StackPanel Orientation =Horizo​​ntal>
< Label Content =名字:/>
< TextBox x:Name =FirstName/>
< / StackPanel>
< StackPanel Orientation =Horizo​​ntalDataContext ={Binding Path = OneCustomer}>
< Label Content =Last Name:/>
< TextBox x:Name =LastNameText ={Binding LastName}/>
< / StackPanel>
< Button Content =加载数据x:Name =Load/>
< Button Content =保存x:Name =SaveChanges/>
< DataGrid x:Name =AllCustomers/>



提前感谢。 p>

解决方案


与Caliburn.Micro我想知道曝光$ b $的优缺点b EF4实体作为ViewModel的属性(这里讨论的一种技巧
和这里)。


我不确定的利弊,但我可以告诉你两种方法都被使用。例如,采用简单的登录屏幕,通常我将UserName属性放在ViewModel上,但是在窗体更复杂的情况下,ViewModel可以聚合其他ViewModels(显示模型)来完成相同的操作。 CM不影响利益/利益,因为它更多的是使用MVVM的问题,什么是利弊。 CM将帮助您绑定两者。




  • 如果您在ViewModel上有一个名为CustomerName的属性,只需命名
    a TextBox x:name =CustomerName。

  • 如果您在ViewModel上有一个名为Customer的类
    的实例的属性,则将TextBox x:name = Customer_Name,再次,CM
    将处理绑定。



所以从你的xaml上面:

 < TextBox x:Name =LastNameText ={Binding LastName}/> 

您不需要在StackPanel上设置DataContext。而是:

 < TextBox x:Name =OneCustomer_LastName/> 

可以更轻松地绑定到DataForms和DataGrids的一个方法是,您可以创建一个显示模型,数据在屏幕上的显示方式。



这是我的意见,但我个人不会直接绑定到EF / Linq实体。相反,我将创建一个显示模型来表示该实体,以及我想要显示的实体,并使用 AutoMapper 进行映射。在很多情况下,它是一对一的映射。这似乎是浪费时间,但它具有优势,特别是对于更复杂的数据模型布局,显示模型允许您平坦化数据进行显示,并将属性归入验证,而不会将其粘贴到数据模型实体上。有关更多信息,请参阅 ASP.NET MVC在操作书籍中的章节。


With Caliburn.Micro I'd like to know the pros and cons of exposing an EF4 Entity as a property of the ViewModel (a technique discussed here and here). This allows me to avoid writing getters and setters for every field (see OneCustomer below). The drawback is I need to write all of the binding statements in XAML (below LastName is not in the ViewModel but does require XAML binding). If I stick to the prescribed technique of filling my ViewModel with properties for each field (as FirstName below) I'll ultimately have to write a ton of extra code just so NotifyOfProperyChange will get called. The application will be quite large. Should I expose each entity as a property of the ViewModel?

In My ViewModel:

private MyEntities _context = new MyEntities();
private BindableCollection<Customer> _custBindableCollection; 
private Customer _oneCustomer;
private string _firstName;

public void Load() 
{
    _custBindableCollection = new BindableCollection<Customer>(_context.Customers.Where(row => row.CustomerType == "FOO"));
    AllCustomers = _custBindableCollection;
    _oneCustomer = _custBindableCollection.FirstOrDefault();
    FirstName = _oneCustomer.FirstName;
    OneCustomer = _oneCustomer;
}

public BindableCollection<Customer> AllCustomers
{ 
get { return _custBindableCollection;}
set {_custBindableCollection = value;
      NotifyOfPropertyChange(() => AllCustomers);}
}

public Customer OneCustomer
{
 get { return _oneCustomer;}
 set { _oneCustomer = value;
        NotifyOfPropertyChange(() => OneCustomer);}
} 

public string FirstName
{
    get { return _firstName; }
    set {
        _firstName = value;
        _oneCustomer.FirstName = value;
        NotifyOfPropertyChange(() => FirstName);
        NotifyOfPropertyChange(() => CanSaveChanges);
    }
}

public void SaveChanges()
{ _context.SaveChanges(); }

public bool CanSaveChanges { get { return IsValid; } }

In My View:

<StackPanel>
<StackPanel Orientation="Horizontal">
    <Label Content="First Name:" />
    <TextBox x:Name="FirstName" />
</StackPanel>
<StackPanel Orientation="Horizontal" DataContext="{Binding Path=OneCustomer}">
    <Label Content="Last Name:" />
    <TextBox x:Name="LastName" Text="{Binding LastName}" />
</StackPanel>
<Button Content="Load Data" x:Name="Load" />
<Button Content="Save" x:Name="SaveChanges"  />
<DataGrid x:Name="AllCustomers" />

Thanks in advance.

解决方案

With Caliburn.Micro I'd like to know the pros and cons of exposing an EF4 Entity as a property of the ViewModel (a technique discussed here and here).

I'm not sure of the pros/cons but I can tell you both methods are used. For example take a simple Login Screen, generally I put the UserName a property on the ViewModel but in cases where the form is more complex the ViewModel can aggregate other ViewModels(display models) to accomplish the same thing. CM doesn't effect the pros/cons much as its more a question of With MVVM, what are the pros/cons. CM is going to help you in binding to both.

  • If you have a property on the ViewModel called CustomerName, simply name a TextBox x:name="CustomerName".
  • If you have a property on the ViewModel that is a instance of a class called Customer, name the TextBox x:name="Customer_Name" and again, CM will handle the bindings.

So from your xaml above:

 <TextBox x:Name="LastName" Text="{Binding LastName}" />

you don't need to set the DataContext on the StackPanel. Instead:

<TextBox x:Name="OneCustomer_LastName"/>

One thing that can make binding to DataForms and DataGrids easier is following a method where you create display models for the way the data is represented on the screen.

This is my opinion but I personally will never bind directly to an EF/Linq entity. Instead I'll create a display model to represent that entity and how I want it displayed and use AutoMapper to do the mappings. In a lot of cases its a one to one mapping. This might seem like a waste of time but it has advantages especially with more complex data model layouts, display models allow you to flatten the data for display purposes and attribute up properties for validation without sticking them on the data model entity. For more on that check out the chapter on it in ASP.NET MVC in Action book.

这篇关于绑定EF4与Caliburn.Micro:我应该将我的实体公开为ViewModel的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆