2014-01-14 50 views
1

我與Catel,MVVM,WPF的工作,我想知道如何與嵌套/ hiearchical的數據。hiearchical數據,catel和MVVM

讓我們從我有一個客戶列表,每個發票的列表,每個InvoiceItems列表數據庫說。客戶擁有許多擁有許多InvoiceItems的發票。

我有一個工作的解決方案,但我不喜歡它。我的方法是構建一個類似ado.net「數據集」的類的集合。一個類將代表這個hiearchy的每一層。

這個頂層類,CustomerModel,將包含的InvoiceBlocks的集合:

CustomerModel
的ObservableCollection的< InvoicesBlocks>

每個InvoceBlock將包含發票和InvoiceItems的集合:

InvoiceBlock
Invoice
ObservableCollection < InvoiceItems>

在涉及數據綁定路徑= satements之前,它似乎很聰明。有時候我不得不通過集合來更新總數,從而擊敗了MVVM的一個主要賣點。

所以,我決定了解更多有關LINQ查詢和數據綁定分組。這是專業人士的做法嗎?

回答

0

你可以做的是使每個視圖模型負責使用正確的服務來檢索數據。我沒有使用Catel性能

注意,可以很容易理解,但你可以簡單地使用Catel.Fody或重寫屬性來獲取Catel性能。

public class CustomerViewModel 
{ 
    private readonly IInvoiceService _invoiceService; 

    public CustomerViewModel(ICustomer customer, IInvoiceService invoiceService) 
    { 
     Argument.IsNotNull(() => customer); 
     Argument.IsNotNull(() => invoiceService); 

     Customer = customer; 
     _invoiceService = invoiceService; 
    } 

    public ICustomer Customer { get; private set; } 

    public ObservableCollection<IInvoice> Invoices { get; private set; } 

    protected override void Initialize() 
    { 
     var customerInvoices = _invoiceService.GetInvoicesForCustomer(Customer.Id); 
     Invoices = new ObservableCollection<IInvoice>(customerInvoices); 
    } 
} 


public class InvoiceViewModel 
{ 
    private readonly IInvoiceService _invoiceService; 

    public InvoiceViewModel(IIinvoice invoice, IInvoiceService invoiceService) 
    { 
     Argument.IsNotNull(() => invoice); 
     Argument.IsNotNull(() => invoiceService); 

     Invoice = invoice; 
     _invoiceService = invoiceService; 
    } 

    public IInvoice Invoice { get; private set; } 

    public ObservableCollection<IInvoiceBlock> InvoiceBlocks { get; private set; } 

    protected override void Initialize() 
    { 
     var invoiceBlocks = _invoiceService.GetInvoiceBlocksForInvoice(Invoice.Id); 
     InvoiceBlocks = new ObservableCollection<IInvoiceBlock>(invoiceBlocks); 
    } 
} 

現在你完全控制時會發生什麼。

+0

我使用ADO.NET實體模型:使用(VAR =新DataEntity()),我應該試着爲創建一個服務,或者直接從調用它,說一個InvoiceService?謝謝。 –

+0

明白了!我將ADO.NET實體模型DataEntities()放入它自己的服務中。甜。 –