Building an MVP Framework for NET Part 4


8,6 31,239 me mbe rs and growing!
l i amE d r o w s s a P ? dSrign n s a p Join o L
o wis t s
Ho me Art icles Quick Answers Discussio ns Learning Z o nes Feat ures Help! The Lounge
Search site
Development Lifecycle Design and Architecture Frameworks
Licence Ms-PL
See Also
:4 tr N o mPndli
P.T rf koM gi u
a E wV B
. aFn
rr a
e
First Posted 25 Apr 2008
More like this
Views 12,6 20
siticsdT ylnt More by this author
n ao y grS
o se o
Ap
Downloads 108
Bookmarked 29 time s
By Oleg Zhukov | 25 Apr 2008
C# .NET Dev
In this article we continue developing a Model-View-Presenter framework for .NET platform. The new features we are
implementing here are strongly typed asscoiations between controllers, views and tasks for higher convenience and type safety.
4.50 (2 votes)
Art icle Bro wse Co de St at s Revisio ns Alt ernat ives
Hot News: 10 Best Practices of Code
Commenting & Formatting
Download source - 771.91 KB
The Code Project Insider. Free each
morning.
Introduction
Solution outline
PDFmyURL.com
Solution outline
Solution implementation
Summary
Introduction
In the previous parts (1, 2, 3) we have constructed a quite usable and functional framework. However, it still has some
drawbacks. One of them is the necessity of typecasting when accessing controllers, views and tasks. The example below
demonstrates such typecasting:
Collapse | Copy Code
public class ProductsView : WebFormView, IProductsView
...
private void ShowProductDetailsButton_Click(object sender, EventArgs e)
{
(Controller as ProductsController).ShowProductDetails(); // typecasting required
}
As a system grows such typecasts may bloat code, excessively decreasing its readability and leading to errors. To eliminate
this drawback we need a means of explicitly specifying the type of the associated controller (or view/task). In other words we
need to make associations between tasks, controllers and views strongly typed.
Solution Outline
The most obvious solution is to isolate the typecasing operation in a new property of the required type:
Collapse | Copy Code
public class ProductsView : WebFormView, IProductsView
...
private new ProductsController Controller
{
get { return base.Controller as ProductsController; }
set { base.Controller = value; }
}
private void ShowProductDetailsButton_Click(object sender, EventArgs e)
{
Controller.ShowProductDetails(); // typecasting NOT required
}
Related Articles
Although acceptable, this solution requires several additional lines of code. A more elegant solution can be constructed with a
handy feature of .NET framework called Generics. The generics mechanism allows varying a class members' types by
Building an MVP Framework for .NET.
Part 1: The Basics of MVC and MVP
specifying those types in the class declaration. For example, it is possible to adjust a return type for certain properties by
writing that type in brackets in the class definition line.
Building an MVP Framework for .NET.
Part 2: Implementing Core
PDFmyURL.com
Part 2: Implementing Core
Applying Generics we could strictly specify the type of the association between a view and its controller as in the code below: Functionality
Building an MVP Framework for .NET.
Collapse | Copy Code
Part 3: Designing a Windows Forms
Views Engine
public class ProductsView : WebFormView, IProductsView
...
Slink Framework - Strongly Typed
private void ShowProductDetailsButton_Click(object sender, EventArgs e) URLs for ASP.NET
{
Building Security Awareness in .NET
Controller.ShowProductDetails(); // typecasting NOT required
Assemblies : Part 3 - Learn to break
Strong Name .NET Assemblies
}
From XML to Strong Types in C#
Extended Strongly Typed Resource
To make the above code workable we need to extend our framework, adding the generics support.
Generator
Strongly Typed DataSet Generator
Solution Implementation
Strongly typed AppSettings with
MSBuild
First of all we will add a generic view and controller interfaces to the framework. They will extend old IView and IController
Introducing the LinFu Framework, Part
interfaces with new strongly typed generic associations:
II: LinFu.DynamicObject  Adding
Dynamic Language Features to
Collapse | Copy Code
Statically Typed Languages
public interface IView : IView where T : IController Strongly Typed Guid Classes
{
The Raz or Framework :: Part 1 ::
new T Controller
Plugins/Extensibility
{
MVP Dependency Chaining
get;
Framework
set;
The Raz or Framework :: Part 2 ::
}
Configuration/Options
}
Strongly Typed Collection Builder
public interface IController : IController where TTask : ITask
Addin for VS.NET 2003
{
new TTask Task
A framework for building of WP7
{
application
get;
Managed Extensibility Framework:
set;
Part 2
}
WPF: If Carlsberg did MVVM
Frameworks Part 2 of n
new TView View
Strongly typed collections
{
get;
Dependency Injection Frameworks -
set; Part 1 - Introduction
}
}
These interfaces alone do provide strongly typed associations, however, we also need to write some base generic
implementation classes for these interfaces. So a developer will only inherit these base classes instead of implementing the
interfaces above.
We will implement the properties simply with backing fields and mark them virtual so that a developer may override them in
PDFmyURL.com
subclasses:
Collapse | Copy Code
public class WinFormView : Form, IView where T : class, IController
{
...
protected T controller;
public virtual T Controller
{
get { return controller; }
set { controller = value; }
}
IController IView.Controller
{
get { return Controller; }
set { Controller = value as T; }
}
...
}
public class ControllerBase : IController
where TTask : class, ITask
where TView : class
{
protected TTask task;
protected TView view;
public virtual TTask Task
{
get { return task; }
set { task = value; }
}
public virtual TView View
{
get { return view; }
set { view = value; }
}
ITask IController.Task
{
get { return Task; }
set { Task = value as TTask; }
}
IView IController.View
{
PDFmyURL.com
get { return View as IView; }
set { View = value as TView; }
}
}
Note that the non-generic IView and IController interfaces are implemented as gateways to the strongly typed generic
properties. This makes the access in the old non- generic manner (as done by the framework) equivalent to accessing the new
strongly typed properties.
Below is an example of using the new generic features of the framework:
Collapse | Copy Code
class MyController : ControllerBase
{
public void MyOperation()
{
View.MyViewOperation(); // Typecasting to IMyView NOT required
}
public override MyTask Task
{
get { return base.Task; }
set
{
base.Task = value;
// Controller initialization here
...
}
}
}
Summary
In the article we have developed new framework features which make it more usable and allow us to avoid typecasting errors.
Project Website
www.MVCSharp.org
License
This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)
About the Author
PDFmyURL.com
Oleg Zhukov Oleg Zhukov, born and living in Russia is a software development consultant in a company which
provides business solutions. He has graduated from Moscow Institute of Physics and Technology
(MIPT) (department of system programming) and has got a M.S. degree in applied physics and
mathematics. His research and development work concerns architectural patterns, domain- driven
development and systems analysis. Being the adherent of agile methods he applies them
extensively in the projects managed by him.
Architect
Russian Federation
Member
Article Top Sign Up to vote Poor Excellent Vote
Comments and Discussions
You must Sign In to use this me ssage board. (secure sign-in)
Se arch this forum G o
Profile popups Noise Me d ium Layout No rmal Per page 10 Update
Refresh
-- There are no messages in this forum --
Perm alink | Advertis e | Privacy | Mobile Layout: fixed | fluid Article Copyright 2008 by Oleg Z hukov
Web04 | 2.5.120405.1 | Las t Updated 25 Apr 2008 Everything els e Copyright © CodeProject, 1999-2012
Term s of Us e
PDFmyURL.com


Wyszukiwarka

Podobne podstrony:
Building An Online Shopping Cart Using C Sharp And Asp Net Part 1
An FPGA Based Framework for Technology Aware Prototyping of Multicore Embedded Architectures CLT
An FPGA Based Framework for Technology Aware Prototyping of Multicore Embedded Architectures CLT
Building An Outdoor Playhouse
Guide to building an RB30DET
Traffic Authority Building An Email List
Warmaster Building an Orc Fort
Healing the Body and Building the Astral Vehicle for the Magnum Opus by Frater DNFF
Comment on A Framework for Modelling Trojans and Computer Virus Infection
Rubio etal An axiom system for Incidence Spatial Geometry
Busta Rhymes Things we? doing for money Part 2
Collins An Attacking Repertoire for White
An Intermediate Google SketchUp Tutorial Part 1
An analysis tool for piezoelectric gages
An Intermediate Google SketchUp Tutorial Part 2
An Intermediate Google SketchUp Tutorial Part 5A

więcej podobnych podstron