Csharp Patterns

["Hello World" removed]

Design Patterns: Solidify Your C# Application Architecture with Design Patterns: http://msdn.microsoft.com/msdnmag/issues/01/07/patterns/

Exploring the SingletonPattern: http://msdn.microsoft.com/library/en-us/dnbda/html/singletondespatt.asp

Exploring the ObserverPattern: http://msdn.microsoft.com/library/en-us/dnbda/html/observerpattern.asp

Exploring the AbstractFactoryPattern: http://msdn.microsoft.com/library/en-us/dnbda/html/factopattern.asp

Asynchronous Programming Design Pattern: http://msdn.microsoft.com/library/en-us/cpguide/html/cpconasynchronousdesignpatternoverview.asp

Design Patterns Used by Reflection Classes: http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondesignpatternsusedbyreflectionclasses.asp

.NET Design Principles http://www.parlezuml.com/tutorials/dotnetarch.htm Spam? [BrokenLink]

.NET Event Handling using the TemplateMethodPattern: http://www.csharphelp.com/archives/archive277.html

Implementing the PrototypePattern: http://www.dotnetextreme.com/articles/PrototypePattern.asp

BuilderPattern: http://www.dotnetextreme.com/articles/builderpattern.asp

Design Patterns in C#: The DecoratorPattern: http://www.csharp.org/whitepapers/dsgn_patterns/articles_20020111.pdf

BridgePattern in C#: http://www.c-sharpcorner.com/Language/BridgePatternsinCSRVS.asp

AdapterPattern in C#: http://www.c-sharpcorner.com/Language/AdapterPatternInCSRVS.asp

MementoPattern in C#: "Object Serialization with the Memento Pattern" http://www.ondotnet.com/pub/a/dotnet/2002/09/09/memento.html

The Dispose Design Pattern (buy the book): http://www.microsoft.com/mspress/books/toc/5027.asp

Design Patterns Snippets in C Sharp. http://www.clipcode.net/components/snippets.htm

UML & Design Principles for C# http://www.parlezuml.com

Could we get a quick summary of the DisposePattern?

An object that manages resources that need to be explicitly released implements the IDisposable interface, which has a single method, called Dispose, which is implemented something like this:

 protected virtual void Dispose(bool disposing) {
	if(!this.disposed) {
		if(disposing) {
	this.disposed = true;         

From Microsoft's SDK docs:

''Dispose(bool disposing) executes in two distinct scenarios. If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and unmanaged resources can be disposed.

If disposing equals false, the method has been called by the runtime from inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.''

So, in the context of something like a database connection, there might be a public Close() method which calls the protected Dispose(true) method, and a Finalize() (or 'destructor') method - called by the runtime during GC - which calls Dispose(false).

And one other factor of the pattern recommended by JeffreyRichter? in Applied Microsoft .Net Programming: If called explicitly, the Dispose method should call GC.SuppressFinalize?(this). Since the Dispose method has freed unmanaged resources, there is no longer need for the garbage collector to call the class's destructor. Because garbage collection is more efficient on non-Finalizable types, the explicitly called IDisposable.Dispose method should suppress the no-longer needed destructor.

In C#, client code manages disposable objects with the "using" keyword. This:

 using (SomeObject? o = new SomeObject?())

is equivalent to this:

 SomeObject? o = new SomeObject?();

CeeSharp makes the Singleton pattern pretty easy:

 sealed class Singleton {
    private Singleton() {}
    public static readonly Singleton Instance = new Singleton();

I found MSDN's explanation of asynchronous programming to be confusing, so I wrote my own notes and example: http://kristopherjohnson.net/kj/AsynchronousDelegate. --KrisJohnson
Microsoft have put together a book, available in PDF format at: http://microsoft.com/downloads/details.aspx?FamilyId=3C81C38E-ABFC-484F-A076-CF99B3485754&displaylang=en, on "Enterprise Solution Patterns Using Microsoft .NET". It's one of those self-extracting executables but unzip on linux extracted the pdf without any problems. Large parts of it seem to be language specific rehashes of patterns in Fowler's PatternsOfEnterpriseApplicationArchitecture and the GoF book but that's not necessarily a bad thing. On the other hand their singleton example uses DoubleCheckedLocking. Does anyone know if that actually works in C# or do the issues why DoubleCheckedLockingIsBroken in Java also apply here? --AdewaleOshineye

Yes, DoubleCheckedLockingIsBroken in C#. For the nitty gritty, google for "chris brumme" and "memory barriers". --CharlesReilly?


Many pattern examples exist at http://www.dofactory.com/Patterns/Patterns.aspx

DesignPatternsCsharp (book)

EditText of this page (last edited May 23, 2012) or FindPage with title or text search