(Ebook) Adaptive Code Via C#: Agile Coding With Design Patterns and SOLID Principles by Gary McLean Hall ISBN 9780735683204, 0735683204 PDF Download
(Ebook) Adaptive Code Via C#: Agile Coding With Design Patterns and SOLID Principles by Gary McLean Hall ISBN 9780735683204, 0735683204 PDF Download
https://ebooknice.com/product/adaptive-code-via-c-agile-coding-with-
design-patterns-and-solid-principles-5032562
https://ebooknice.com/product/javascript-patterns-build-better-
applications-with-coding-and-design-patterns-55886614
https://ebooknice.com/product/agile-software-development-principles-
patterns-and-practices-21976766
https://ebooknice.com/product/pro-wpf-and-silverlight-mvvm-effective-
application-development-with-model-view-viewmodel-1722038
https://ebooknice.com/product/pro-wpf-and-silverlight-mvvm-effective-
application-development-with-model-view-viewmodel-42323434
(Ebook) MASTERING JAVASCRIPT DESIGN PATTERNS: create scalable
and reliable applications with advanced Javascript design
patterns using reliable code by Tomas Corral Cosas ISBN
9781788627580, 178862758X
https://ebooknice.com/product/mastering-javascript-design-patterns-create-
scalable-and-reliable-applications-with-advanced-javascript-design-
patterns-using-reliable-code-9992610
https://ebooknice.com/product/the-easiest-way-to-learn-design-patterns-
with-c-code-samples-using-net-6-templates-47443310
https://ebooknice.com/product/agile-software-development-principles-
patterns-and-practices-11074242
https://ebooknice.com/product/functional-design-principles-patterns-and-
practices-robert-c-martin-55621382
https://ebooknice.com/product/java-design-patterns-a-tour-of-23-gang-of-
four-design-patterns-in-java-5470384
From the Library of Ida Schander
Adaptive Code via C#:
Agile coding with
design patterns and
SOLID principles
First Printing
Microsoft Press books are available through booksellers and distributors worldwide. If you need support related
to this book, email Microsoft Press Book Support at [email protected]. Please tell us what you think of
this book at http://aka.ms/tellpress.
This book is provided “as-is” and expresses the author’s views and opinions. The views, opinions and information
expressed in this book, including URL and other Internet website references, may change without notice.
Some examples depicted herein are provided for illustration only and are fictitious. No real association or
connection is intended or should be inferred.
Microsoft and the trademarks listed at http://www.microsoft.com on the “Trademarks” webpage are trademarks
of the Microsoft group of companies. All other marks are property of their respective owners.
Introduction xv
Index 387
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Artifacts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Scrum board. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Charts and metrics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Backlogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The sprint. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Release planning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Sprint planning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Daily Scrum. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Sprint demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Sprint retrospective. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Scrum calendar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
microsoft.com/learning/booksurvey
vii
Managing dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Implementations versus interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
The new code smell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Alternatives to object construction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
The Entourage anti-pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The Stairway pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Resolving dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Dependency management with NuGet. . . . . . . . . . . . . . . . . . . . . . . . . . 77
Layering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Common patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Cross-cutting concerns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Asymmetric layering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
viii Contents
Refactoring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Changing existing code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
A new account type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Contents ix
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Preconditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Postconditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Data invariants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Liskov contract rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Code contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
x Contents
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Contents xi
“I want to view the messages that have been sent to a room.“ . . . . . . . . . 353
xii Contents
Index 387
microsoft.com/learning/booksurvey
Contents xiii
T he first words of the title of this book, Adaptive Code, provide a good description
of the outcome of applying the principles in the book: the ability of code to adapt
to any new requirement or unforeseen scenario while avoiding significant rework. The
aim of this book is to aggregate into one volume many of the current best practices in
the world of C# programming with the Microsoft .NET Framework. Although some of
the content is covered in other books, those books either focus heavily on theory or are
not specific to .NET development.
Programming can be a slow process. If your code is adaptive, you will be able to
make changes to it more quickly, more easily, and with fewer errors than you would
if you were working with a codebase that impedes changes. Requirements, as every
developer knows, are subject to change. How change is managed is a key differen
tiating factor between successful software projects and those that atrophy due to scope
creep. Developers can react in many ways to requirement changes, with two opposing
viewpoints highlighting the continuum that lies between.
First, developers can choose a rigid viewpoint. In this approach, from the develop-
ment process down to class design, the project is as inflexible as if it were implemented
50 years ago by using punch cards. Waterfall methodologies are conspicuous culprits
in ensuring that software cannot change freely. Their determination that the phases of
analysis, design, implementation, and testing be distinct and one-way make it difficult—
or at least expensive—for customers to change requirements after implementation has
begun. The code, then, does not need to be built for change: the process all but forbids
alterations.
The second approach, Agile methodology, is not just an alternative to such rigid
methodologies, but a reaction to them. The aim of Agile processes is to embrace change
as a necessary part of the contract between client and developer. If customers want to
change something in the product that they are paying for, the temporal and financial
cost should be correlated to the size of the change, not the phase of the process that is
currently in progress. Unlike physical engineering, software engineering works with a
malleable tool: source code. The bricks and mortar that form a house are literally fused
together as construction progresses. The expense involved in changing the design of
a house is necessarily linked to the completion of the building phase. If the project
has not been started—if it is still just in blueprints—change is relatively cheap. If the
windows are in, the electricity wired up, and the plumbing fitted, moving the upstairs
bathroom down next to the kitchen could be prohibitively expensive. With code, mov-
ing features around and reworking the navigation of a user interface should not be as
xv
This book demonstrates the second approach and explains, with real-world exam-
ples, the practicalities of implementing adaptive code.
Capable intermediate programmers who want to plug the gaps in their knowledge
or have doubts and questions about how some of the industry’s best practices fit
together will benefit most from this book, especially because the day-to-day reality
of programming rarely matches simple examples or theory. Much of SOLID is now
understood, but the intricacies of the open/closed principle (covered in Chapter 6)
and Liskov substitution (covered in Chapter 7) are not fully comprehended. Even
experienced programmers sometimes do not fully realize the benefits provided by
dependency injection (covered in Chapter 9). Similarly, the flexibility—adaptability—
that interfaces (covered in Chapter 3) lend to code is often overlooked.
This book can also help the more junior developer learn, from the ground up, which
aspects of common patterns and practices are benevolent and which are, in the long
term, malevolent. The code samples that I see from prospective employees have a lot
in common. The general theme is that the candidate is almost there with respect to
many skills but just needs a slight push in the right direction to become a significantly
better programmer. Specifically, the Entourage anti-pattern (covered in Chapter 2) and
the Service Locator anti-pattern (covered in Chapter 9) are very prevalent in sample
code. Practical alternatives, and their rationales, are provided in this book.
Assumptions
Ideally, you should have some practical experience of programming in a language that
is syntactically similar to C#, such as Java or C++. You should also have a strong founda-
tion in core procedural programming concepts such as conditional branching, loops,
and expressions. You should also have some experience of object-oriented program-
ming using classes, and at least a passing familiarity with interfaces.
xvi Introduction
■■ Chapter 1: Introduction to Scrum This chapter sets the scene for the book
by introducing Scrum, which is an Agile project management methodology. The
chapter gives an in-depth overview of the artifacts, roles, metrics, and phases
of a Scrum project. Finally, it shows how developers should organize themselves
and their code when operating in an Agile environment.
Introduction xvii
■■ Chapter 4: Unit testing and refactoring Two practices that are becoming
prerequisite skills are unit testing and refactoring. The two are closely related
and work in unison to produce adaptive code. Without the safety net of unit
tests, refactoring is prone to error; without refactoring, code becomes unwieldy,
rigid, and hard to comprehend. This chapter takes an example of unit testing
from humble beginnings and expands it to use more advanced—but practical
—patterns and practices such as fluent assertions, test-driven development,
and mocking. For refactoring, the chapter provides examples of real-world
refactors that improve the readability and maintainability of the source code.
■■ Chapter 7: The Liskov substitution principle This chapter shows the posi-
tive effects that result from applying the Liskov substitution principle on code,
particularly the fact that the guidelines help enforce the open/closed principle
and prevent the unintended consequences of change. Contracts—through
xviii Introduction
■■ Chapter 12: Adaptive sample: Sprint 2 The client, inevitably, makes some
changes to the requirements of the application, and the team accommodates
those changes through adaptive code.
Introduction xix
In the interior of South America, with the rivers Parana and Paraguay
to the east, with Argentine to the south, and Bolivia to the west,
there is a vast, low country called the Gran Chaco, about as large as
the state of Texas and inhabited by Indians. The country is flat and
there are grass-lands, swamps, and forests of palm trees. There are
many different animals with which the children of the North are not
familiar but of which they may have seen pictures, among them the
tapir, the marsh deer, the otter, the peccary, and the armadillo. There
are some savage animals such as the jaguar, the puma, and a very
large wolf with a long mane.
There are also some of the queerest animals in the world, especially
the ant-eater, a bow-legged creature seven feet long from the tip of
his snout to the tip of his hairy tail. There is a queer little opossum
about the size of a mouse, with enormous black eyes, fan-like ears,
and a long tail, which runs about in the trees like a squirrel. Most
interesting of all is the lungfish which can live either in the water or
in the air. In the wet season he stays in the swamps and eats and
eats, and when the dry season comes and the swamps disappear, he
burrows in the ground and lives without eating anything, by using up
the fat he has stored.
There are many birds both large and small, from great ostriches
down to tiny hummingbirds, and there are insects of all kinds, ants
and crickets and mosquitoes and beetles and locusts, and there are
twenty-four different kinds of frogs, each with a different croak.
For many weeks no rain falls, and the Indians have a hard time to
get along; then when the rain comes they have more than they need
to eat, water-birds, fish, and, by-and-by, their harvests. They do not
mind having to tramp round in deep water, because wet weather
brings plenty.
Among the Indians in this strange country was a young man named
Poit. One morning in December Poit awoke with a frightened,
anxious heart. It was not because he was too warm, though in
December in Chaco the mornings are hot, nor because he had not
slept comfortably on his bed on the ground nor because he was
hungry; it was because he plotted a wicked deed. Today Poit
planned to do the most dreadful thing anyone can do, he was going
to kill his best friend, the missionary.
Though these Indians lived so uncomfortably, they did not want to
change their ways, and they killed everybody who came to explore
their country or to search for silver or to tell them of the love of God.
Even soldiers sent to conquer them by force failed because they
were so fierce and cunning.
The chief reason for their resistance and their cruelty was not
wickedness, but ignorance and dreadful fear. They were afraid of
spirits and afraid of witches and wizards. They were so afraid that
the souls of the dead might come and annoy them that whenever
anyone died they destroyed the village and went to another place to
live. This wasn't very difficult because their houses were made of
boughs stuck into the ground. They were especially afraid of people
unlike themselves, and this was the reason they killed foreigners.
In spite of their objections, a little mission had been established
among them. It was situated on the banks of the Paraguay River
and its influence did not extend very far inland, but it was a
beginning. The first missionary died as a result of his hard work, and
there arrived one day a new missionary, a tall, slender young man,
hardly more than a boy in years, whose name was Barbrooke Grubb.
Mr. Grubb was not satisfied to stay along the river where he could
see only a few of the Indians, he determined to travel to the interior
villages. He knew perfectly well that the undertaking was dangerous.
He had heard of the explorers and the missionaries whom the
Indians had murdered; he knew that a poor white man who had
strayed from his companions and had taken refuge with them had
been slain; he knew that if sickness broke out while he was staying
in a village, he would be held responsible and be killed. He knew
that if an Indian had a bad dream about him, he might kill him.
Nevertheless, he not only visited the interior of the country, but he
lived with the Indians for months at a time, staying in their villages,
eating their strange food, hunting and fishing with them, so that he
might learn all about their ways and help them. He went unarmed
and unprotected, saying that he was a messenger of peace.
He had many thrilling experiences, and some that were very funny.
Of course he did not know the language well at first and he mistook
the word "evil" for the word "good," and assured the people that he
was a friend of the "evil spirit."
Barbrooke Grubb
Unarmed and unprotected, he was a messenger
of peace to the Indians of Paraguay.
They were now traveling by day, and they set out at about half-past
six for their last journey together. The sun was already high and so
hot that it had dried the heavy dew. They had gone but a short
distance when Mr. Grubb saw that he had been led into a thicket. He
observed a strange look on Poit's face, and did not realize that he
had caught Poit's eye at the moment when he was trying to get into
a position from which he could shoot him.
A moment later he bent over, trying to break a path through the
undergrowth, and in that instant Poit lifted his bow and arrow. A
stinging blow under his shoulder blade, and Mr. Grubb understood in
a flash that this was not his friend but his enemy, and that he had
been shot, perhaps fatally.
When the deed was done, Poit came to himself. He shouted in
dismay and terror, "Ak kai! Ak kai!" and rushed away.
He had run only a short distance when he sat down to think. He
believed that he had either killed Mr. Grubb outright or that Mr.
Grubb would soon die from his wounds or that he would be slain by
a jaguar whose tracks they had crossed. He decided craftily that he
would set out straightway for the mission and say that he had seen
a jaguar about to leap, and that, shooting at the jaguar, he had
killed Mr. Grubb.
He had not gone very far when he met an Indian with paint marks
on his body, which showed that he was in mourning. Poit supposed
this meant that Mr. Grubb was dead—someone must have found Mr.
Grubb's body before the jaguar devoured it. He ran back into the
forest. By this time he was out of his mind with fear. For hundreds
and hundreds of years the Indians had killed foreigners without
thinking anything about it; but now there was a change. Here was
an Indian mourning for a foreigner! Poit was puzzled and frightened.
He did not yet know that all the Indians were crying out for
vengeance upon the man who had tried to murder their benefactor.
But what neither Poit nor the mourning Indian knew was that Mr.
Grubb was still alive. How he reached the mission was a miracle. He
was more dead than alive from the wound which pierced his lung,
and from exhaustion. Sometimes he staggered along leaning on two
Indians; sometimes he rode a horse on whose back he had to be
supported. Often his companions had to lay him down on the ground
lest he should die. He suffered from the heat by day and was
tortured by the mosquitoes by night. As though this were not
enough, one night a goat belonging to an Indian jumped on him by
accident!
But at last he reached the mission and had proper medical attention,
and all along the weary way the Indians saw his agony and
understood that he was suffering because he had come to help
them. They thought not only of him, but of the Master about whom
he had told them, and they believed that he had been saved by a
miracle.
Though Mr. Grubb still lived, the Indians decided that Poit must die,
and they searched for him until they captured him. He pleaded with
them desperately, reminding them that he was their relative whom
they had known all their lives and that Mr. Grubb was only a
stranger; but they would not listen.
When he heard that Poit was to die, Mr. Grubb tried to save him, but
in vain. He did, however, succeed in saving Poit's family whom the
Indians would have killed also. This forgiving spirit amazed and
touched them still more.
Now this story is sad and dreadful and there would not be any
reason for telling it if Poit's death were the end. But in a way, it was
only a beginning.
Mr. Grubb had to make two journeys for further medical attention,
one to Ascuncion, nearly four hundred miles away, and one to
Buenos Ayres, nine hundred miles away. It was December when Poit
attacked him; it was June before he was able to take up his work.
When he did so, the seed so strangely sown by poor Poit had
ripened. Two Indians who had been impressed by Mr. Grubb's
devotion and by his almost miraculous recovery asked to be
baptized. Thus the foundation of the Church in the Chaco was laid.
Mr. Grubb is still working, and the extent of his influence has greatly
increased. The Indians in the distant settlements no longer wait for
him to seek them out; they come to see for themselves what he has
done and to hear the story he has to tell. The government has
named him the "pacificator of the Indians."
Do you not suppose that sometimes as he thinks of his years in the
Chaco, he thinks with pity of poor Poit and hopes that his cry "Ak
kai! Ak kai!" showed repentance as well as fear of punishment?
IV
TREE-NOT-SHAKEN-BY-THE-WIND
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com