Navigation Makes ASP.NET WebForms More Testable
Navigation is a library that makes ASP.NET Web Forms more testable by moving the CRUD logic out of the codebehind files. Graham Mendick’s article “Unit Testing in the Navigation..” explores some of the features and how unit tests can be written.
What does this library do? It allows
- Controls to be bound to controller methods in separate classes
- Navigational binding
all inside your aspx page mark-up. For e.g., the markup for binding a FormView to its controller methods would look something like this -
<asp:FormView ID="Question" runat="server" DataSourceID="QuestionDataSource" DefaultMode="Edit">
<asp:ObjectDataSource ID="QuestionDataSource" runat="server" SelectMethod="GetQuestion" UpdateMethod="UpdateQuestion" TypeName="Survey.SurveyController" DataObjectTypeName="Survey.Question" />
where the GetQuestion and UpdateQuestion are controller methods implemented in an external class named SurveyController – they cannot directly access the controls, but the data is passed to them by the Navigation Library. Survey.Question refers to a ViewModel that is used in this example for transferring the data.
Why does this simplify unit testing? Because now, the business logic is in a separate POCO class with no web-specific behaviour. And this can be tested more easily. See the article for more details.
As we have already covered on InfoQ, WebForms 4.5 gets model binders out-of-the-box – however this library allows the controller methods to be put in a separate class instead of the code-behind file which seems better for testability and reusability. If interested, you can download the Navigation NerdDinner walkthrough.
Shane Hastie on Distributed Agile Teams, Product Ownership and the Agile Manifesto Translation Program
Shane Hastie Apr 17, 2015