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

Preface

Writing modern Enterprise software requires large teams that need close co-ordination to deliver quality products and services. The teams require a common language for communication when dealing with technical solutions and artifacts, in order to work as a cohesive unit for meeting the set objective. The GOF design pattern catalog was a major step in this direction. This was followed by other similar catalogs to empower software engineering teams with a common pool of shared idioms and practices. For all practical purpose, GOF patterns became sine qua non for patterns. But there are other equally important catalogs that you would want to leverage.

There have been attempts to map the pattern catalogs to platforms such as JEE and .NET. The J2EE --design patterns and Microsoft--patterns and practices catalogs were attempts in this direction. The emergence and adoption of functional programming and reactive programming models mandates a fresh look at the patterns, applied to modern .NET software development. That is where this book comes into the picture. This book provides detailed examples written using C#/.NET, which will help developers map different constructs from various pattern catalogs to executable code.

The book you have got in your hand outlines author's perspectives about how design, architecture, and domain patterns can be applied in practice by leveraging the .NET platform. The topics covered deals with various patterns and pattern catalogs, including GOF patterns, concurrent programming, object/functional programming, and functional reactive programming. The final chapter talks about materials which we could not cover in this book, but, which a modern programmer should master to become an effective developer.

Learning, as we all know, is a continuous process. We have made to the best of our ability to impart our learnings on the job so that it benefits any one who is interested. We have leveraged code extensively for illustrating this with very detailed examples. Design illustration in the form of UML and context diagrams accompany the concepts and code. The views and perspectives we have outlined mostly align with the industry references and standards.

What this book covers

Chapter 1, An Introduction to Patterns and Pattern Catalogs, deals with the summary of key pattern catalogs and also deals with the evolution of the C# programming language. The chapter deals with the gist of GOF, POSA, DDD, EIP, J2EE, POEAA, and the Arlow and Neustadt pattern catalog.

Chapter 2, Why We Need Design Patterns?, provides the rationale for applying patterns in the context of software engineering. The chapter deals with some foundational principles such as SOLID, KISS, YAGNI, DRY/WET and so on, that lay the foundation for pattern oriented software development. A tax computation engine that uses the factory method pattern, the template method pattern, and design by contract is given as a running example.

Chapter 3, A Logging Library, implements a logging library which can be used in a multithreaded environment. The implementation leverages the template method pattern, the strategy pattern, and the factory method pattern. A custom Dependency Injection framework loads the classes on the fly.

Chapter 4, Targeting Multiple Databases, shows how one can use different persistent technologies (Oracle, SQL Server, SQLite, and so on) without changing a single line of application code. The library leverages ADO.net and uses the abstract factory pattern, the factory pattern, and the adapter pattern.

Chapter 5, Producing Tabular Reports, shows how one can create a canonical object model based on the composite pattern and traverse the object model through visitor pattern to generate HTML and PDF documents.

Chapter 6, Plotting Mathematical Expressions, deals with a library that uses composite, interpreter, observer, builder, and facade patterns to plot arbitrary mathematical expression on the fly using Windows Presentation Foundation. 

Chapter 7, Patterns in the .NET Base Class Library, demonstrates how the designers of .NET base class library has leveraged various GOF patterns to give developers a good programing model.

Chapter 8, Concurrent and Parallel Programming under .NET, steps into the very important topic of concurrent and parallel programming by leveraging constructs provided by the .NET platform. Topics such as concurrency versus parallelism, fork/join parallelism, producer-consumer model, and so on are dealt at depth.

Chapter 9, Functional Programming Techniques for Better State Management, deals with the very important topic of managing state by leveraging closures, lambda, immutability, LINQ and other functional programming techniques.

Chapter 10, Pattern Implementation Using Object/Functional Programming, illustrates Map/Reduce, the template method pattern, the iterator pattern, and the strategy pattern by a judicious mix of functional and object-oriented programming constructs.

Chapter 11, What is Reactive Programming?, gives you the gist of Functional Reactive programming model and its realization on the .NET platform.

Chapter 12, Reactive Programming Using .NET Rx Extensions, carries forward from the previous chapter and shows some concrete examples where Functional Reactive Programming techniques can be put for real world use.

Chapter 13, Reactive Programming Using RxJS, gives a very good introduction to the techniques and internals of RxJS for writing web-based responsive UI.

Chapter 14, A Road Ahead, sheds light on a learning progression path highlighting the importance of polyglot programming, domain-specific languages, ontology, and antipatterns.

What you need for this book

The reader is supposed to have worked with at least a project with the .NET platform using C# as the primary programming language. A good understanding of basic object oriented programming will help you navigate the territory faster. From a software perspective, a laptop, desktop or a virtual machine with Visual Studio 2012 (or above) to test and play around with the source code.

Who this book is for

The book is useful for any C#/.NET developer to understand how enterprise class applications are designed, developed, and deployed. Rather than studying various patterns in isolation, the book deals with scenarios where patterns are often combined to engineer state of the art applications. Developers who work with Java can also follow the book without much difficulty, as both platforms have lot of things in common.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "An instance of this class needs to be passed to the Sort routine of the List<T> instance."

A block of code is set as follows:

    class Employee 
    { 
        public String name {get;set;} 
        public int age {get;set;} 
        public double salary { get; set; } 
    }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    return function () { 
      try { 
        return fn.apply(null, args.slice(1)); 
      }

Any command-line input or output is written as follows:

npm install rx

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "You could simply do this in JSbin by using the Add library option shown in the web editor."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/.NET-Design-Patterns. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.