Linqify your code.


I’m sure most programmers are familiar with Linq, and agree with me that it’s great. I personally really enjoying using Linq, mostly in the form o f Linq to SQL and Linq to Objects. It also helps with simplifying your code and makes it easier to read. Some of the most useful Linq functions I find are FirstOrDefault, Any, All and Where. However sometimes I still come across developers writing loops to do work when Linq can be utilized to simplify it. So here are some simple code samples on how to use Linq to eliminate writing foreach loops.

Read the rest of this entry »

Advertisements

Strategy and Decorator Pattern


It’s been a long time since I posted anything, been very busy with work. Many things have changed and I am currently working as an independent contractor.  My latest gig at the moment is working on a WPF product! 🙂 woo hoo!

Today I’m going to talk about using the Strategy and Decorator Pattern together, as it turns out they work pretty well together. Strategy pattern is a good way to encapsulate your algorithms, or in my case I want to use it to encapsulate reusable code, following the principle to keep it DRY. With the introduction of Action and Func classes in .NET framework, I have found that writing callbacks to be so much more accessible. For instance, you may sometimes need to repeat lots of Try…Catch statements in your code, and find that hard to keep DRY. This is where using callbacks are useful, like so…

Read the rest of this entry »

Perfect match: Composite and Visitor Pattern


In one of my earliest post, I talked about the importantance of understanding principles of good programming, and the useful-ness of Design Patterns… in this post I intend to share some implementation of some of these patterns. Patterns are something I use from time to time on projects, and something that I’m continuously learning from reading and from others, and still enjoying it very much. Let’s proceed then…

Design Patterns can be categorized into creational, structural and behavioral. I find two patterns that work pretty well together being the Composite Pattern (structural) and the Visitor Pattern (behavior). Composite Pattern is a structural pattern that helps one create tree-like hierarchial structures whereas the Visitor Pattern is a behavioral pattern that allows a visitor object to “visit” each element in a structural hierarchy to perform some operation/behavior on that element. Read the rest of this entry »

WPF: DragDrop Decorator for ItemsControl


Drag and Drop functionality in WPF is pretty similar to how you do it in Winforms, using DataObject, DragEffects, etc in the System.Windows namespace. In Winforms, there’s a DoDragDrop method in the Control class, but in WPF, it’s been extracted out into a DragDrop static helper class. Writing drag and drop code can be quite repetitive and boring, and can be abstracted out to handle most scenarios. In this post, I have done so using the Decorator pattern.

Read the rest of this entry »

SOA 2.0: SOA evolved


In the IT world, acronyms and buzzwords come and go really quickly, with only the well-received ones persisting. Service Oriented Architecture (SOA) is one such example, and apparently there’s now an evolved version called SOA 2.0, coined at the JavaOne Conference not too long ago. From what I understand, SOA 2.0 leverages off SOA by incorporating Event Driven Architecture (EDA).

Read the rest of this entry »

Using Factory Method Pattern with System.Activator


Over the time I spent developing, I have used Factory Method Pattern numerous times to create my objects. It provides a layer of abstraction by using an interface (or abstract base class) to hide away your concrete implementations. At runtime, we don’t care about the concrete objects that is being returned because we should always strive to program to an interface, not an implementation.
Read the rest of this entry »

Article: Microsoft Patterns and Practices


I came across a good article on MSDN that talks about the separation of concerns using Enterprise Library (now at 4.1), using the Object Builder framework and Unity Application Block for Dependency Injection/Inversion of Control, with examples of incorporating it into Model View Presenter (MVP) pattern.

With quite a number of open source frameworks (like Castle Windsor) out there that do these already, it’s refreshing to see that MS is churning out their own pattern frameworks for .NET Development.

Read article here!