Hi Rodrigo,
The Model-View-ViewModel or Presentation Model as Fowler calls it is in my
eyes a special case of the Model-View-Presenter pattern focusing even more
on the data binding and commands. When using the MVP pattern your presenter
communicates with the view through interfaces. The view creates a presenter
and passes in a reference to the view interface. For instance you may have
an ICustomerView, a CustomerPresenter and a concrete WebCustomerView. This
would look something like this:
public interface ICustomerView
{
string Name { get; set; }
}
public class CustomerPresenter
{
private ICustomerView view;
public CustomerPresenter(ICustomerView view)
{
this.view = view;
}
public void LoadCustomer()
{
view.Name = "My Customer!";
}
public void SaveCustomer()
{
string value = view.Name;
// Save to DB
}
}
public class WebCustomerView : ICustomerView
{
private CustomerPresenter presenter;
public string Name
{
get { return txtName.Text; }
set { txtName.Text = value; }
}
public WebCustomerView()
{
presenter = new CustomerPresenter(this);
}
public void Page_Load(object sender, EventArgs e)
{
presenter.LoadCustomer();
}
public void btnSave_Click(object sender, EventArgs e)
{
presenter.SaveCustomer();
}
}
As you can see the concrete view implementation listen to UI-events and call
methods on the presenter. The presenter then talks to the Model (database,
web service or what ever) and gets thata and updates the view. The bennefit
of this pattern is that it's "UI agnostic", meaning that the thing that
couples your presenter to your View is the View-interface. So the MVP
framework would work nicely in both Windows Forms, WPF, Silverlight and
APS.NET applications...
The draw-back is that you need modre code-behind in your XAML files to
implement the IView-interface. WPF and Silverlight has fantastic databinding
support, and patterns like ViewModel/PresentationModel really lets you take
advantage of that. The Command-Patern lets you lously couple UI-events (like
buttons beeing pressed) with the subscriber (the ViewModel). The benefit of
having slim (or no) code-behind your XAML files is that it makes it easier
for designers to be creative and really "own" the UI experience. Once you
have alot of code you're view interface implementation is bound to the
concrete UI widgets (textboxes, checkboxes etc), and changing the UI in XAML
would require to change some C# code as well.
If you're about to start on a new WPF project I would recommend checking out
Prism (now known as Composite WPF - http://www.codeplex.com/CompositeWPF).
For an easy-to-follow introduction to MVP check out
http://msdn.microsoft.com/en-gb/magazine/cc188690.aspx - It's using Windows
Forms and ASP.NET for its samples.
Cheers,
Jonas Follesø
On Fri, Aug 8, 2008 at 12:53 PM, Rodrigo Ratan <[EMAIL PROTECTED]>wrote:
> Hi Jonas,
>
> I'm working in a team of a WPF project and we're exploring the advantages
> of MVP in our project. Our main goal is to have our application logic
> easily-compatible with other UI technologies like Silverlight and ASP.net
> for Web or any other "view" that we would want to use.
> Do you think that M-V-VM (Presentation model) should be used in this
> scenario?
>
> By the way, Can anyone recommend a good focused WPF mail-list?
>
> Thanks in advance,
>
> []s! Rodrigo Ratan
>
>
> On 7/24/08, Jonas Follesø <[EMAIL PROTECTED]> wrote:
>
>> Hi guys,
>>
>> I normally don't "pimp" my blog on the mailing list, but have two posts
>> I'd like to share with you guys:
>>
>>
>> - YouCard Re-visited: Implementing the ViewModel pattern (
>>
>> http://jonas.follesoe.no/YouCardRevisitedImplementingTheViewModelPattern.aspx
>> )
>> - YouCard Re-visited: Implementing Dependency Injection in Silverlight
>> (
>>
>> http://jonas.follesoe.no/YouCardRevisitedImplementingDependencyInjectionInSilverlight.aspx
>> )
>>
>> Lately I've been spending allot of time thinking about how to best
>> architect a Silverlight application and which patterns that apply. Since
>> Silverligth is so similar to WPF we can learn from their experiences. There
>> are several frameworks being brought over from WPF to Silverlight, such as
>> Unity (
>> http://michaelsync.net/2008/07/11/unity-application-block-unity-for-silverlight-and-stoplight-quickstart)
>> and Composit WPF/Prism (
>> http://www.codeplex.com/CompositeWPFContrib/Wiki/View.aspx?title=Prism%20to%20Silverlight&referringTitle=PrismAG
>> ).
>>
>> One of the things I care allot about is providing a good design time
>> experience in Blend, with proper test data etc. Way to often I download
>> WPF/Silverlight samples from Microsoft, and try to put my self in the shoes
>> of a designer who want to redesign the application. Often that is really
>> hard, as there isn't good test data for items controls etc.
>>
>> The two posts I mentioned talks about how to architect/build your
>> Silverlight in a way that gives you a good design time experience, and at
>> the same time apply good design principles like louse coupling, seperation
>> of concerns etc. Dependency injection for instance makes allot of sense, as
>> you can inject mock providers when the code is consumed in Blend, giving
>> your designers a better experience.
>>
>> What are your experience in building code that works nicely in Blend? Any
>> tips trick? War stories? Does this matter?
>>
>> These are some of the topics I'm planning on covering in my TechEd talk in
>> September.
>>
>> Cheers,
>> Jonas Follesø
>> http://jonas.follesoe.no
>>
>>
>>
>>
>>
>>
>> -------------------------------------------------------------------
>> OzSilverlight.com - to unsubscribe from this list, send a message back to
>> the list with 'unsubscribe' as the subject.
>> Powered by mailenable.com - List managed by www.readify.net
>>
>
> -------------------------------------------------------------------
> OzSilverlight.com - to unsubscribe from this list, send a message back to
> the list with 'unsubscribe' as the subject.
> Powered by mailenable.com - List managed by www.readify.net
>
-------------------------------------------------------------------
OzSilverlight.com - to unsubscribe from this list, send a message back to the
list with 'unsubscribe' as the subject.
Powered by mailenable.com - List managed by www.readify.net