0% found this document useful (0 votes)
346 views40 pages

Full Software Architecture by Example: Using C# and .NET 1st Edition Paul Michaels Ebook All Chapters

Paul

Uploaded by

orsertalahaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views40 pages

Full Software Architecture by Example: Using C# and .NET 1st Edition Paul Michaels Ebook All Chapters

Paul

Uploaded by

orsertalahaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Get ebook downloads in full at ebookmeta.

com

Software Architecture by Example: Using C# and


.NET 1st Edition Paul Michaels

https://ebookmeta.com/product/software-architecture-by-
example-using-c-and-net-1st-edition-paul-michaels-2/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Software Architecture by Example: Using C# and .NET 1st


Edition Paul Michaels

https://ebookmeta.com/product/software-architecture-by-example-using-
c-and-net-1st-edition-paul-michaels/

ebookmeta.com

Software Architecture with C# 10 and .NET 6 - Third


Edition Gabriel Baptista

https://ebookmeta.com/product/software-architecture-with-c-10-and-
net-6-third-edition-gabriel-baptista/

ebookmeta.com

Introducing .NET MAUI: Build and Deploy Cross-platform


Applications Using C# and .NET Multi-platform App UI 1st
Edition Shaun Lawrence
https://ebookmeta.com/product/introducing-net-maui-build-and-deploy-
cross-platform-applications-using-c-and-net-multi-platform-app-ui-1st-
edition-shaun-lawrence/
ebookmeta.com

Developmental Editing: A Handbook for Freelancers,


Authors, and Publishers, 2nd Edition Scott Norton

https://ebookmeta.com/product/developmental-editing-a-handbook-for-
freelancers-authors-and-publishers-2nd-edition-scott-norton/

ebookmeta.com
Musical Ethics and Islam The Art of Playing the Ney First
Edition Senay

https://ebookmeta.com/product/musical-ethics-and-islam-the-art-of-
playing-the-ney-first-edition-senay/

ebookmeta.com

Notices of the American Mathematical Society 5th Edition


Ezra Brown Richard K Guy

https://ebookmeta.com/product/notices-of-the-american-mathematical-
society-5th-edition-ezra-brown-richard-k-guy/

ebookmeta.com

Criminology and Public Theology: On Hope, Mercy and


Restoration 1st Edition Andrew Millie

https://ebookmeta.com/product/criminology-and-public-theology-on-hope-
mercy-and-restoration-1st-edition-andrew-millie-2/

ebookmeta.com

The Emma Project The Rajes 4 1st Edition Sonali Dev

https://ebookmeta.com/product/the-emma-project-the-rajes-4-1st-
edition-sonali-dev/

ebookmeta.com

Contesting Public Spaces: Social Lives of Urban


Redevelopment in London 1st Edition Ed Wall

https://ebookmeta.com/product/contesting-public-spaces-social-lives-
of-urban-redevelopment-in-london-1st-edition-ed-wall/

ebookmeta.com
The Bookie (Chicago Bratva #6) 1st Edition Renee Rose

https://ebookmeta.com/product/the-bookie-chicago-bratva-6-1st-edition-
renee-rose/

ebookmeta.com
Software
Architecture
by Example
Using C# and .NET

Paul Michaels
Foreword by Mark Richards
Software Architecture by
Example
Using C# and .NET

Paul Michaels
Foreword by Mark Richards
Software Architecture by Example: Using C# and .NET
Paul Michaels
Derbyshire, UK

ISBN-13 (pbk): 978-1-4842-7989-2 ISBN-13 (electronic): 978-1-4842-7990-8


https://doi.org/10.1007/978-1-4842-7990-8

Copyright © 2022 by Paul Michaels


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Smriti Srivastava
Development Editor: Laura Berendson
Coordinating Editor: Shrikant Vishwakarma
Cover designed by eStudioCalamar
Cover image designed by Pexels
Distributed to the book trade worldwide by Springer Science+Business Media LLC, 1 New York Plaza, Suite
4600, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.
com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner)
is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware
corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback,
or audio rights, please e-mail [email protected], or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at https://link.springer.com/book/10.1007/
978-1-4842-7989-2.
Printed on acid-free paper
To my wife, Claire, who has always been on, and at, my side,
even when I have not.
And to my dad, who has understood and supported me in everything
that I’ve done from the minute I was born.
Table of Contents
About the Author����������������������������������������������������������������������������������������������������� xi

About the Technical Reviewer������������������������������������������������������������������������������� xiii


Acknowledgments���������������������������������������������������������������������������������������������������xv

Introduction�����������������������������������������������������������������������������������������������������������xvii

Foreword����������������������������������������������������������������������������������������������������������������xxi

Chapter 1: The Ticket Sales Problem������������������������������������������������������������������������ 1


Background����������������������������������������������������������������������������������������������������������������������������������� 2
Requirements�������������������������������������������������������������������������������������������������������������������������������� 3
Options������������������������������������������������������������������������������������������������������������������������������������ 4
Manual Process����������������������������������������������������������������������������������������������������������������������� 4
Existing System����������������������������������������������������������������������������������������������������������������������� 6
Existing System Considerations���������������������������������������������������������������������������������������������� 7
Minimum Viable Product��������������������������������������������������������������������������������������������������������� 8
Target Architecture������������������������������������������������������������������������������������������������������������������������ 8
How to Deal with High Throughput������������������������������������������������������������������������������������������ 9
Widening the Funnel�������������������������������������������������������������������������������������������������������������� 10
Multiple Funnels�������������������������������������������������������������������������������������������������������������������� 11
Message Queues������������������������������������������������������������������������������������������������������������������� 13
Message Brokers������������������������������������������������������������������������������������������������������������������� 14
Separation of Concerns��������������������������������������������������������������������������������������������������������� 16
Target Architecture Diagram�������������������������������������������������������������������������������������������������� 17
Proxy�������������������������������������������������������������������������������������������������������������������������������������� 18
A Note on Cloud Vendors������������������������������������������������������������������������������������������������������� 19
Why Cloud?���������������������������������������������������������������������������������������������������������������������������� 20

v
Table of Contents

Examples������������������������������������������������������������������������������������������������������������������������������������ 21
External APIs�������������������������������������������������������������������������������������������������������������������������� 21
Getting Ticket Availability������������������������������������������������������������������������������������������������������ 21
Ordering a Ticket������������������������������������������������������������������������������������������������������������������� 25
Summary������������������������������������������������������������������������������������������������������������������������������������ 29

Chapter 2: The Cash Desk Problem������������������������������������������������������������������������ 31


Background��������������������������������������������������������������������������������������������������������������������������������� 32
Requirements������������������������������������������������������������������������������������������������������������������������������ 32
Options���������������������������������������������������������������������������������������������������������������������������������� 32
Manual Process��������������������������������������������������������������������������������������������������������������������� 33
Target Architecture���������������������������������������������������������������������������������������������������������������������� 36
Audit�������������������������������������������������������������������������������������������������������������������������������������� 36
Event Sourcing���������������������������������������������������������������������������������������������������������������������� 40
CQRS�������������������������������������������������������������������������������������������������������������������������������������� 46
Target Architecture Diagram�������������������������������������������������������������������������������������������������� 47
Examples������������������������������������������������������������������������������������������������������������������������������������ 49
Persisting Events to Memory������������������������������������������������������������������������������������������������� 49
Persisting Events to Disk������������������������������������������������������������������������������������������������������� 52
Summary������������������������������������������������������������������������������������������������������������������������������������ 61

Chapter 3: The Travel Agent Problem��������������������������������������������������������������������� 63


Background��������������������������������������������������������������������������������������������������������������������������������� 64
Requirements������������������������������������������������������������������������������������������������������������������������������ 65
Options���������������������������������������������������������������������������������������������������������������������������������� 66
Manual Process��������������������������������������������������������������������������������������������������������������������� 66
Transactions�������������������������������������������������������������������������������������������������������������������������� 67
Distributed Transactions�������������������������������������������������������������������������������������������������������� 71
Distributed Transaction with Timeout������������������������������������������������������������������������������������ 74
Book and Cancel�������������������������������������������������������������������������������������������������������������������� 75
Hold a Booking���������������������������������������������������������������������������������������������������������������������� 76

vi
Table of Contents

Advanced Purchase��������������������������������������������������������������������������������������������������������������� 76
Business Decision����������������������������������������������������������������������������������������������������������������� 76
Target Architecture���������������������������������������������������������������������������������������������������������������������� 77
Stateful Service��������������������������������������������������������������������������������������������������������������������� 79
Distributed Service���������������������������������������������������������������������������������������������������������������� 79
Target Architecture Diagram�������������������������������������������������������������������������������������������������� 79
Examples������������������������������������������������������������������������������������������������������������������������������������ 80
Project Structure������������������������������������������������������������������������������������������������������������������� 80
Service Bus Configuration����������������������������������������������������������������������������������������������������� 82
Coordinator���������������������������������������������������������������������������������������������������������������������������� 85
Summary������������������������������������������������������������������������������������������������������������������������������������ 90

Chapter 4: The Social Media Problem�������������������������������������������������������������������� 93


Background��������������������������������������������������������������������������������������������������������������������������������� 94
Requirements������������������������������������������������������������������������������������������������������������������������������ 94
Options���������������������������������������������������������������������������������������������������������������������������������� 95
Manual Process��������������������������������������������������������������������������������������������������������������������� 96
CQRS�������������������������������������������������������������������������������������������������������������������������������������� 97
Target Architecture�������������������������������������������������������������������������������������������������������������������� 102
Examples���������������������������������������������������������������������������������������������������������������������������������� 104
Schema Creation����������������������������������������������������������������������������������������������������������������� 104
Updating the Database�������������������������������������������������������������������������������������������������������� 105
Checking the Data��������������������������������������������������������������������������������������������������������������� 107
Web Service������������������������������������������������������������������������������������������������������������������������� 107
Accessing MongoDB������������������������������������������������������������������������������������������������������������ 110
The Client���������������������������������������������������������������������������������������������������������������������������� 114
Process Data Service���������������������������������������������������������������������������������������������������������� 118
Summary���������������������������������������������������������������������������������������������������������������������������������� 122

vii
Table of Contents

Chapter 5: The Admin Application Problem���������������������������������������������������������� 125


Background������������������������������������������������������������������������������������������������������������������������������� 126
Requirements���������������������������������������������������������������������������������������������������������������������������� 126
Options�������������������������������������������������������������������������������������������������������������������������������������� 127
Manual Process������������������������������������������������������������������������������������������������������������������������ 127
SOLID���������������������������������������������������������������������������������������������������������������������������������������� 129
Single Responsibility����������������������������������������������������������������������������������������������������������� 130
Open-Closed������������������������������������������������������������������������������������������������������������������������ 133
Liskov Substitution�������������������������������������������������������������������������������������������������������������� 136
Interface Segregation Principle������������������������������������������������������������������������������������������� 138
Dependency Inversion Principle������������������������������������������������������������������������������������������ 139
Methods of Extending Software������������������������������������������������������������������������������������������������ 141
Hooks����������������������������������������������������������������������������������������������������������������������������������� 141
Messages���������������������������������������������������������������������������������������������������������������������������� 142
Injection������������������������������������������������������������������������������������������������������������������������������� 145
Target Architecture�������������������������������������������������������������������������������������������������������������������� 148
Examples���������������������������������������������������������������������������������������������������������������������������������� 149
Basic Functionality�������������������������������������������������������������������������������������������������������������� 149
Extensibility������������������������������������������������������������������������������������������������������������������������� 154
Custom Functionality����������������������������������������������������������������������������������������������������������� 156
Summary���������������������������������������������������������������������������������������������������������������������������������� 157

Chapter 6: The Travel Rep Problem���������������������������������������������������������������������� 159


Background������������������������������������������������������������������������������������������������������������������������������� 160
Requirements���������������������������������������������������������������������������������������������������������������������������� 160
Options�������������������������������������������������������������������������������������������������������������������������������������� 161
Manual Process������������������������������������������������������������������������������������������������������������������� 161
Caching�������������������������������������������������������������������������������������������������������������������������������� 162
Sidecar Pattern�������������������������������������������������������������������������������������������������������������������� 163
Ambassador Pattern������������������������������������������������������������������������������������������������������������ 164

viii
Table of Contents

Target Architecture�������������������������������������������������������������������������������������������������������������������� 166


Containers��������������������������������������������������������������������������������������������������������������������������� 167
Examples���������������������������������������������������������������������������������������������������������������������������������� 168
Project Structure����������������������������������������������������������������������������������������������������������������� 169
TravelRep.CentralApi����������������������������������������������������������������������������������������������������������� 170
TravelRep.App���������������������������������������������������������������������������������������������������������������������� 173
TravelRep.Ambassador�������������������������������������������������������������������������������������������������������� 174
Containers��������������������������������������������������������������������������������������������������������������������������� 182
Summary���������������������������������������������������������������������������������������������������������������������������������� 196

Appendix A: Technical Appendix�������������������������������������������������������������������������� 199


C
 hapter 1���������������������������������������������������������������������������������������������������������������������������������� 199

Index��������������������������������������������������������������������������������������������������������������������� 205

ix
About the Author
Paul Michaels is the Head of Development at musicMagpie.
He started his career as a professional software engineer
in 1997. Paul is a regular speaker, published author, and
Microsoft MVP. He enjoys programming, playing with new
technology, and finding neat solutions to problems. When
he’s not working, you can find him cycling or walking around
the Peak District, playing table tennis, or trying to cook for
his wife and two children. You can follow him on Twitter at
@paul_michaels or find him on LinkedIn. He also writes a
blog at http://pmichaels.net.

xi
About the Technical Reviewer
Kasam Shaikh is an Azure AI enthusiast, published author,
global speaker, community MVP, and Microsoft Docs
contributor. He has more than 14 years of experience in
the IT industry and is a regular speaker at various meetups,
online communities, and international conferences on Azure
and AI. He is currently working as Senior Cloud Architect for
a multinational firm where he leads multiple programs in the
Practice for Microsoft Cloud Platform and Low Code. He is
also a founder of the community named Dear Azure-Azure
INDIA (az-India) and leads the community for learning
Microsoft Azure. He owns a YouTube channel and website
and shares his experiences over his website
(www.kasamshaikh.com).

xiii
Acknowledgments
For this book, I have an absolute phone book of people to thank for their help.
Firstly, I have to thank my daughter, Abi, for all the artwork in the book.
Special thanks to Ash Burgess and Kevin Smith for putting up with impromptu
requests for a discussion or a review of an idea, and for Kev’s painstaking and repeated
explanations of event sourcing.
Thanks to Ian Curtis for reviewing my architectural diagram, and to James Little for
making me think so hard about architecture that a book seemed a good idea.
The travel industry is very complex, and I don’t think I could have landed in a better
place, where I had the expertise and advice of both David Hilton and Jagdip Ajimal.
When it came to containers, I found that what I was trying to do kept throwing up
roadblocks until Rob Richardson generously offered his time and expertise.
The examples in this book are based on my time working across industries and trying
to solve problems. I’ve been fortunate in my career to work with a number of very clever
and talented people. There are probably too many of these to mention, and while they
may not have directly helped with this book, I’d like to acknowledge their contribution.
Finally, I’d like to thank the team at Apress – especially Shrikant for dealing with the
various twists and turns that the book, and my life, has taken during its creation, and
Smriti for bringing me onto Apress in the first place.

xv
Introduction
All the code in this book is available from the following GitHub repo:

https://github.com/Apress/software-­architecture-­by-­example

Should you choose to follow along and create the solution for each chapter, it may be
helpful to have a clone of the code available for reference.

Note As I’ll be working on a Windows machine, what I do will only be tested on


Windows; however, again, this is software architecture, not software architecture
for Windows, so everything should also work on MacOS, Linux, or any other
modern OS: .Net is a cross-platform framework.

T echnology
Let’s quickly discuss the specific technologies that we’re going to choose and why.
Firstly, I will use Visual Studio to write all of the code samples in this book. You can
download the community edition of this here:

https://visualstudio.microsoft.com/vs/community/

You may also wish to use VS Code, which can be found here:

https://code.visualstudio.com/download

All of the applications that we create will be in .Net and written in C#. This choice
I made simply because it’s the language that I’m most familiar with; however, I don’t
believe there’s anything in here that couldn’t be translated to any other modern OO
development language; after all, architecture should be language agnostic. Most of the
principles are broader than a specific language and could apply to any language capable
of making HTTP calls.

xvii
Introduction

S
 etup
In this section, we’ll cover the basic setup that you’ll need to follow along with the code
samples. However, since this is predominantly a book on architecture, you should be
able to translate the concepts to any language.
Let’s cover a basic setup for those readers that wish to follow along.

T erminal
If you choose to use VS Code, you have a terminal built in; however, there are other
options.
Since you’re likely to be using Git, you can easily use git bash for the terminal
commands; you can download git for windows here:

https://gitforwindows.org/

Another possibility if you’re on Windows is the new (at least at the time of writing)
Microsoft Terminal. This can be found here:

https://aka.ms/terminal

Note This is, in fact, an open source product; you can find the source code for
it here:
https://github.com/Microsoft/Terminal

E xamples
The title of this book is Software Architecture by Example, so it will not surprise you to
learn that there are examples in each chapter. The purpose is to propose a problem,
suggest one or more solutions, and provide an example of how that solution might work
in reality.
What this doesn’t mean is that contained within the pages of this book are full,
complete, solutions to each problem. To illustrate my point by example, for the first
chapter, we address the problem of a business that sells tickets for concerts and festivals.

xviii
Introduction

In that chapter, there are code samples that will compile and run, but those samples are
for illustration purposes; there’s no website there, but I’ve made sure that each element
of the system is there by proxy – so the website will be simulated by a console app.

How to Use This Book


There are a number of ways that you may choose to use this book. Each chapter has
an explanation of an architectural principle, driven by the typical requirement that it
satisfies; once this has been explained, there is an example in each chapter.
All of the code for every chapter can be found here:

https://github.com/Apress/software-­architecture-­by-­example

You can choose to follow along and recreate the examples, or you can clone the
repo and simply view the code, or you may decide that you’re not interested in a specific
implementation, in which case, you can simply skip the examples altogether.

xix
Other documents randomly have
different content
THE SLEEPING BEAUTY

At the same moment there was a deep silence everywhere in the


castle. The little bird that just before had been singing so sweetly on
the windowsill hushed its song. The distant hum of voices from the
courtyard beneath was stilled. Even the old woman, who had been
standing beside her wheel telling the princess how to spin, stopped
short and fell asleep. In the great hall, the king and queen, who had
just returned, and were inquiring for their daughter, fell asleep before
the lady-in-waiting could answer them, and the lady herself began to
snore. The guards slumbered at their posts. The horses in their stalls
became motionless, and so did the dogs in the yard, the pigeons on
the roof, and the flies on the wall. The fire on the hearth stopped
burning, and the meat on the spit ceased roasting. In short, sleep fell
on the whole castle, and round about it there sprung up a thick and
thorny magic wood which it seemed impossible for anyone to
penetrate, and which hid the entire castle from view except a
weather-vane on the roof.
Time went on until a hundred years had passed, and then one day
a king’s son happened to be hunting in the region. He became
separated from his attendants in the excitement of the chase, and at
length he came to a woodcutter’s cottage and dismounted to ask the
way. The old man who lived in the hut gave him the required
directions, and then told the prince about a thick wood a little farther
on in the direction he had been riding. “No one has ever been able to
get through that wood,” said the old man, “and my grandfather used
to say it surrounded a castle in which was a beautiful princess
condemned to sleep for a hundred years. He said some prince would
come and awaken her with a kiss.”
On hearing this, nothing would do but the prince must go and have
a look at the wood. He found it, and dismounted and prepared to
push his way through the thorny thicket. But no sooner did he start to
penetrate the wood than the tangled briars of the undergrowth were
changed into beautiful flowers which parted and bent aside to let him
pass. When he reached the courtyard he saw the dogs lying asleep,
and on the roof the pigeons were sitting with their heads under their
wings. He went indoors, and there were the flies asleep on the wall,
and there was the cook with his hand uplifted to strike the kitchen
boy, and a maid sitting near by had a fowl on her lap ready to pluck.
When the prince entered the great hall he found the whole court
asleep, and the king and queen slumbering on their thrones.
Everything was so still he could hear his own breathing.
As yet he saw no princess, and he continued looking about till he
came to the old tower and ascended the narrow, winding stair. He
went into the little room where the princess lay, and she looked so
lovely in her sleep that he could not turn away his eyes, and
presently he stooped and kissed her. At once she awoke and said:
“O prince, are you here at last? I have had such pleasant dreams!”
She sat up laughing and rubbing her eyes, and after a few
moments stood on her feet, and they went hand in hand out of the
room. The old woman stared at them in amazement, and then,
mumbling to herself, resumed her spinning. They descended the
stairs and passed along the corridors until they came to the throne
room. The king and queen and whole court had just waked up and
were gazing at each other with wonderment. The long sleep was
ended for the rest of the palace also. Roosters crowed, dogs barked,
the cats began to mew, the clocks struck the hours, the heralds blew
their trumpets, the pigeons flew away from the roof to the fields, the
kitchen fire blazed up, and the meat was again roasting, the cook
gave the kitchen boy such a box on the ear that he roared lustily, and
the maid began to pluck the fowl.
In short, everything went on as if there had been no enchantment
at all. To be sure, the dress the princess was wearing was such as
the prince’s great-grandmother might have worn, but that gave them
something to laugh at.
As soon as preparations could be made, the wedding of the prince
and princess was celebrated with great splendor, and they lived
happily ever after.
THE LOVE OF THE SNOW-WHITE FOX

O
NCE upon a time there lived a young fox that was snow-white,
and it was so gentle and intelligent that it was beloved by all
the good people for miles around. If, in the evening, it
knocked softly at their doors with its tail they were glad, and were
quick to let it in. When it entered it would play with the children, eat
of their humble fare, and then trot away. But there were hunters in
the country who wanted to kill the beautiful white fox. Once or twice it
nearly lost its life at the hands of these cruel men.
One summer afternoon, as it was frisking about in the woods with
some young fox friends, two men caught sight of it. They were fleet
of foot and had dogs with them. Away ran the white fox, and the men
uttered an excited cry and gave chase. Instead of going deeper into
the forest the fox ran across the open farm lands until it came to a
holy temple. “There, surely, I will find a safe refuge from my
pursuers,” it thought.
In the temple there happened to be a young prince of noble family
named Yashi, deep in meditation. The white fox, whose strength was
nearly spent, came running in at the door and went directly to the
prince and took refuge behind him. The poor creature trembled with
fright, and Yashi took pity on it and did all he could to calm its fears.
“I will protect you, little one,” said he. “No one shall harm you.”
The fox looked up at him and seemed to understand his words. It
ceased to tremble. The prince went to the door of the great temple.
Two men hastened up to him and asked if he had seen a snow-white
fox. “It must have run into the temple,” they declared.
But
Yashi,
faithful
to his
promis
e,
answer
ed, “I
have
been in
the
temple
praying, but I can tell you
nothing of the fox.”
The men were about to go on
when they caught a glimpse of
the fox behind him. Fiercely they
demanded that he should stand
aside. The prince firmly refused. Then the men, intent on having their
prey, attacked him, and he was obliged to draw his sword in self-
defense. At this moment Yashi’s father, a brave old man, came up.
He rushed on the assailants of his son, but a deadly blow, which
Yashi could not avert, struck the old man down. This made Yashi
very wroth, and with two mighty strokes he felled his adversaries to
the ground.
The loss of his father filled Yashi with grief, and as he stood
looking down on the body his heart was very heavy. Just then a
sweet song from within the sacred building greeted his ears. Who
could the singer be? for there was no one inside when he came out.
He reëntered the temple, and a beautiful maiden appeared before
him. He saw from her look and manner that she knew he was in
deep trouble, and he told her of the snow-white fox and the cruel
hunters, and of the death of his father. Then the maiden spoke to
Yashi tender words of sympathy, and her voice was so kindly and
gentle that even the sound of it brought comfort to him.
Presently he asked her who she was, and she replied that she
was a homeless stranger. So he insisted that she should dwell with
him. As the days passed she constantly became more attractive to
him, until he loved her more than anyone else in the world and asked
her to be his bride.
“I already love you,” she replied. “I know that you are good and
brave, and I would solace you for the loss of your father.”
So they were married and lived happily together. Time passed
swiftly, and Yashi ruled his people wisely. At length a son was born to
the prince and princess, and they were more happy than ever. But
one day Yashi noticed that the princess was sorely troubled. For
hours she sat alone, and tears sprang to her eyes when Yashi asked
her the cause of her sorrow.
She took his hand and said: “My life with you has been very
delightful. But now that I have given you a son to be with you always,
I must leave you. I am the snow-white fox whose life you saved.”
Once again she looked into his eyes, and then without another
word was gone. Yashi and his son lived long and were greatly
beloved, but the snow-white fox was seen no more.
THE GRAZIER’S WIFE

I
N a certain valley, long ago, there dwelt a grazier who had a wife
named Barbara. The grazier was famous for his valor in
encountering wolves, and there was not in all the valley a man
who was his match in handling the quarter-staff. Moreover, so expert
was he with a sling that he could hurl a stone a distance of a
hundred yards and hit a deer between the eyes, and so kill it. With
his knife he was equally skilful, and he was greatly feared in a
quarrel. Yet in spite of all his prowess and courage he quailed before
his wife Barbara.
She was no longer young, and her beauty was a thing of the past,
but she was a woman who made herself respected. She never failed
to produce a startling effect on her husband when she visited him as
he was tending his herds on the mountain-sides, for no other woman
ever had such a tongue. He often prayed to the saints for relief, but
she continued to both plague him with her tongue and mark him with
her nails.
At last he applied for advice to an old wizard who lived in a
neighboring valley. He had begun telling of his troubles when the
wizard interrupted him and said: “I see plainly that you are
complaining bitterly, but I would have you know that I am deaf, and
no matter how violently you shout and jump and gesture, what you
say or do will have no effect on me. Nevertheless, let me tell you,
that if you have some bright yellow gold to bestow on me, you will be
heard and understood. Yes, I would hear and comprehend, even if
you were dumb and had no voice whatever.”
“I will hasten to the market,” said the grazier, “and sell some of my
finest beasts, and the money that I receive for them I will gladly give
to you.”
So away he went and sold some of his beasts and returned to the
wizard and counted out the gold-pieces one by one. Then the wizard
listened patiently to his story and sent him home with a promise of
speedy relief.
That very night, after the grazier and his wife were in bed, and the
latter was delivering a lengthy lecture on his lack of breeding in
snoring when a lady was speaking, a white figure appeared at the
bedside with a mirror in its hand.
“Barbara,” said the specter, “your virtues are known to me, and as
a reward you shall be restored to youth and beauty, which you shall
yourself behold when you look into this mirror. But beware lest angry
or vain words pass your lips, for such a lapse will be punished by
hideous old age and infirmity.”
So saying, the apparition vanished. Barbara lit a lamp and
occupied herself in admiring her reflection in the magic mirror. Thus
the grazier was enabled to enjoy an unbroken sleep till morning, a
thing he had not done for years. He had peace also on the morrow
and ever after, for Barbara never allowed the mirror to pass out of
her possession, and it was a constant solace to her even to the day
of her death.
THE MAGIC HORN

O
NCE upon a time there was a poor farmer who had three
sons, and the sons’ names were Peter, Paul, and Philip. None
of the three liked work very well, and instead of helping their
father they spent most of their time sauntering about. At last Peter
heard that the king wanted a keeper to watch his rabbits. So the
youth told his father that he would go to the king’s palace and apply
for the position.
“I doubt if you are fitted for just that sort of work,” said his father.
“He who keeps the king’s rabbits needs to be light and quick, and no
lazy-bones. You could not loiter when the rabbits began to skip and
frisk, for if you dawdled as you do at home you would be
discharged.”
But the father’s advice had no effect. Peter was determined to go,
and after filling a bag with something to eat and drink and a few
other necessaries, he took the bag on his back and started. He had
not traveled many miles when he heard a voice calling for help. On
going in the direction of the sound he found an old woman in a pit
from which she was unable to climb out. “Don’t stand there staring,”
said she sharply. “Reach me your hand and pull me up. I have been
in this pit a whole year, and in all that time I have not had a morsel of
food.”
“What!” exclaimed Peter, “a whole year, do you say? Then you
must be a witch, or you could not fast so long, and I will have nothing
to do with you.”
So off he marched. At length he arrived at the king’s palace and
was engaged as the keeper of the rabbits. He was promised plenty
of food and good pay and maybe the princess into the bargain, for
the king had decreed that any keeper who took such good care of
the rabbits that not one of them escaped should have the princess
for his wife.
The next day Peter let the rabbits out to browse. As long as they
were near the stables and in the adjacent open fields he kept them in
one flock, but toward evening they got into a wood and began to
scuttle about among the trees. Peter ran after them this way and that
until he had no breath left for any more running. He could not get the
rabbits together. They all disappeared, and he saw nothing more of
them.
After resting a while he started to go back to the palace. As he
went along he kept a sharp look-out, and he stopped to call his
fugitive charges at every fence. But no rabbits came, and when he
reached the palace there stood the king waiting for him. It was plain
that Peter had failed, and for a punishment he was banished from
the country.
The king presently got a new lot of rabbits, and then he let it be
known that he wanted a keeper. Peter’s brother Paul heard of this,
and nothing would do but he must try for the place. Away he went,
and by and by he found the old woman in the pit just as Peter had,
and he would not help her out. When he got to the palace he was
promptly engaged as keeper of the rabbits, and the next day he let
them out to feed. All went well until in the late afternoon they went
from the fields into the woods. Then they skipped and hopped away,
and though he rushed about and raced after them till he was ready
to drop, they all escaped. So he returned to the palace without a
rabbit, and the king ordered that he should leave the country.
More rabbits were obtained to replace those lost, and again word
went forth that his Majesty wanted a keeper for them. Philip, the
youngest of the three brothers, heard of this and concluded to apply
for the job. “It will be just the right work for me,” he said to his father.
“I would like nothing better than to spend my days in the fields and
woodlands watching the rabbit flock, and I would be sure to have
plenty of time to nap on the sunny hillsides.”
“I fear,” said the old farmer, “that you will fare no better than your
two brothers. The person who keeps the king’s rabbits must not be
like a fellow with leaden soles to his shoes, or like a fly in a tar-pot.”
“Well,” responded Philip, “however things may turn out, I shall get
the job if I can. Surely it will be no harder than to take care of the calf
and goat here at home.”
So he packed his bag, lifted it to his shoulder, and started for the
palace of the king. He trudged along until he heard a voice calling,
and when he looked about he saw the old woman in the pit. “Good
day, grandmother,” said he, “what can I do for you?”
“Help me out of this hole,” she said, “and give me something to
eat. I will do you a good turn afterward, you may depend on it.”
He was willing enough, and he pulled her out of the pit. Then he
opened his bag and sat down to eat and drink with her. She had a
keen appetite after her long fast, and naturally got the lion’s share,
but that did not trouble Philip any. As soon as they finished, she gave
him a magic horn, and said: “If you blow into the small end of it,
whatever you wish away will be scattered to the four winds; and if
you blow into the large end the things you wish near will at once
come about you. Should the horn ever be lost or taken from you, all
you have to do is to wish for it, and it will return to you.”
“Very good,” responded Philip. “Such a horn is worth having.”
So saying, he resumed his walk, and at length he came to the
king’s palace. He was hired to keep the rabbits, and he was much
pleased, for he was certain of good food and generous wages, and if
he were clever enough not to lose any of the rabbits he might win the
princess, too. The next morning he began work, and at first he found
the task an easy one. As long as the rabbits were in the lanes and
fields they behaved very well, but while he was eating his noon lunch
they wandered to the woodland, where they frisked about and
scampered away into the underbrush.
“Ho, ho!” cried Philip, “you want to leave me, do you? Well, off with
you then,” and he blew into the small end of the magic horn.
Immediately they were all gone from view, and Philip found a
mossy spot to his liking and lay down to sleep till eventide. The sun
was low in the west when he awoke, and he took up his horn and
blew into the large end of it. At once the rabbits came frolicking
about him, and he led them like a flock of sheep to the king’s palace.
The king, the queen, and the princess, too, all came out on the porch
and wondered how he contrived to manage the rabbits so well.
Several times the king counted them to make sure they were all
there and he had to acknowledge that not one was missing.
“That rabbit-keeper would be a fine lad,” said the princess, “if only
he was of noble birth.”
The next day he took the rabbits out again, and when they roamed
to the woodland he lay down in the shade at the edge of the wood
close to the sunny slope where the wild strawberries grew and
scented the air with their sweet odor.
The king was curious to learn how the youth contrived to control
the rabbits so admirably, and he sent a servant to watch him. By and
by the servant came peeping about among the trees and spied Philip
asleep in the pleasant shade of the woodland. He hid in a thicket and
waited. Toward evening he saw Philip rise to his feet and blow his
horn, and immediately all the rabbits came scampering about him.
The servant hastened home and told the king what he had observed,
and the king told his wife and daughter.
“Unless we put a stop to his using that horn,” said the princess, “I
shall have to marry him, and he is only a common farmer’s son.
Tomorrow I shall go to the wood, and while he is asleep I will take his
horn and bring it home to the palace.”
She went to the wood just as she had planned, and she had little
trouble in getting possession of the horn. When Philip awoke it was
gone—and how was he to bring the rabbits together? But he
remembered that the old woman had said he could get it back by
wishing. So he wished for it; and the princess, who had nearly
reached the palace, felt it suddenly slip through her fingers, and
though she searched all about she could not find it. The horn had
returned to the hands of Philip in the woodland, and he immediately
blew it to fetch the rabbits together, and then he went with them to
the palace.
Philip blows into the large end of his horn

The royal family saw that Philip had the horn, and the queen said
she would go the next day and take it, and they might be sure she
would bring it home. The morrow came, and in the early afternoon
off she tramped to the wood. She secured the horn and hurried away
with it, holding it very tight, but as she approached the palace it
slipped from her grasp, and by and by the rabbit-keeper returned
with his horn and flock as usual.
“I shall have to look into this matter myself,” grumbled the king, “if
we are going to get that wretched horn into our possession. You
women plan all right, but it usually takes a man to carry a plan to a
successful conclusion.”
The following day, while Philip was having his nap in the wood, the
king came to the spot where the youth lay and took the horn. To
make doubly sure of it, the king put the horn in a bag he had brought
along for the purpose. Back he went to the palace. His wife and
daughter met him at the door, and he triumphantly opened the bag to
show them the horn; but it was not there. He had not succeeded any
better than the women folk. “Plague take the fellow!” he exclaimed.
“There is some magic about the way that horn disappears. The lad
gets the best of us every time, and I suppose he might as well marry
into the family first as last.”
Pretty soon Philip arrived with his flock of rabbits and put them in
their night quarters. Then he heard the king calling to him, and went
to the palace porch, where he found all the royal family waiting for
him. “What sort of a horn is that of yours?” asked the king. “It looks
ordinary enough, but I am sure it has some strange power or you
would not be able to take such excellent care of the rabbits and
never lose a single one of them.”
“It was given to me by an old woman,” said Philip, “and if I blow in
one end it does one thing, and if I blow in the other it does the
opposite.”
“Oh, bother your explanations!” cried the king. “Show us its power,
and then we shall understand.”
“But perhaps the showing would not please your Majesty,” said
Philip.
“Stuff and nonsense!” the king exclaimed. “I said, ‘Show us.’ Who
is king here—you or me? It is my business to command, and it is
yours to obey.”
“Very well,” responded Philip, “then I wish you to scatter;” and he
blew a good strong blast into the little end of his horn.
At once the king, very much against his will, and kicking savagely,
was hurried off north, the queen flew east, and the princess west,
and a little kitchen-maid, who had come up behind Philip and was
looking on, was hustled off south in such sudden haste it seemed to
her she would be scared out of a year’s growth.
“Stop me, you rascal! Bring me back!” yelled the king as he
vanished in the distance.
Philip turned the horn about and blew into the big end. In a few
moments the king and the others were back on the porch; and the
little maid, vastly astonished by her experiences, lost no time in
escaping to the kitchen. “What do you mean by treating me in that
fashion?” the king demanded. “You shall hang for it.”
Philip raised the little end of the horn to his lips, and the king,
fearful that he would have to repeat his wild race, called out:
“Enough! enough! The fault was mine. You shall have my daughter
and half the kingdom if only you won’t blow that horrible horn in my
presence. I’m too old and stiff to be dashing about over the country
as I did just now.”

You might also like