refactoring asp.net and beyond

13
Refactoring with R# Giorgetti Alessandro Ricci Gian Maria

Upload: dotnetmarche

Post on 29-Nov-2014

88 views

Category:

Technology


1 download

DESCRIPTION

In questa sessione verranno analizzate alcune pagine ASP.NET che verranno sottoposte a refactoring per aumentarne la modularità e comprensibilità del codice, nonchè favorire la testabilità delle funzioni core. Si presenteranno techiche e strumenti che è possibile utilizzare in modo da avere un approccio più razionale ed 'ordinato' nella stesura del codice. Le pratiche ed i concetti illustrati sono di carattere generale, quindi liberamente applicabili ad ogni tipologia di applicazione.

TRANSCRIPT

Page 1: Refactoring ASP.NET and beyond

Refactoring with R#Giorgetti Alessandro

Ricci Gian Maria

Page 2: Refactoring ASP.NET and beyond

Giorgetti AlessandroOwner of SID s.r.l.

Blog: http://www.nablasoft.com/guardianEmail: [email protected]

Page 3: Refactoring ASP.NET and beyond

Ricci Gian MariaMVP Team System

Blog: http://www.nablasoft.com/alkampfer [Eng]http://blogs.ugidotnet.org/rgm [Ita]

Email: [email protected]

Page 4: Refactoring ASP.NET and beyond

Refactoring PurposesClean-up the codeImprove readabilityImprove the ‘code maintenance’Implement a ‘Separation of Concerns’ in your

applicationAllow better testabilityImprove performances appling some ‘best

practices’

Page 5: Refactoring ASP.NET and beyond

Prerequisites to RefactoringUnderstand what the code doesUnderstand what the code is supposed to do(sometimes they do not match)Knowledge of what you are going to do (do not

blindly follow some practices or use tools without knowing how the work and the impact they have).

You are altering (someone else’s) code, so always be carefull and assure yourself to preserve the original functionalities.

Page 6: Refactoring ASP.NET and beyond

The Refactoring PipelineRefactoring can be a very complex operation,

especially depending on the legacy code you are working on.

Sometimes is wise to divide the task in multiple iterations, and work on a single task a time. This way you can test what you are doing and it’s easier to revert back in case of errors.

Let’s see some common steps:

Page 7: Refactoring ASP.NET and beyond

The Refactoring PipelineAnalyze the legacy code.Write down some objectives you want to

accomplish with refactoring (like: beautify code and improve readability, separate business logic code from presentation, improve performace, testability, etc…).

Assign a priority to all these task and start an iteration for any of them.

Page 8: Refactoring ASP.NET and beyond

The Refactoring PipelineThe Iteration Chain should be something like:Think what you are going to do (for example if

you are isolating business code from presentation code, define a set of classes and interface and so on…).

Write down some documentation about the changes you are going to do.

Implement your solution.Add some tests for the new component

eventually written.Verify that the whole system works.

Page 9: Refactoring ASP.NET and beyond

Demo – Refactoring of an Image GalleryOur final goal will be:- Structurate the application- Less code bloating- More classes with small methods in them

Page 10: Refactoring ASP.NET and beyond

Demo - Refactoring an Image GalleryDefining the iterations:Step 0 - Analyze the code.Step 1 - Isolate the tasks, separate concerns,

make the methods smaller, improve readability and code maintenace.

Step 2- Define application layers and reduce couplings, especially the ‘persistence coupling’; we end up with a layered application usually: presentation, services, repository layers.

Step 3 – Apply an MVP/MVC/MVVM pattern and move code to testable classes.

Page 11: Refactoring ASP.NET and beyond

Demo - refactor to reach testabilityWe start from a web form where all the logic

is inside the code behindWe will move, step by step, to a custom and

simple implementation of MVCWe add test for the logic of the pageThe goal is having a web page so simple that

not a simple if should remain in code behind.

Page 12: Refactoring ASP.NET and beyond

Tips & TricksUse a CDN service when possible to deliver

library services (AJAX, jQuery and so on) (http://www.asp.net/ajax/cdn/)

Consider using Javascript or jQuery for simple tasks to avoid unneeded PostBacks

Test…test…test…

Page 13: Refactoring ASP.NET and beyond

Questions ?

We hope NOT, cause we are TIRED and we want to EAT and DRINK!