This Blog Has A New Home!

This blog has been moved to www.SoftwareRockstar.com. Articles already here will remain intact, while new content will only be added to the new location at www.SoftwareRockstar.com.

Showing posts with label Architecture. Show all posts
Showing posts with label Architecture. Show all posts

Saturday, March 14, 2009

Learn APS.NET MVC

ScottGu  just made available a free downloadable eBook in PDF format as well as a simple and easy-to-understand sample project called Nerddinner.  This eBook is actually a chapter from his upcoming book on MVC, but it is an end-to-end tutorial that walks through building a small but complete ASP.NET MVC application from scratch.

Download eBook.

Download Nerddinner sample code.

Friday, August 17, 2007

The Castle Project

In the last couple of years I have noticed a steadily growing interest in the .NET community around the Inversion of Control (IoC) and Dependency Injection (DI) patterns.  This is partly due to the fact that a number of excellent frameworks have emerged and gained popularity that allow us to use these designs in our applications relatively painlessly.  One such framework is the open source Castle Project.  In my opinion Castle is the best implementation of IoC and DI among many others that I have come across including Spring.NET, NInject, and ObjectBuilder.  This is however my opinion; which framework you use largely depends upon your requirements and your preferences.

If you are just getting started with Castle or are curious about why the heck one should use IoC and DI, I strongly suggest you check out the 3-part article series by Simone Busoli.  I just came across these articles and I can't stress enough how well they are written.

Monday, August 28, 2006

ISP: Interface Segregation Principle

The Interface Segregation Principle (ISP) deals with cohesion and is closely related to the Single Responsibility Principle (SRP). In Uncle Bob's terms, the ISP states that:
Clients should not be forced to depend on methods that they do not use.
In other words, this principle states that if a class performs multiple functions that may be used by different client classes, then those functions should be seperated out into different interfaces according to their usage. The idea behind this principle is to confine the aount of rework to only clients that use a certain interface, should the interface of a depended-upon class changes.

WWF 101: Introduction to Windows Workflow Foundation

Windows Workflow Foundation (WF) is Microsoft's latest platform for building workflow-enabled applications. Along with Windows Presentation Foundation (WPF) and Windows Communication Foundation (WCF), WF is part of the WinFX platform, now officially named as .NET Framework 3.0. While workflow capability has been available to developers using BizTalk Server and other third-party products, WF provides a single platform and programming model for Microsoft products (e.g. Office) and non-Microsoft products (developed using .NET) making workflow more of a mainstream paradigm. In it's simplest form, the workflow-based programming model consists of a set of business activities along with repetition and conditional logic that is modelled using some sort of a user interface. At run-time, the conditionals are evaluated and activities are executed based on the modelled workflow. If you have ever created a SQL Server DTS or SSIS package then you are already familiar with workflow-based programming model. Some of the advantages of workflow-based programming include durability, transparency, and tolerance to dynamic change. It allows for rule-based programming and supports a declarative model which allows for easy modelling and modification of business processes. WF nicely integrates into Visual Studio offering a rich design-time and debugging environment. Workflows can be hosted by any .NET executable assemblies including Win Forms and ASP.NET applications. WF comes with an array of built-in activities e.g. Code, Delay, Invoke, Throw, Suspend, etc., but the best part is that custom activities can be created as simply as deriving from the Activity base class and overriding the Execute method. Even composite activities can be created providing a rich and extensible programming model. The Activity Framework in WF provides several of out-of-the-box services such as transactions, compensation, serialization, etc. Other services can be added as desired. For example, by default workflows are persisted using XAML, but if needed custom serialization may be added. In conclusion, WF provides a very rich and highly extensible workflow-based programming model. With the passage of time we shall witness more and more Microsoft and non-Microsoft products using the WF to build and enhance applications that could use workflow functionality in new and innovative ways. As they say in French, the best is yet to come!

Sunday, August 27, 2006

WCF 101: Introduction to Windows Communication Foundation

Windows Communication Foundation (WCF) is Microsoft's next generation platform for distributed systems. Along with Windows Presentation Foundation (WPF) and Windows Workflow Foundation (WWF), WCF (code-named "Indigo") is part of the WinFX platform, now officially named as .NET Framework 3.0. With such a diverse array of distributed technologies already floating around, each one with a distinct benefit, Microsoft decided to unify these technologies under a single unified platform that would not only simply developers' lives but also pull in the benefits of all such existing technologies into one. ASP.NET web services, for example, offer ease of programability, WSE offers security, System.Messaging offers guaranteed delivery of asynchronous messages, Enterprise Services offers attribute based transaction support, and yet .NET Remoting offers transparent use of objects. WCF unifies all these benefits and more such that the code base remains the same, while depending upon the requirements, features such as transactions can be configured mostly through XML configuration settings. WCF is based on Microsoft's vision of Service Oriented Architecture (SOA), where developers shall be able to combine multiple services (could be cross-platform and cross-vendor) to invent software applications that have not yet been possible. WCF is based on a well-thought-out design where services and clients define one or more end-points. Each end-point defines the address, binding, and contract (ABC) which can be configured independently, but work together to provide a distributed message-oriented communication infrastructure. WCF runs on Windows Vista, XP and 2003. Beta versions of .NET Framework 3.0 (including WCF) can be downloaded from Microsoft.

DIP: Dependency Inversion Principle

The Dependency Inversion Principle deals with how to correctly design classes such that their dependency on one another causes the least amount of work in case of a change. Uncle Bob's definition of DIP states:
  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions.
Essentially what this principle means is that in a tiered design, higher level modules and lower level modules should not directly depend on each other; instead they should only depend on abstractions. Moreover, abstractions should not have any knowledge of details (other classes in the system). This principle closely relates to some of the other design principles I discussed previously in that it yields a design that is highly decoupled ensuring that minor changes in one part of the application do not cause a domino effect in other parts of the application. Moreover such a design is extensible -- as new business entities are added, usually such as design scales well by requiring only new code to be added rather than existing code to be modified. ASP.NET 2.0's Provider Model is a great example of such a design that provides a default implementation for various sub-systems such as Membership, Personalization, Navigation, etc. but also allows developers to modify the default behavior by extending certain classes.

Wednesday, August 23, 2006

LSP: Liskov Substitution Principle

The Liskov Substitution Principle dictates when and where it is correct to derive a subtype from an existing supertype. As defined by Barbara Liskov and Jeannette Wing, it essentially states that:
Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.
What it means is that if S is a subtype of T, then a function q(x) must behave in the same manner irrespective of the type of x whether it be S or T. In other words, if a piece of code behaves differently for a subtype than a supertype, then that code violates the LSP (and consequently the OCP). Let's say that we work for a bank and that we have a simple teller application. Currently the teller application works with checking and savings account types. Our bank just entered the mortgage business so we need to modify our teller application to support this new account type. During the short analysis phase we decide that since mortgage account is type of an account (IS-A relationship), we can simply create a new mortgage class deriving from the Account abstract class, override a method and a property, and we should be in business. We add a new class as follows: Everything sounds logical, that is until we come across this code in the teller application: Based on our pre-existing code, the ReceiveMoney() method is making a reasonable assumption that the new balance should be equal to the old balance plus the newly deposited amount. This assumption is, however, violated if we pass an instance of the Mortgage class to this method. This is a clear violation of the LSP.

Monday, August 21, 2006

OCP Open-Closed Principle

Another fundemental principle of object-oriented software design is the Open-Closed Principle. According to Uncle Bob (Robert Martin), this principle states that:
Software entities (classes, modules, functions, etc.) should be
open for extension, but closed for modification.
Essentially, what this means is that software's design should be such that it's behavior can be modified by extending the existing source code rather than modifying it. Consider the following example: Notice that the ReceiveMoney() method of the Bank class has to determine the type of account passed in. Only based on that information it can perform the appropriate business logic. Later if we add another account type then we may need to modify this code in order for it to work properly. The above code is, hence, in violation of the Open-Closed principle. So how can we revise our code to conform to the Open-Closed Principle? Abstraction is the answer. Let's take a look: Notice how we got rid of all conditional logic in the ReceiveMoney() method. Also notice that if we later add a new account type, the ReceiveMoney() method will work without any modifications. The revised version of our code above, hence, conforms to the Open-Closed Principle. Let's now consider what happens to our code if a new business rule is added that requires an e-mail notification to be sent to the account holder whenever a deposit is made. Obviously, we'll have to modify the ReceiveMoney() method to perform that logic. Since we can't account for all possible scenarios, we can never acheive perfect closure. With that in mind, design the simplest software that will do the job, and conform to OCP or other such design principles where you see the need.

Sunday, August 20, 2006

SRP: Single Responsibility Principle

SRP is one of the fundamental object oriented software design principles, first introduced by Tom DeMarco in 1979. In simple terms, SRP states that every class should have a single well defined goal, and that all members of that class (properties, methods, etc.) should work together to achieve that goal. Robert Martin puts it in slightly different terms:
There should never be more than one reason for a class to change.
Essentially SRP enforces the concept of high cohesion that should not only be applied to classes but also internals of a class, such as methods. So why is SRP such an important design principle? It is because a class with multiple responsibilities is more difficult to change and more difficult to reuse (design smells: fragility and immobility). While SRP is quite easy to understand, it's not always logical to implement. Consider the following class:

Given the convenience of having a customer class that is smart enough to know how to load and save itself, would you rather break it out like this?:

According to SRP, you should, because the combined customer class shares multiple responsibilities: customer business logic, and customer persistence logic. In fact if you use TDD, you may even be forced to separate these two responsibilities.

In practicality, however, while we should keep this principle in mind and try to apply it wherever it makes sense, we should never get so carried away that we start introducing other design smells in our architecture, such as Rigidity, Viscosity, and Needless Complexity.

Saturday, August 19, 2006

Generic Collection<T> and ReadOnlyCollection<T> Are Actually Not Misplaced

Since I started using generic collections in .NET Framework 2.0, it always bothered me that Collection<T> and ReadOnlyCollection<T> objects reside in the System.Collections.ObjectModel namespace rather than where they actually belong in the System.Collections.Generic namespace. Today I came across this blog by Krzysztof Cwalina that explains why Microsoft made this decission. Now it makes good sense to me.

Tuesday, July 18, 2006

Inherit With Care

I was asked to look at an ASP.NET 1.1 application that was designed with a distributed architecture using .NET Remoting. Over a period of a few hours of heavy usage, this production application would intermittently bring the Windows 2003 Server that it was being hosted on to a screeching halt. Cause unknown. No event log entries, nothing suspicious in IIS logs, nothing too special about the application that would indicate this behavior. Performance monitoring, however, indicated that the handles on the OS would gradually increase to dangerous levels of 50,000+ before the application would crash. Hmmm, what could be using so many handles and not releasing them? My first instinct was to make sure that all the remoting objects were disposed properly after use. Upon closer investigation, however, I found out that all ASP.NET pages in the application were inheriting from a custom base class. The base class was overriding the page's Init method, and guess what I found in that method? A call to RemotingConfiguration.Configure() method. At first I was surprised why would someone configure remoting every single time a page is instantiated? Soon it hit me why system handles were astronomically increasing. Once that line of code was moved to Application_Start event, the application hasn't crashed in weeks. Moral of the story? Inheritance is a good OO concept, but only we as developers can decide how badly we want to abuse it!

Monday, July 17, 2006

Which UML Models Are The Most Important?

Well, the answer depends on who you ask and what they are trying to model, but the following five models have proven to be essential to me on almost every project since I started using UML:
  1. Activity
  2. Use Case
  3. Class
  4. Component
  5. Deployment

Class models are obviously the most important ones since I can directly generate code from them. Others help me generate artifacts that I feel are essential for project documentation.

Which UML Tool Do You Use?

While I have used several modelling tools in the past, since I started using Sparx Systems Enterprise Architect (EA), I have not looked back. This is a great tool (for the most part) with a reasonable price tag. What I do wish this tool could do better is provide CodeSmith type support for code generation. EA does have reverse and forward code engineering support, of course, but forward engineering is not exactly very intuitive. It does however provide a COM API that one can tap into in order to extend the tool in more suitable ways.

New Articles On Software Rockstar