Many people write code that is not re-usable, testable, and basically, totally unmaintainable.
Let's face reality - writing "good code" is not trivial, and software developers often find themselves in a position where time is limited and they have to deliver a piece of software.
The question is not only "will they deliver the software on time", but perhaps "what will they end-up delivering"?
Over the past month of research and development, I have read numerous blog posts, MSDN articles, white papers, and conducted my own development to try to answer one question: what are the best patterns & practices known today in order to build maintainable line-of-business Silverlight 2 applications?
I have written a sample Silverlight application called "SilverList" that uses most of the results of my extensive research.
Here are the key Silverlight design concepts:
- M-V-VM (Model-View-ViewModel) design pattern + Silverlight Commanding in order to allow a more seamless developer/designer relationship. You can read Shawn Wildermuth's article on the subject @ http://msdn.microsoft.com/en-us/magazine/dd458800.aspx
- Unit Testing (because if you don't write unit tests - you should start, trust me, it provides many benefits). You can find it @ http://code.msdn.microsoft.com/silverlightut
- Dependency Injection (forget about hardwiring dependencies and start injecting them using Ninject - the ninja of dependency injection!) Ninject can be found @ http://ninject.org/
- Isolated Storage (think "Google Gears" - cache, cache, cache!) Why make a round-trip to the server if you don't really need to?
- Blendability (your UI/UX designer is complaining that when he/she is working in Expression Blend, all the ListBoxes appear empty with no data, same with all the DataGrids and TextBoxes. Well, with M-V-VM and IoC (Inversion of Control), you can inject dummy data so that when your beloved designer wants to change the font, color, or the style of a control, the dummy data is there to reflect the change in real-time! There isn't a need to run the application in the browser just so see the effect of a simple change - very useful for RAD (Rapid Application Development) scenarios.
- LINQ (Language Integrated Query). Why implement IComparable/IComparer on your custom objects when you can simply write a couple of lines of LINQ code to search/sort a list of data by a given property?
Here is what the the application actually does:
-
Add name-value pairs to the list in the format: name=value (e.g. Hello = World). The name can be comprised of any alpha-numeric characters, following by an equal sign, followed by another set of alpha-numeric characters
-
Delete a name-value pair from the list
-
Sort the list in ascending order either by name or by value
-
Filter the list by either Name or Value. For filtering, the name portion has to be "Name" or "Value". For example, to display all entries in the list with the name Hello, type in "Name = Hello" (no quotes). To filter by value, type in "Value = World" (no quotes).
-
Clear the filter to return to the main list
-
When adding/sorting/deleting name-value pairs, SilverList automatically saves the current data into IsolatedStorage. So if you close the browser and visit the page again, you will see your latest data in the ListBox right away. Filtering, however, is a temporary operation, and does not cause the list data to be saved into IsolatedStorage
As you can see, the application is simple, yet is mainly used to show good design patterns and practices.
Here is a link to the SilverList Unit Testing page: SilverList Unit Tests . Be patient while the XAP is downloaded and the Unit Testing Framework is initialized. You will be able to see how the Silverlight Unit Testing framework executes my unit tests for SilverList live in your brower. Let's hope all the tests pass!
This was an introductory post and I will write about this application and plan on dissecting it in more detail in future posts. For now, however, you can grab all the code, and play with it live below.
Here is a link to all the code:
Here is the application live (you need to have the Silverlight plugin installed to use it):
[silverlight:source=/blog/ClientBin/SilverList.xap;width=500;height=600]