Book Image

.NET Design Patterns

By : Praseed Pai, Shine Xavier
Book Image

.NET Design Patterns

By: Praseed Pai, Shine Xavier

Overview of this book

Knowing about design patterns enables developers to improve their code base, promoting code reuse and making their design more robust. This book focuses on the practical aspects of programming in .NET. You will learn about some of the relevant design patterns (and their application) that are most widely used. We start with classic object-oriented programming (OOP) techniques, evaluate parallel programming and concurrency models, enhance implementations by mixing OOP and functional programming, and finally to the reactive programming model where functional programming and OOP are used in synergy to write better code. Throughout this book, we’ll show you how to deal with architecture/design techniques, GoF patterns, relevant patterns from other catalogs, functional programming, and reactive programming techniques. After reading this book, you will be able to convincingly leverage these design patterns (factory pattern, builder pattern, prototype pattern, adapter pattern, facade pattern, decorator pattern, observer pattern and so on) for your programs. You will also be able to write fluid functional code in .NET that would leverage concurrency and parallelism!
Table of Contents (22 chapters)
.NET Design Patterns
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Patterns and pattern movement


Programmers of the early 1990s struggled a lot to understand OOP, and how to effectively use them in large projects. Without a viral medium such as the Internet, it was quite a struggle for them. Early adopters published technical reports, wrote in periodicals/journals, and conducted seminars to popularize OOP techniques. Magazines such as Dr. Dobbs Journal and C++ Report carried columns featuring OOP.

A need was felt to transfer the wisdom of the experts to the ever-increasing programming community, but this knowledge propagation was not happening. The legendary German mathematician Carl Friedrich Gauss once remarked, "Always learn from the masters". Even though Gauss had mathematics in mind, it is true for any non-trivial human endeavor. However, there were very few masters of the OOP techniques, and the apprenticeship model was not scaling well.

Note

James Coplien published an influential book titled Advanced C++ Programming Styles and Idioms, which dealt with low-level patterns (idioms) associated with the usage of the C++ programming language. Despite being not widely cited, authors consider this a notable book towards cataloguing the best practices and techniques of OOP.

  • It was during this time that Erich Gamma began his work on a pattern catalog as part of his PhD thesis, inspired by an architect named Christopher Alexander. Christopher Alexander's A Pattern Language - Towns, Buildings, Construction was a source of inspiration for Erich Gamma. Then, people with similar ideas, namely Ralph Johnson, John Vlissides, and Richard Helm, joined hands with Erich Gamma to create a catalog of 23 patterns, now popularly known as the Gang of Four (GoF) design patterns. Addison Wesley published the book Design Patterns: Elements of Reusable Object-Oriented Software in the year 1994. This soon became a great reference for the programmer, and fueled software development based on patterns. The GoF catalog was mostly focused on software design.
  • In the year 1996, a group of engineers from Siemens published a book titled Pattern-Oriented Software Architecture, which focused mostly on the architectural aspects of building a system. The entire Pattern-Oriented Software Architecture (POSA) pattern catalog was documented in five books published by John Wiley and Sons. The group was joined by Douglas Schmidt, the creator of the Adaptive Communication Environment (ACE) network programming library and TAO (The ACE ORB). He later became the chair of Object Management Group (OMG), which develops, adopts, and maintains standards such as CORBA and UML.
  • Another influential catalog was published by Martin Fowler in a book titled Patterns of Enterprise Application Architecture in the year 2001. The book mostly focused on patterns that come up while developing enterprise applications using the JEE and .NET frameworks. Incidentally, most of the code snippets were in Java and C#.
  • Gregor Hohpe and Bobby Woolf published a pattern catalog to document the patterns that arise in the enterprise integration scenario. Their catalog titled Enterprise Integration Patterns, published as part of the Martin Fowler signature book series, is widely recognized as a source of ideas regarding enterprise integration techniques. The Apache Camel integration library is inspired by this book.
  • Core J2EE Patterns: Best Practices and Design Strategies (by Deepak Alur et al.), although a platform-specific catalog, is a rich source of ideas regarding the structuring of an enterprise application. The book includes patterns for presentation, data, and service tiers in web application development.
  • Domain-Driven Design, published by Eric Evans in the year 2003, deals with a technique called domain-driven design (DDD). The book uses GoF and Patterns of Enterprise Application Architecture (POEAA) patterns to put forward a design methodology that focuses on building a persistent ignorant domain model. The book also introduces some patterns and idioms for structuring domain logic.
  • Jim Arlow and Ila Nuestadt published a book entitled Enterprise Patterns and MDA, which catalogued a set of patterns based on the Jungian Archetypes. This catalog contains nine top-level archetypes and 168 business archetypes for developing applications.

The following figure illustrates the evolution of design methodologies, programming languages, and pattern catalogs:

Key pattern catalogs

Patterns are most often catalogued in some kind of pattern repository. Some of them are published as books. The most popular and widely used pattern catalog is GoF, named after the four collaborators that produced them. They are Eric Gama, Ralph Johnson, John Vlissides, and Richard Helm.

GoF patterns

The GoF catalog, named after the four creators of the catalog, started the pattern movement. The creators mostly focused on designing and architecting object-oriented software. The ideas of Christopher Alexander were borrowed to the software engineering discipline, and applied to application architecture, concurrency, security, and so on. The GoF divided the catalog into structural, creational, and behavioral patterns. The original book used C++ and Smalltalk to explain the concepts. These patterns have been ported and leveraged in most of the programming languages that exist today.

Sr. no

Pattern type

Patterns

1

Creational patterns

Abstract factory, builder, factory method, prototype, singleton.

2

Structural patterns

Adapter, bridge, composite, decorator, facade, flyweight, proxy.

3

Behavioral patterns

Chain of responsibility, command, interpreter, iterator, mediator, memento, observer, state, strategy, template method, visitor.

We believe that a good understanding of the GoF patterns is necessary for any programmer. These patterns occur everywhere, regardless of the application domain. GoF patterns help us to communicate and reason about systems in a language-agnostic manner. They are widely implemented in the .NET and Java world.

POSA catalog

POSA Volume 5 is an influential book series, which covers most of the applicable patterns while developing mission-critical systems. An element of bias is seen towards native code programming; perhaps C++ was the prevalent OOP language during the time of research. The catalog, which spanned five published volumes, is listed as follows:

Sr. no

Pattern type

Patterns

1

Architectural

Layers, pipes and filters, blackboard, broker, MVC, presentation-abstraction-control, microkernel, reflection.

2

Design

Whole-part, mater-slave, proxy, command processor, view handler, forwarder-receiver, client-dispatcher-server, publisher-subscriber.

3

Service access and configuration patterns

Wrapper façade, component configurator, interceptor, extension interface.

4

Event handling patterns

Reactor, proactor, asynchronous completion token, acceptor-connector.

5

Synchronization patterns

Scoped locking, strategized locking, thread-safe interface, double-checked locking optimization.

6

Concurrency patterns

Active object, monitor object, half-sync/half-async, leader/followers, thread-specific storage.

7

Resource acquisition patterns

Lookup, lazy acquisition, eager acquisition, partial acquisition.

8

Resource lifecycle

Caching, pooling, coordinator, resource lifecycle manager.

9

Resource release patterns

Leasing, evictor.

10

A pattern language for distributive computing

Consolidation of patterns from different catalogs in the context of distributed programming.

11

On patterns and pattern languages

This last volume gives some meta information about patterns, pattern languages, and their usage.

We believe the POSA catalog is very important (to the extent, one of the author feels, that if someone has not heard about the POSA catalog, he or she does not understand patterns) if one is writing middleware servers and scalable web infrastructure. For some reason, it has not got the kind of traction that it deserves. They are very useful for writing server-class software infrastructure such as web containers, application containers, and other middleware components.

POEAA catalog

Martin Fowler, along with some co-authors, published a book entitled Patterns of Enterprise Application Architecture. The book is a treasure trove of patterns, that helps one to structure and organize the design of enterprise applications using .NET and Java. Some of Fowler's pattern has been leveraged in the context of distributed computing by POSA-Volume 4 authors.

Sr. no

Pattern type

Patterns

1

Domain logic

Transaction script, domain model, table module, service layer.

2

Data source architectural patterns

Table data gateway, row data gateway, active record, data mapper.

3

Object-relational behavioral patterns

Unit of work, identity map, lazy load.

4

Object-relational structural patterns

Identity field, foreign key mapping, association table mapping, dependent mapping, embedded value, serialized LOB, single table inheritance, class table inheritance, concrete table inheritance, inheritance mappers.

5

Object-relational metadata mapping patterns

Metadata mapping, query object, repository.

6

Web presentation patterns

Model view controller, page controller, front controller, template view, transform view, two-step view, application controller.

7

Distribution patterns

Remote facade, data transfer object.

8

Offline concurrency patterns

Optimistic offline lock, pessimistic offline lock, coarse grained lock, implicit lock.

9

Session state patterns

Client session state, server session state, database session state.

10

Base patterns

Gateway, mapper, layer supertype, separated interface, registry, value object, money, special case, plugin, service stub, record set.

The POEAA catalog is a rich source of ideas when it comes to enterprise application software development. Some of these patterns are implemented by frameworks such as Spring (including Spring.NET), Nhibernate/Entity Framework, Windows Communication Foundation (WCF), and Windows Presentation Foundation (WPF). Awareness about the POEAA catalog helps one to reason about the architecture of pretty much everything happening in the .NET platform.

EIP catalog

A modern day enterprise requires information to flow from one application to another, in real time or while offline. Since applications use different implementation technologies, we require message passing systems to transfer the data. Most often, these communications happen in an asynchronous manner. The Enterprise Integration Patterns (EIP) catalog deals with time-tested solutions by professionals, who have cut their teeth on integration issues for recurring problems.

Sr. no

Pattern type

Patterns

1

Messaging systems

Message channel, message, pipes and filters, message router, message translator, message endpoint.

2

Messaging channels

Point-to-point channel, publish-subscribe channel, datatype channel, invalid message channel, dead letter channel, guaranteed delivery, channel adapter, messaging bridge, message bus.

3

Message construction

Command message, document message, event message, request-reply, return address, correlation identifier, message sequence, message expiration, format indicator.

4

Message routing

Content-based router, message filter, dynamic router, recipient list, splitter, aggregator, resequencer, composed message processor, scatter-gather, routing slip, process manager, message broker.

5

Message transformation

Envelope wrapper, content enricher, content filter, claim check, normalizer, canonical data model.

6

Messaging endpoints

Messaging gateway, messaging mapper, transactional client, polling consumer, event-driven consumer, competing consumers, message dispatcher, selective consumer, durable subscriber, idempotent receiver, service activator.

7

System management

Control bus, detour, wire tap, message history, message store, smart proxy, test message, channel purger.

The EIP catalog is a very influential one in transferring knowledge about strategies for asynchronous messaging and point-to-point synchronous communication between applications. The Apache Camel library implements most of the commonly occurring patterns, while doing Enterprise Application Integration (EAI). The authors feel that this catalog is worth studying should one embark on a project that requires information/data flow from one system to another, including mobile device communication with backend services (MBAAS) that involves data synchronization and queuing mechanisms.

J2EE design patterns catalog

This is a catalog that captures design experience in the form of a book entitled Core J2EE Patterns: Best Practices and Design Strategies by Deepak Alur, John Crupi, and Dan Malks. The book and the associated website deals with common solutions that can be leveraged while writing enterprise web applications. Even though conceived for the J2EE platform, the patterns outlined in the catalog can be used in any context where there is a programming model similar to the J2EE platform. Fortunately, the .NET server-side model is very similar to J2EE.

Sr. no

Pattern class

Patterns

1

Business tier pattern

Business delegate, session facade, service locator, transfer object, composite entity, transfer object, assembler, value list handler, business object, application service.

2

Presentation tier patterns

Intercepting filter, front controller, composite view, view helper, service to worker, dispatcher view, context object, application controller.

3

Integration patterns

Data access object, service activator, domain store, web service broker.

The authors believe that the J2EE catalog has been used extensively in the .NET platform, especially after Microsoft released the ASP.Net MVC programming model. The catalog is a rich source of ideas to structure your enterprise web application.

DDD-based patterns

The book entitled Domain-Driven Design by Eric J. Evans, released in the year 2003, is not a book on patterns in itself. The primary goal of the book is to outline a method by which one can create persistent ignorant domain models by leveraging the ubiquitous language used by the stakeholders in a business scenario. The book contains a lot of patterns and idioms for architecture, design, and application integration, in a model-driven manner.

Sr. no

Pattern type

Patterns

1

Patterns for supple design

Intention-revealing interfaces, side-effect-free functions, assertions, conceptual contours, standalone classes, closure of operations, declarative design.

2

Patterns for domain model expression

Associations, entities (reference objects), value objects, services, modules (packages).

3

Patterns for domain model integrity

Bounded context, context map, shared kernel, anticorruption layer, open host service, published language.

4

Patterns for domain model distillation

Core domain, generic subdomains, segregated core, abstract core.

This is one of the most influential books in terms of thought leadership towards creating a methodology that goes along with the Agile development models. The ideas from this book have percolated deep into building the present day software systems.

Arlow/Nuestadt patterns

Jim Arlow and Ila Nuestadt published a book entitled Enterprise Patterns and MDA. The book is based on the concept of an archetype borrowed from the works of Carl Gustav Jung. Archetypes are primordial entities that occur time and again in the sociocultural context across cultures. Business archetypes are entities that occur in a business context (where business is a socio-economic activity). The business archetypes covered in the book include party, CRM, product, business rules, order, inventory, units, and so on. Archetypes help model the business problem, and this gives a clear indication of the expected composition and behavior of the solution. Archetypes are a powerful meme that provide direct mapping between the business and solution models, thereby avoiding mismatch during business analysis, design, and implementation. The ideas and schema from the book can be used to write better enterprise software products.

Sr. no

Pattern type

Patterns

1

Party archetype pattern

PartyIdentifier, RegisteredIdentifier, PartySignature, PartyAuthentication, address, person, ISOGender, ethnicity, BodyMetrics, PersonName, organization, company, company names, identifiers for companies, company organizational units, partnerships and sole proprietors, preferences, PartyManager.

2

Party relationship archetype pattern

PartyRole, PartyRoleType, PartyRelationshipType, responsibilities, capabilities.

3

Customer relationship management archetype pattern

Customer, CustomerCommunicationManager, customer communication, CustomerServiceCase.

4

Product archetype pattern

ProductType, ProductInstance, SerialNumber, batch, product specification, ProductCatalog, CatalogEntry, packages, PackageType, package instance, combining ProductTypes, rule-driven package specification, ProductRelationships, price, package pricing, measured products, services, ServiceType and ServiceInstance, product pleomorphs.

5

Inventory archetype pattern

The inventory archetype, ProductInventoryEntry, ServiceInventoryEntry, inventory capacity planning, inventory management, availability, reservations.

6

Order archetype pattern

The order archetype, PurchaseOrder, SalesOrder, OrderLine, PartySummaryRoleInOrder, DeliveryReceiver, ChargeLine, OrderManager, OrderEvents, order status, LifeCycleEvents, AmendEvents, AmendOrderLineEvent, AmendPartySummaryEvent, AmendTermsAndConditionsEvent, DiscountEvent, DespatchEvent, ReceiptEvent, OrderPayment, PaymentEvents, payment strategy, PurchaseOrder process, PurchaseOrder cancellation, process PurchaseOrder, SalesOrder process archetype, SalesOrder process, process SalesOrder, OrderProcess documentation.

7

Quantity archetype pattern

Quantity archetype pattern, metric, units/SystemOfUnits, SIBaseUnit, DerivedUnit, ManHour, quantity, StandardConversion/UnitConverter.

8

Money archetype pattern

Money archetype pattern, currency, locale, ISOCountryCode, ExchangeRate/CurrencyConverter, payment.

9

Rule archetype pattern

Rule archetype pattern, business rules/system rules, RuleElement, RuleContext, rule evaluation, ActivityRule, RuleSet, RuleOverride.

The authors have borrowed ideas from the book, while creating an ontology for realizing a domain-specific language (DSL) on a mobile-based healthcare application. If one is embarking on creating a DSL-based system architecture, this book can be a good starting point for rich domain models based on business archetypes.