Caliburn.Microでは、EF4エンティティをViewModelのプロパティ(公開されている技術hereand here)として公開することの長所と短所を知りたいと思います。これにより、すべてのフィールドにgetterとsetterを書くのを避けることができます(下記のOneCustomerを参照してください)。欠点は、すべてのバインディングステートメントをXAMLに書き込む必要があることです(LastNameはViewModelにはありませんが、XAMLバインディングが必要です)。私がViewModelに各フィールドのプロパティ(下のFirstNameとして)を埋め込むという所定のテクニックに固執すれば、私は最終的にNotifyOfProperyChangeが呼び出されるように1トンの余分なコードを書く必要があります。アプリケーションはかなり大きくなります。 各エンティティをViewModelのプロパティとして公開する必要がありますか?私の見解ではCaliburn.MicroでEF4をバインドする:自分のエンティティを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; } }
:私のViewModelで
事前に
<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" />
感謝。
これは素晴らしい情報です。特に、アンダースコアをドット表記と解釈するCMの慣行。私は間違いなくAutoMapperとMVCの本をチェックします。しかし、1つの小さな質問...セッターで(値に)エンティティプロパティを更新する必要がありますか、または保存をクリックして一度にすべてを更新するまで待つ必要がありますか?再度、感謝します。 – DeveloperDan
プロパティの値が変更されたときと保存したときのどちらを比較してデータベースに保存するかを尋ねていますか? –
いいえ、私は救いを続けるつもりです。私が考えていたことは、エンティティを公開しないと、すべてのエンティティプロパティを更新するために保存まで待つことができるということです。しかし、今は私が考えることは意味がありません。なぜなら、1つだけが変更されたとしてもすべての値を更新する必要があるからです。だから、私自身の質問に答えました - 私はセッターにエンティティのプロパティ/フィールドの更新を保持します。 – DeveloperDan