Unit of Work

Unit of Work

Old forum URL: forums.lhotka.net/forums/t/9863.aspx


gjbrown posted on Sunday, December 12, 2010

The scenrio is this. CSLA 3.8.4, VS 2008 and MVVM.  I have a simple control that uses a Unit Of Work to get a BusinessBase and a NVL List. This works well up to a point. You can bind to the NVL or the Business Base, but it is a bit different then binding to the Model Property.  The Model implements a couple of useful properties such as CanSave and CanCancel.  Is there anyway to do this using Unit of Work. My Code is below.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

[

Serializable]

 

public class VerifyUoW : ReadOnlyBase<VerifyUoW>{

 

private static PropertyInfo<Loan> LoanProperty = RegisterProperty<Loan>(p => p.Loan);

public Loan Loan { get { return GetProperty(LoanProperty); } }

 

 

private static PropertyInfo<CodeExceptionList> CodeExceptionListProperty = gisterProperty<CodeExceptionList>(p => p.CodeExceptionList);

 

 

public CodeExceptionList

CodeExceptionList { get { return GetProperty(CodeExceptionListProperty); } }

 

 

public static void GetVerifyUoW(EventHandler<DataPortalResult<VerifyUoW>> handler)

{

 

 

DataPortal<VerifyUoW> dp = new DataPortal<VerifyUoW>();

dp.FetchCompleted += handler;

dp.BeginFetch();

}

#if

 

!SILVERLIGHT

 

 

private void

DataPortal_Fetch()

{

LoadProperty(LoanProperty,

Loan.NewLoan());

LoadProperty(CodeExceptionListProperty,

CodeExceptionList.GetCodeExceptionList());

}

#endif

}

RockfordLhotka replied on Sunday, December 12, 2010

I use two ViewModel<T> objects/elements. One for the root (UOW), and the second is bound to the first (to set its Model property to the editable object), and manages the editable root object. Most of my UI then binds to this second ViewModel object - eliminating the complexity.

Peran replied on Monday, December 13, 2010

I took a slightly different approach and extended ViewModelBase<T> to create ViewModelBase<T, Uow>; with a UnitOfWork property and associated  BeginRefreshUow method.

The view model can decide when to call BeginRefresh or BeginRefreshUow dependent on how much data you want to pull from the server.  In OnUnitOfWorkChanged I set the properties of the view model from the values returned by the UOW ensuring the NVL's are set first as UI combo boxes work best when the drop down list is bound before the model is bound.

 

        protected override void OnUnitOfWorkChanged(Uow oldValue, Uow newValue)
        {
            this.SomeNVL = newValue.SomeNVL;
            this.Model = newValue.SomeEditableObject;
        }

 

Peran

 

gjbrown replied on Tuesday, December 14, 2010

Thanks to Rocky and Peran for repying to my post.  Very useful info in Both posts.

The final solution for those interested is

(1) -  UnitOfWork Class Remains unchanged

(2) - Added the following to LoanVerifyViewModel Class

 public static readonly DependencyProperty

LoanViewModelProperty = DependencyProperty.Register("LoanViewModel", typeof(LoanViewModel), typeof(LoanVerifyViewModel), null);

 

 

public LoanViewModel

LoanViewModel{

get { return (LoanViewModel)GetValue(LoanViewModelProperty); }

 

 

set { SetValue(LoanViewModelProperty, value

); }

 

 

 

protected override void OnModelChanged(VerifyUoW oldValue, VerifyUoW newValue)

{

 

 

 

base

.OnModelChanged(oldValue, newValue);

LoanViewModel =

 

new LoanViewModel

(newValue.Loan);

(3) Added New ViewModel

public class LoanViewModel : DisburseViewModel<Loan>{

 

 

 

 

 

 public LoanViewModel(Loan Loan)

{

Model = Loan;

}}

 

 

 (4) Updated XAML

- Changed to point to New LoanViewModelProperty

- Change DataContext for Save and Cancel triggers to Point to New ViewModel

 

 

 

 

 

 

Copyright (c) Marimer LLC