Skip to content

Instantly share code, notes, and snippets.

@panesofglass
Created November 12, 2009 17:33
Show Gist options
  • Select an option

  • Save panesofglass/233094 to your computer and use it in GitHub Desktop.

Select an option

Save panesofglass/233094 to your computer and use it in GitHub Desktop.
A view interface for WPF and Silverlight
using System.Windows.Controls;
using Foundation.Client.PresentationModels;
namespace Foundation.Client.Views
{
/// <summary>
/// Common interface from which all UI views should inherit.
/// </summary>
/// <remarks>
/// In general, all views will inherit from UserControl, which has uses
/// a DataContext property to store data for presentation in the control. In
/// the event that another control is used, the developer needs to implement
/// a public DataContext property.
/// </remarks>
public interface IView
{
#region properties
/// <summary>
/// Gets or sets the data context.
/// </summary>
/// <value>The data context.</value>
object DataContext { get; set; }
#endregion
}
/// <summary>
/// Defines extension methods on the <see cref="IView"/> interface to allow
/// common functionality to all classes inheriting from <see cref="IView"/>.
/// </summary>
/// <remarks>
/// <para>Creating extension methods on an interface allows a common implementation
/// for all inheritors of that interface. This can be beneficial in lieu of
/// or in addition to an abstract base class when the potential for multiple,
/// abstract base classes may be used for similar types that do reuse a small
/// number of methods.</para>
/// <para>
/// <example>
/// The following demonstrates what is possible with the extension method.
/// <code>
/// public interface ISomeDefinedView : IView
/// {
/// }
///
/// public class SomeDefinedView : UserControl, ISomeDefinedView
/// {
/// ... // Generic UserControl code behind
/// }
///
/// public class SomePresentationModel : PresentationModelBase, IPresentationModel
/// {
/// public ISomeDefinedView View { get; set; }
///
/// [InjectionConstructor]
/// public SomePresentationModel(ISomeDefinedView view)
/// {
/// View = view;
/// View.SetModel(model);
/// }
///
/// ... // Additional PresentationModel logic
/// }
/// </code>
/// </example>
/// </para>
/// </remarks>
/// <seealso href="http://timrayburn.net/blog/interfaces-extension-methods-power/"/>
/// <seealso href="http://msdn.microsoft.com/en-us/vcsharp/bb625996.aspx"/>
public static class IViewExtensions
{
#region methods
/// <summary>
/// Sets the IView's DataContext property.
/// </summary>
/// <param name="view">The <see cref="IView"/>.</param>
/// <param name="model">The <see cref="IPresentationModel"/>.</param>
public static void SetModel(this IView view, IPresentationModel model)
{
view.DataContext = model;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment