├── .DS_Store
├── .gitignore
├── CSharp9Cookbook
├── .DS_Store
├── .vscode
│ ├── launch.json
│ └── tasks.json
├── CSharp9Cookbook.sln
├── Chapter01
│ ├── Section-1-1
│ │ ├── DeploymentProcess.cs
│ │ ├── DeploymentReport.txt
│ │ ├── Program.cs
│ │ └── Section-1-1.csproj
│ ├── Section-1-10
│ │ ├── DeploymentBuilder.cs
│ │ ├── DeploymentService.cs
│ │ ├── Program.cs
│ │ └── Section-1-10.csproj
│ ├── Section-1-2
│ │ ├── DeploymentArtifacts.cs
│ │ ├── DeploymentRepository.cs
│ │ ├── DeploymentService.cs
│ │ ├── IDeploymentService.cs
│ │ ├── Program.cs
│ │ └── Section-1-2.csproj
│ ├── Section-1-3
│ │ ├── IValidatorFactory.cs
│ │ ├── Program.cs
│ │ ├── Section-1-3.csproj
│ │ ├── ThirdPartyDeploymentService.cs
│ │ └── ValidatorFactory.cs
│ ├── Section-1-4
│ │ ├── DeploymentManagementBase.cs
│ │ ├── DeploymentManager1.cs
│ │ ├── DeploymentManager2.cs
│ │ ├── DeploymentPlugin1.cs
│ │ ├── DeploymentPlugin2.cs
│ │ ├── IDeploymentPlugin.cs
│ │ ├── Program.cs
│ │ └── Section-1-4.csproj
│ ├── Section-1-5
│ │ ├── MultiLayer
│ │ │ ├── EnterpriseApp.CmdLine
│ │ │ │ ├── EnterpriseApp.CmdLine.csproj
│ │ │ │ ├── Program.cs
│ │ │ │ └── View
│ │ │ │ │ ├── Menu.cs
│ │ │ │ │ └── SignIn.cs
│ │ │ ├── EnterpriseApp.Data
│ │ │ │ ├── EnterpriseApp.Data.csproj
│ │ │ │ └── GreetingRepository.cs
│ │ │ └── EnterpriseApp
│ │ │ │ ├── EnterpriseApp.csproj
│ │ │ │ └── Greeting.cs
│ │ ├── SeparateData
│ │ │ ├── SystemApp.Console
│ │ │ │ ├── Model
│ │ │ │ │ └── Greeting.cs
│ │ │ │ ├── Program.cs
│ │ │ │ ├── SystemApp.Console.csproj
│ │ │ │ └── View
│ │ │ │ │ ├── Menu.cs
│ │ │ │ │ └── SignIn.cs
│ │ │ └── SystemApp.Data
│ │ │ │ ├── GreetingRepository.cs
│ │ │ │ └── SystemApp.Data.csproj
│ │ └── SingleProject
│ │ │ └── SimpleApp
│ │ │ ├── Data
│ │ │ └── GreetingRepository.cs
│ │ │ ├── Model
│ │ │ └── Greeting.cs
│ │ │ ├── Program.cs
│ │ │ ├── SimpleApp.csproj
│ │ │ └── View
│ │ │ ├── Menu.cs
│ │ │ └── SignIn.cs
│ ├── Section-1-6
│ │ ├── DeploymentService.cs
│ │ ├── Program.cs
│ │ ├── Section-1-6.csproj
│ │ └── ValidationStatus.cs
│ ├── Section-1-7
│ │ ├── CircularQueueGeneric.cs
│ │ ├── CircularQueueObjects.cs
│ │ ├── Deployment.cs
│ │ ├── HealthChecksGeneric.cs
│ │ ├── HealthChecksObjects.cs
│ │ ├── Program.cs
│ │ └── Section-1-7.csproj
│ ├── Section-1-8
│ │ ├── DeploymentService1.cs
│ │ ├── DeploymentService2.cs
│ │ ├── IDeploymentService.cs
│ │ ├── Program.cs
│ │ ├── Section-1-8.csproj
│ │ ├── ThirdPartyDeploymentAdapter.cs
│ │ └── ThirdPartyDeploymentService.cs
│ └── Section-1-9
│ │ ├── DeploymentService.cs
│ │ ├── DeploymentValidationException.cs
│ │ ├── Program.cs
│ │ ├── Section-1-9.csproj
│ │ └── ValidationFailureReason.cs
├── Chapter02
│ ├── Section-02-01
│ │ ├── Program.cs
│ │ └── Section-02-01.csproj
│ ├── Section-02-02
│ │ ├── Invoice.txt
│ │ ├── Program.cs
│ │ └── Section-02-02.csproj
│ ├── Section-02-03
│ │ ├── Program.cs
│ │ └── Section-02-03.csproj
│ ├── Section-02-04
│ │ ├── BankInvoice.cs
│ │ ├── EnterpriseInvoice.cs
│ │ ├── GovernmentInvoice.cs
│ │ ├── IInvoice.cs
│ │ ├── Program.cs
│ │ └── Section-02-04.csproj
│ ├── Section-02-05
│ │ ├── Invoice.cs
│ │ ├── Program.cs
│ │ └── Section-02-05.csproj
│ ├── Section-02-06
│ │ ├── Program.cs
│ │ └── Section-02-06.csproj
│ ├── Section-02-07
│ │ ├── Program.cs
│ │ └── Section-02-07.csproj
│ ├── Section-02-08
│ │ ├── IInvoiceRepository.cs
│ │ ├── InvoiceCategory.cs
│ │ ├── InvoiceRepository.cs
│ │ ├── Program.cs
│ │ └── Section-02-08.csproj
│ ├── Section-02-09
│ │ ├── IInvoiceRepository.cs
│ │ ├── InvoiceCategory.cs
│ │ ├── InvoiceRepository.cs
│ │ ├── Program.cs
│ │ └── Section-02-09.csproj
│ └── Section-02-10
│ │ ├── Program.cs
│ │ └── Section-02-10.csproj
├── Chapter03
│ ├── Section-03-01
│ │ └── OrdersLibrary
│ │ │ ├── CustomerType.cs
│ │ │ ├── Order.cs
│ │ │ └── OrdersLibrary.csproj
│ ├── Section-03-02
│ │ ├── CompanyOrder.cs
│ │ ├── CustomerOrder.cs
│ │ ├── IOrder.cs
│ │ ├── Program.cs
│ │ └── Section-03-02.csproj
│ ├── Section-03-03
│ │ ├── Program.cs
│ │ └── Section-03-03.csproj
│ ├── Section-03-04
│ │ ├── OrderLibraryNonNull.cs
│ │ ├── OrderLibraryWithNull.cs
│ │ ├── Program.cs
│ │ └── Section-03-04.csproj
│ ├── Section-03-05
│ │ ├── Delivery.cs
│ │ ├── Order.cs
│ │ ├── Program.cs
│ │ └── Section-03-05.csproj
│ ├── Section-03-06
│ │ ├── Order.cs
│ │ ├── Program.cs
│ │ └── Section-03-06.csproj
│ ├── Section-03-07
│ │ ├── OrderOrchestrator.cs
│ │ ├── Orders.cs
│ │ ├── Program.cs
│ │ └── Section-03-07.csproj
│ ├── Section-03-08
│ │ ├── Program.cs
│ │ └── Section-03-08.csproj
│ ├── Section-03-09
│ │ ├── Program.cs
│ │ └── Section-03-09.csproj
│ └── Section-03-10
│ │ ├── Program.cs
│ │ └── Section-03-10.csproj
├── Chapter04
│ ├── Section-04-01
│ │ ├── InMemoryContext.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-01.csproj
│ ├── Section-04-02
│ │ ├── InMemoryContext.cs
│ │ ├── Product.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-02.csproj
│ ├── Section-04-03
│ │ ├── InMemoryContext.cs
│ │ ├── Product.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-03.csproj
│ ├── Section-04-04
│ │ ├── InMemoryContext.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-04.csproj
│ ├── Section-04-05
│ │ ├── InMemoryContext.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-05.csproj
│ ├── Section-04-06
│ │ ├── InMemoryContext.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-06.csproj
│ ├── Section-04-07
│ │ ├── InMemoryContext.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-07.csproj
│ ├── Section-04-08
│ │ ├── InMemoryContext.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-08.csproj
│ ├── Section-04-09
│ │ ├── CookbookExtensions.cs
│ │ ├── InMemoryContext.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-09.csproj
│ └── Section-04-10
│ │ ├── InMemoryContext.cs
│ │ ├── Program.cs
│ │ ├── SalesPerson.cs
│ │ └── Section-04-10.csproj
├── Chapter05
│ ├── .DS_Store
│ ├── Section-05-01
│ │ ├── ColumnAttribute.cs
│ │ ├── InventoryItem.cs
│ │ ├── Program.cs
│ │ ├── Report.cs
│ │ └── Section-05-01.csproj
│ ├── Section-05-02
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── InventoryItem.cs
│ │ ├── Program.cs
│ │ ├── Report.cs
│ │ └── Section-05-02.csproj
│ ├── Section-05-03
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── GeneratorBase.cs
│ │ ├── HtmlGenerator.cs
│ │ ├── InventoryItem.cs
│ │ ├── MarkdownGenerator.cs
│ │ ├── Program.cs
│ │ ├── Report.cs
│ │ ├── ReportType.cs
│ │ └── Section-05-03.csproj
│ ├── Section-05-04
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── InventoryItem.cs
│ │ ├── Program.cs
│ │ ├── Report.cs
│ │ └── Section-05-04.csproj
│ ├── Section-05-05
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── Inventory.cs
│ │ ├── InventoryItem.cs
│ │ ├── Program.cs
│ │ ├── Report.cs
│ │ └── Section-05-05.csproj
│ ├── Section-05-06
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── ExcelDynamicGenerator.cs
│ │ ├── ExcelTypedGenerator.cs
│ │ ├── GeneratorBase.cs
│ │ ├── HtmlGenerator.cs
│ │ ├── InventoryItem.cs
│ │ ├── MarkdownGenerator.cs
│ │ ├── Program.cs
│ │ ├── Report.cs
│ │ ├── ReportType.cs
│ │ └── Section-05-06.csproj
│ ├── Section-05-07
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── DynamicLog.cs
│ │ ├── LogEntry.cs
│ │ ├── Program.cs
│ │ ├── Report.cs
│ │ └── Section-05-07.csproj
│ ├── Section-05-08
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── LogEntry.cs
│ │ ├── Program.cs
│ │ ├── Report.cs
│ │ └── Section-05-08.csproj
│ ├── Section-05-09
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── Program.cs
│ │ ├── Report.cs
│ │ ├── Section-05-09.csproj
│ │ ├── Semantic.py
│ │ └── Tweet.cs
│ ├── Section-05-10-old
│ │ ├── .DS_Store
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── InventoryItem.cs
│ │ ├── MainApp.py
│ │ ├── Report.cs
│ │ └── Section-05-10.csproj
│ └── Section-05-10
│ │ ├── ColumnAttribute.cs
│ │ ├── ColumnDetail.cs
│ │ ├── InventoryItem.cs
│ │ ├── MainApp.py
│ │ ├── Properties
│ │ └── AssemblyInfo.cs
│ │ ├── Report.cs
│ │ └── Section-05-10.csproj
├── Chapter06
│ ├── .DS_Store
│ ├── Section-06-01
│ │ ├── .DS_Store
│ │ ├── CheckoutService.cs
│ │ ├── Program.cs
│ │ └── Section-06-01.csproj
│ ├── Section-06-02
│ │ ├── CheckoutService.cs
│ │ ├── Program.cs
│ │ └── Section-06-02.csproj
│ ├── Section-06-03
│ │ ├── CheckoutRequest.cs
│ │ ├── CheckoutService.cs
│ │ ├── CheckoutStream.cs
│ │ ├── Program.cs
│ │ └── Section-06-03.csproj
│ ├── Section-06-04
│ │ ├── CheckoutService.cs
│ │ ├── Program.cs
│ │ └── Section-06-04.csproj
│ ├── Section-06-05
│ │ ├── CheckoutRequest.cs
│ │ ├── CheckoutRequestProgress.cs
│ │ ├── CheckoutService.cs
│ │ ├── CheckoutStream.cs
│ │ ├── Program.cs
│ │ └── Section-06-05.csproj
│ ├── Section-06-06
│ │ ├── CheckoutService.cs
│ │ ├── Program.cs
│ │ └── Section-06-06.csproj
│ ├── Section-06-07
│ │ ├── CheckoutService.cs
│ │ ├── Program.cs
│ │ └── Section-06-07.csproj
│ ├── Section-06-08
│ │ ├── CheckoutService.cs
│ │ ├── Program.cs
│ │ └── Section-06-08.csproj
│ ├── Section-06-09
│ │ ├── CheckoutRequest.cs
│ │ ├── CheckoutRequestProgress.cs
│ │ ├── CheckoutService.cs
│ │ ├── CheckoutStream.cs
│ │ ├── Program.cs
│ │ └── Section-06-09.csproj
│ └── Section-06-10
│ │ ├── CheckoutRequest.cs
│ │ ├── CheckoutRequestProgress.cs
│ │ ├── CheckoutService.cs
│ │ ├── CheckoutStream.cs
│ │ ├── ConsoleLogger.cs
│ │ ├── ILogger.cs
│ │ ├── Program.cs
│ │ └── Section-06-10.csproj
├── Chapter07
│ ├── .DS_Store
│ ├── Section-07-01
│ │ ├── Program.cs
│ │ └── Section-07-01.csproj
│ ├── Section-07-02
│ │ ├── Crypto.cs
│ │ ├── Program.cs
│ │ └── Section-07-02.csproj
│ ├── Section-07-03
│ │ ├── Program.cs
│ │ └── Section-07-03.csproj
│ ├── Section-07-04
│ │ ├── Program.cs
│ │ ├── PurchaseItem.cs
│ │ ├── PurchaseOrder.cs
│ │ ├── PurchaseOrderService.cs
│ │ ├── PurchaseOrderStatus.cs
│ │ └── Section-07-04.csproj
│ ├── Section-07-05
│ │ ├── Program.cs
│ │ ├── PurchaseItem.cs
│ │ ├── PurchaseOrder.cs
│ │ ├── PurchaseOrderService.cs
│ │ ├── PurchaseOrderStatus.cs
│ │ ├── PurchaseOrderStatusConverter.cs
│ │ ├── Section-07-05.csproj
│ │ └── SnakeCaseNamingPolicy.cs
│ ├── Section-07-06
│ │ ├── JsonConversionExtensions.cs
│ │ ├── Program.cs
│ │ ├── PurchaseItem.cs
│ │ ├── PurchaseOrder.cs
│ │ ├── PurchaseOrderService.cs
│ │ ├── PurchaseOrderStatus.cs
│ │ └── Section-07-06.csproj
│ ├── Section-07-07
│ │ ├── Program.cs
│ │ ├── PurchaseItem.cs
│ │ ├── PurchaseOrder.cs
│ │ ├── PurchaseOrderStatus.cs
│ │ └── Section-07-07.csproj
│ ├── Section-07-08
│ │ ├── Program.cs
│ │ ├── PurchaseItem.cs
│ │ ├── PurchaseOrder.cs
│ │ ├── PurchaseOrderStatus.cs
│ │ └── Section-07-08.csproj
│ ├── Section-07-09
│ │ ├── Program.cs
│ │ ├── Section-07-09.csproj
│ │ └── Url.cs
│ └── Section-07-10
│ │ ├── Program.cs
│ │ ├── Section-07-10.csproj
│ │ └── StringExtensions.cs
├── Chapter08
│ ├── Section-08-01
│ │ ├── BronzeSchedule.cs
│ │ ├── GoldSchedule.cs
│ │ ├── IRoomSchedule.cs
│ │ ├── Program.cs
│ │ ├── Section-08-01.csproj
│ │ └── SilverSchedule.cs
│ ├── Section-08-02
│ │ ├── Program.cs
│ │ ├── Scheduler.cs
│ │ └── Section-08-02.csproj
│ ├── Section-08-03
│ │ ├── BronzeSchedule.cs
│ │ ├── GoldSchedule.cs
│ │ ├── IRoomSchedule.cs
│ │ ├── Program.cs
│ │ ├── ScheduleType.cs
│ │ ├── Scheduler.cs
│ │ ├── Section-08-03.csproj
│ │ └── SilverSchedule.cs
│ ├── Section-08-04
│ │ ├── Program.cs
│ │ ├── Room.cs
│ │ ├── ScheduleType.cs
│ │ └── Section-08-04.csproj
│ ├── Section-08-05
│ │ ├── Program.cs
│ │ ├── Room.cs
│ │ ├── ScheduleType.cs
│ │ └── Section-08-05.csproj
│ ├── Section-08-06
│ │ ├── Program.cs
│ │ ├── Room.cs
│ │ ├── ScheduleType.cs
│ │ └── Section-08-06.csproj
│ ├── Section-08-07
│ │ ├── BronzeSchedule.cs
│ │ ├── GoldSchedule.cs
│ │ ├── IRoomSchedule.cs
│ │ ├── Program.cs
│ │ ├── Section-08-07.csproj
│ │ └── SilverSchedule.cs
│ ├── Section-08-08
│ │ ├── BronzeSchedule.cs
│ │ ├── Customer.cs
│ │ ├── GoldSchedule.cs
│ │ ├── IRoomSchedule.cs
│ │ ├── Program.cs
│ │ ├── Section-08-08.csproj
│ │ └── SilverSchedule.cs
│ ├── Section-08-09
│ │ ├── Customer.cs
│ │ ├── Program.cs
│ │ └── Section-08-09.csproj
│ └── Section-08-10
│ │ ├── BronzeCustomer.cs
│ │ ├── BronzeSchedule.cs
│ │ ├── Customer.cs
│ │ ├── GoldCustomer.cs
│ │ ├── GoldSchedule.cs
│ │ ├── IRoomSchedule.cs
│ │ ├── Program.cs
│ │ ├── Section-08-10.csproj
│ │ ├── SilverCustomer.cs
│ │ └── SilverSchedule.cs
├── Chapter09
│ ├── Section-09-01
│ │ ├── AnotherFile.cs
│ │ ├── Program.cs
│ │ └── Section-09-01.csproj
│ ├── Section-09-02
│ │ ├── Address.cs
│ │ ├── Program.cs
│ │ └── Section-09-02.csproj
│ ├── Section-09-03
│ │ ├── Address.cs
│ │ ├── Program.cs
│ │ └── Section-09-03.csproj
│ ├── Section-09-04
│ │ ├── Program.cs
│ │ └── Section-09-04.csproj
│ ├── Section-09-05
│ │ ├── Program.cs
│ │ └── Section-09-05.csproj
│ ├── Section-09-06
│ │ ├── AddressBase.cs
│ │ ├── MailingAddress.cs
│ │ ├── Program.cs
│ │ ├── Section-09-06.csproj
│ │ └── ShippingAddress.cs
│ ├── Section-09-07
│ │ ├── AddressBase.cs
│ │ ├── Communications.cs
│ │ ├── DeliveryBase.cs
│ │ ├── MailingAddress.cs
│ │ ├── Program.cs
│ │ ├── Section-09-07.csproj
│ │ ├── Shipping.cs
│ │ └── ShippingAddress.cs
│ ├── Section-09-08
│ │ ├── Address.cs
│ │ ├── AddressExtensions.cs
│ │ ├── Program.cs
│ │ └── Section-09-08.csproj
│ ├── Section-09-09
│ │ ├── Address.cs
│ │ ├── AddressService.cs
│ │ ├── Program.cs
│ │ └── Section-09-09.csproj
│ └── Section-09-10
│ │ ├── AddressManager
│ │ ├── AddressManager.csproj
│ │ └── Program.cs
│ │ └── AddressUtilities
│ │ ├── Address.cs
│ │ ├── AddressRepository.cs
│ │ ├── AddressService.cs
│ │ ├── AddressUtilities.csproj
│ │ ├── IAddressRepository.cs
│ │ └── Initializer.cs
└── OrdersLibrary.Test
│ ├── OrderTests.cs
│ └── OrdersLibrary.Test.csproj
├── LICENSE.md
└── README.md
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/.DS_Store
--------------------------------------------------------------------------------
/CSharp9Cookbook/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/CSharp9Cookbook/.DS_Store
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-1/DeploymentProcess.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 |
4 | namespace Section_1_1
5 | {
6 | public class DeploymentProcess : IDisposable
7 | {
8 | bool disposed;
9 |
10 | readonly StreamWriter report = new StreamWriter("DeploymentReport.txt");
11 |
12 | public bool CheckStatus()
13 | {
14 | report.WriteLine($"{DateTime.Now} Application Deployed.");
15 |
16 | return true;
17 | }
18 |
19 | protected virtual void Dispose(bool disposing)
20 | {
21 | if (!disposed)
22 | {
23 | if (disposing)
24 | {
25 | // disposal of purely managed resources goes here
26 | }
27 |
28 | report?.Close();
29 | disposed = true;
30 | }
31 | }
32 |
33 | ~DeploymentProcess()
34 | {
35 | Dispose(disposing: false);
36 | }
37 |
38 | public void Dispose()
39 | {
40 | Dispose(disposing: true);
41 | GC.SuppressFinalize(this);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-1/DeploymentReport.txt:
--------------------------------------------------------------------------------
1 | 7/19/2021 9:10:36 PM Application Deployed.
2 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-1/Program.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_1
2 | {
3 | class Program
4 | {
5 | static void Main(string[] args)
6 | {
7 | using (var deployer = new DeploymentProcess())
8 | {
9 | deployer.CheckStatus();
10 | }
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-1/Section-1-1.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_1_1
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-10/DeploymentBuilder.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_10
2 | {
3 | public class DeploymentBuilder
4 | {
5 | DeploymentService service = new DeploymentService();
6 |
7 | public DeploymentBuilder SetStartDelay(int delay)
8 | {
9 | service.StartDelay = delay;
10 | return this;
11 | }
12 |
13 | public DeploymentBuilder SetErrorRetries(int retries)
14 | {
15 | service.ErrorRetries = retries;
16 | return this;
17 | }
18 |
19 | public DeploymentBuilder SetReportFormat(string format)
20 | {
21 | service.ReportFormat = format;
22 | return this;
23 | }
24 |
25 | public DeploymentService Build()
26 | {
27 | return service;
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-10/DeploymentService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_10
4 | {
5 | public class DeploymentService
6 | {
7 | public int StartDelay { get; set; } = 2000;
8 | public int ErrorRetries { get; set; } = 5;
9 | public string ReportFormat { get; set; } = "pdf";
10 |
11 | public void Start()
12 | {
13 | Console.WriteLine(
14 | $"Deployment started with:\n" +
15 | $" Start Delay: {StartDelay}\n" +
16 | $" Error Retries: {ErrorRetries}\n" +
17 | $" Report Format: {ReportFormat}");
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-10/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_10
4 | {
5 | class Program
6 | {
7 | static void Main()
8 | {
9 | DeploymentService service =
10 | new DeploymentBuilder()
11 | .SetStartDelay(3000)
12 | .SetErrorRetries(3)
13 | .SetReportFormat("html")
14 | .Build();
15 |
16 | service.Start();
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-10/Section-1-10.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_1_10
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-2/DeploymentArtifacts.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_2
2 | {
3 | public class DeploymentArtifacts
4 | {
5 | public void Validate()
6 | {
7 | System.Console.WriteLine("Validating...");
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-2/DeploymentRepository.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_2
2 | {
3 | public class DeploymentRepository
4 | {
5 | public void SaveStatus(string status)
6 | {
7 | System.Console.WriteLine("Saving status...");
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-2/DeploymentService.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_2
2 | {
3 | public class DeploymentService : IDeploymentService
4 | {
5 | readonly DeploymentArtifacts artifacts;
6 | readonly DeploymentRepository repository;
7 |
8 | public DeploymentService(
9 | DeploymentArtifacts artifacts,
10 | DeploymentRepository repository)
11 | {
12 | this.artifacts = artifacts;
13 | this.repository = repository;
14 | }
15 |
16 | public void PerformValidation()
17 | {
18 | artifacts.Validate();
19 | repository.SaveStatus("status");
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-2/IDeploymentService.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_2
2 | {
3 | interface IDeploymentService
4 | {
5 | void PerformValidation();
6 | }
7 | }
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-2/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.DependencyInjection;
2 | using System;
3 |
4 | namespace Section_1_2
5 | {
6 | class Program
7 | {
8 | readonly IDeploymentService service;
9 |
10 | public Program(IDeploymentService service)
11 | {
12 | this.service = service;
13 | }
14 |
15 | static void Main()
16 | {
17 | var services = new ServiceCollection();
18 |
19 | services.AddTransient();
20 | services.AddTransient();
21 | services.AddTransient();
22 |
23 | ServiceProvider serviceProvider = services.BuildServiceProvider();
24 |
25 | IDeploymentService deploymentService =
26 | serviceProvider.GetRequiredService();
27 |
28 | var program = new Program(deploymentService);
29 |
30 | program.StartDeployment();
31 | }
32 |
33 | public void StartDeployment()
34 | {
35 | service.PerformValidation();
36 | Console.WriteLine("Validation complete - continuing...");
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-2/Section-1-2.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_1_2
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-3/IValidatorFactory.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_3
2 | {
3 | public interface IValidatorFactory
4 | {
5 | ThirdPartyDeploymentService CreateDeploymentService();
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-3/Program.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_3
2 | {
3 | public class Program
4 | {
5 | readonly ThirdPartyDeploymentService service;
6 |
7 | public Program(IValidatorFactory factory)
8 | {
9 | service = factory.CreateDeploymentService();
10 | }
11 |
12 | static void Main()
13 | {
14 | var factory = new ValidatorFactory();
15 | var program = new Program(factory);
16 | program.PerformValidation();
17 | }
18 |
19 | void PerformValidation()
20 | {
21 | service.Validate();
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-3/Section-1-3.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_1_3
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-3/ThirdPartyDeploymentService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_3
4 | {
5 | public class ThirdPartyDeploymentService
6 | {
7 | public void Validate()
8 | {
9 | Console.WriteLine("Validated");
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-3/ValidatorFactory.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_3
2 | {
3 | public class ValidatorFactory : IValidatorFactory
4 | {
5 | public ThirdPartyDeploymentService CreateDeploymentService()
6 | {
7 | return new ThirdPartyDeploymentService();
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-4/DeploymentManagementBase.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_4
2 | {
3 | public abstract class DeploymentManagementBase
4 | {
5 | IDeploymentPlugin deploymentService;
6 |
7 | protected abstract IDeploymentPlugin CreateDeploymentService();
8 |
9 | public bool Validate()
10 | {
11 | if (deploymentService == null)
12 | deploymentService = CreateDeploymentService();
13 |
14 | return deploymentService.Validate();
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-4/DeploymentManager1.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_4
2 | {
3 | public class DeploymentManager1 : DeploymentManagementBase
4 | {
5 | protected override IDeploymentPlugin CreateDeploymentService()
6 | {
7 | return new DeploymentPlugin1();
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-4/DeploymentManager2.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_4
2 | {
3 | public class DeploymentManager2 : DeploymentManagementBase
4 | {
5 | protected override IDeploymentPlugin CreateDeploymentService()
6 | {
7 | return new DeploymentPlugin2();
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-4/DeploymentPlugin1.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_4
4 | {
5 | public class DeploymentPlugin1 : IDeploymentPlugin
6 | {
7 | public bool Validate()
8 | {
9 | Console.WriteLine("Validated Plugin 1");
10 | return true;
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-4/DeploymentPlugin2.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_4
4 | {
5 | public class DeploymentPlugin2 : IDeploymentPlugin
6 | {
7 | public bool Validate()
8 | {
9 | Console.WriteLine("Validated Plugin 2");
10 | return true;
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-4/IDeploymentPlugin.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_4
2 | {
3 | public interface IDeploymentPlugin
4 | {
5 | bool Validate();
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-4/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_4
4 | {
5 | class Program
6 | {
7 | readonly DeploymentManagementBase[] deploymentManagers;
8 |
9 | public Program(DeploymentManagementBase[] deploymentManagers)
10 | {
11 | this.deploymentManagers = deploymentManagers;
12 | }
13 |
14 | static DeploymentManagementBase[] GetPlugins()
15 | {
16 | return new DeploymentManagementBase[]
17 | {
18 | new DeploymentManager1(),
19 | new DeploymentManager2()
20 | };
21 | }
22 |
23 | static void Main()
24 | {
25 | DeploymentManagementBase[] deploymentManagers = GetPlugins();
26 |
27 | var program = new Program(deploymentManagers);
28 |
29 | program.Run();
30 | }
31 |
32 | void Run()
33 | {
34 | foreach (var manager in deploymentManagers)
35 | manager.Validate();
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-4/Section-1-4.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_1_4
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.CmdLine/EnterpriseApp.CmdLine.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.CmdLine/Program.cs:
--------------------------------------------------------------------------------
1 | using EnterpriseApp.CmdLine.View;
2 |
3 | namespace EnterpriseApp.CmdLine
4 | {
5 | class Program
6 | {
7 | SignIn signIn = new SignIn();
8 | Menu menu = new Menu();
9 |
10 | static void Main()
11 | {
12 | new Program().Start();
13 | }
14 |
15 | void Start()
16 | {
17 | signIn.Greet();
18 | menu.Show();
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.CmdLine/View/Menu.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace EnterpriseApp.CmdLine.View
6 | {
7 | public class Menu
8 | {
9 | public void Show()
10 | {
11 | Console.WriteLine(
12 | "*------*\n" +
13 | "* Menu *\n" +
14 | "*------*\n" +
15 | "\n" +
16 | "1. ...\n" +
17 | "2. ...\n" +
18 | "3. ...\n" +
19 | "\n" +
20 | "Choose: ");
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.CmdLine/View/SignIn.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace EnterpriseApp.CmdLine.View
4 | {
5 | public class SignIn
6 | {
7 | Greeting greeting = new Greeting();
8 |
9 | public void Greet()
10 | {
11 | Console.Write("Is this your first visit? (true/false): ");
12 | string newResponse = Console.ReadLine();
13 |
14 | bool.TryParse(newResponse, out bool isNew);
15 |
16 | string greetResponse = greeting.GetGreeting(isNew);
17 |
18 | Console.WriteLine($"\n*\n* {greetResponse} \n*\n");
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.Data/EnterpriseApp.Data.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp.Data/GreetingRepository.cs:
--------------------------------------------------------------------------------
1 | namespace EnterpriseApp.Data
2 | {
3 | public class GreetingRepository
4 | {
5 | public string GetNewGreeting() => "Welcome!";
6 |
7 | public string GetVisitGreeting() => "Welcome back!";
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp/EnterpriseApp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/MultiLayer/EnterpriseApp/Greeting.cs:
--------------------------------------------------------------------------------
1 | using EnterpriseApp.Data;
2 |
3 | namespace EnterpriseApp
4 | {
5 | public class Greeting
6 | {
7 | GreetingRepository greetRep = new GreetingRepository();
8 |
9 | public string GetGreeting(bool isNew) =>
10 | isNew ? greetRep.GetNewGreeting() : greetRep.GetVisitGreeting();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/Model/Greeting.cs:
--------------------------------------------------------------------------------
1 | using SystemApp.Data;
2 |
3 | namespace SystemApp.Console.Model
4 | {
5 | public class Greeting
6 | {
7 | GreetingRepository greetRep = new GreetingRepository();
8 |
9 | public string GetGreeting(bool isNew) =>
10 | isNew ? greetRep.GetNewGreeting() : greetRep.GetVisitGreeting();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/Program.cs:
--------------------------------------------------------------------------------
1 | using SystemApp.Console.View;
2 |
3 | namespace SystemApp.Console
4 | {
5 | class Program
6 | {
7 | SignIn signIn = new SignIn();
8 | Menu menu = new Menu();
9 |
10 | static void Main()
11 | {
12 | new Program().Start();
13 | }
14 |
15 | void Start()
16 | {
17 | signIn.Greet();
18 | menu.Show();
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/SystemApp.Console.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/View/Menu.cs:
--------------------------------------------------------------------------------
1 | namespace SystemApp.Console.View
2 | {
3 | public class Menu
4 | {
5 | public void Show()
6 | {
7 | System.Console.WriteLine(
8 | "*------*\n" +
9 | "* Menu *\n" +
10 | "*------*\n" +
11 | "\n" +
12 | "1. ...\n" +
13 | "2. ...\n" +
14 | "3. ...\n" +
15 | "\n" +
16 | "Choose: ");
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Console/View/SignIn.cs:
--------------------------------------------------------------------------------
1 | using SystemApp.Console.Model;
2 |
3 | namespace SystemApp.Console.View
4 | {
5 | public class SignIn
6 | {
7 | Greeting greeting = new Greeting();
8 |
9 | public void Greet()
10 | {
11 | System.Console.Write("Is this your first visit? (true/false): ");
12 | string newResponse = System.Console.ReadLine();
13 |
14 | bool.TryParse(newResponse, out bool isNew);
15 |
16 | string greetResponse = greeting.GetGreeting(isNew);
17 |
18 | System.Console.WriteLine($"\n*\n* {greetResponse} \n*\n");
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Data/GreetingRepository.cs:
--------------------------------------------------------------------------------
1 | namespace SystemApp.Data
2 | {
3 | public class GreetingRepository
4 | {
5 | public string GetNewGreeting() => "Welcome!";
6 |
7 | public string GetVisitGreeting() => "Welcome back!";
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SeparateData/SystemApp.Data/SystemApp.Data.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/Data/GreetingRepository.cs:
--------------------------------------------------------------------------------
1 | namespace SimpleApp.Data
2 | {
3 | public class GreetingRepository
4 | {
5 | public string GetNewGreeting() => "Welcome!";
6 |
7 | public string GetVisitGreeting() => "Welcome back!";
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/Model/Greeting.cs:
--------------------------------------------------------------------------------
1 | using SimpleApp.Data;
2 |
3 | namespace SimpleApp.Model
4 | {
5 | public class Greeting
6 | {
7 | GreetingRepository greetRep = new GreetingRepository();
8 |
9 | public string GetGreeting(bool isNew) =>
10 | isNew ? greetRep.GetNewGreeting() : greetRep.GetVisitGreeting();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/Program.cs:
--------------------------------------------------------------------------------
1 | using SimpleApp.View;
2 |
3 | namespace SimpleApp
4 | {
5 | class Program
6 | {
7 | SignIn signIn = new SignIn();
8 | Menu menu = new Menu();
9 |
10 | static void Main()
11 | {
12 | new Program().Start();
13 | }
14 |
15 | void Start()
16 | {
17 | signIn.Greet();
18 | menu.Show();
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/SimpleApp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/View/Menu.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SimpleApp.View
4 | {
5 | public class Menu
6 | {
7 | public void Show()
8 | {
9 | Console.WriteLine(
10 | "*------*\n" +
11 | "* Menu *\n" +
12 | "*------*\n" +
13 | "\n" +
14 | "1. ...\n" +
15 | "2. ...\n" +
16 | "3. ...\n" +
17 | "\n" +
18 | "Choose: ");
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-5/SingleProject/SimpleApp/View/SignIn.cs:
--------------------------------------------------------------------------------
1 | using SimpleApp.Model;
2 | using System;
3 |
4 | namespace SimpleApp.View
5 | {
6 | public class SignIn
7 | {
8 | Greeting greeting = new Greeting();
9 |
10 | public void Greet()
11 | {
12 | Console.Write("Is this your first visit? (true/false): ");
13 | string newResponse = Console.ReadLine();
14 |
15 | bool.TryParse(newResponse, out bool isNew);
16 |
17 | string greetResponse = greeting.GetGreeting(isNew);
18 |
19 | Console.WriteLine($"\n*\n* {greetResponse} \n*\n");
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-6/DeploymentService.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_6
2 | {
3 | public class DeploymentService
4 | {
5 | public
6 | (bool deployment, bool smokeTest, bool artifacts)
7 | PrepareDeployment()
8 | {
9 | ValidationStatus status = Validate();
10 |
11 | (bool deployment, bool smokeTest, bool artifacts) = status;
12 |
13 | return (deployment, smokeTest, artifacts);
14 | }
15 |
16 | ValidationStatus Validate()
17 | {
18 | return new ValidationStatus
19 | {
20 | Deployment = true,
21 | SmokeTest = true,
22 | Artifacts = true
23 | };
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-6/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_6
4 | {
5 | class Program
6 | {
7 | readonly DeploymentService deployment = new DeploymentService();
8 | static void Main(string[] args)
9 | {
10 | new Program().Start();
11 | }
12 |
13 | void Start()
14 | {
15 | (bool deployed, bool smokeTest, bool artifacts) =
16 | deployment.PrepareDeployment();
17 |
18 | Console.WriteLine(
19 | $"\nDeployment Status:\n\n" +
20 | $"Is Previous Deployment Complete? {deployed}\n" +
21 | $"Is Previous Smoke Test Complete? {smokeTest}\n" +
22 | $"Are artifacts for this deployment ready? {artifacts}\n\n" +
23 | $"Can deploy: {deployed && smokeTest && artifacts}");
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-6/Section-1-6.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_1_6
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-6/ValidationStatus.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_6
2 | {
3 | public class ValidationStatus
4 | {
5 | public bool Deployment { get; set; }
6 | public bool SmokeTest { get; set; }
7 | public bool Artifacts { get; set; }
8 |
9 | public void Deconstruct(
10 | out bool isPreviousDeploymentComplete,
11 | out bool isSmokeTestComplete,
12 | out bool areArtifactsReady)
13 | {
14 | isPreviousDeploymentComplete = Deployment;
15 | isSmokeTestComplete = SmokeTest;
16 | areArtifactsReady = Artifacts;
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-7/CircularQueueGeneric.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_7.Generic
4 | {
5 | public class CircularQueue
6 | {
7 | int current = 0;
8 | int last = 0;
9 | T[] items;
10 |
11 | public CircularQueue(int size)
12 | {
13 | items = new T[size];
14 | }
15 |
16 | public void Add(T obj)
17 | {
18 | if (last >= items.Length)
19 | throw new IndexOutOfRangeException();
20 |
21 | items[last++] = obj;
22 | }
23 |
24 | public T Next()
25 | {
26 | current %= last;
27 | T item = items[current];
28 | current++;
29 |
30 | return item;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-7/CircularQueueObjects.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_7.Objects
4 | {
5 | public class CircularQueue
6 | {
7 | int current = 0;
8 | int last = 0;
9 | object[] items;
10 |
11 | public CircularQueue(int size)
12 | {
13 | items = new object[size];
14 | }
15 |
16 | public void Add(object obj)
17 | {
18 | if (last >= items.Length)
19 | throw new IndexOutOfRangeException();
20 |
21 | items[last++] = obj;
22 | }
23 |
24 | public object Next()
25 | {
26 | current %= last;
27 | object item = items[current];
28 | current++;
29 |
30 | return item;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-7/Deployment.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_7
4 | {
5 | public class Deployment
6 | {
7 | string config;
8 |
9 | public Deployment(string config)
10 | {
11 | this.config = config;
12 | }
13 |
14 | public bool PerformHealthCheck()
15 | {
16 | Console.WriteLine($"Performed health check for config {config}.");
17 | return true;
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-7/HealthChecksGeneric.cs:
--------------------------------------------------------------------------------
1 | using Section_1_7.Generic;
2 |
3 | namespace Section_1_7
4 | {
5 | public class HealthChecksGeneric
6 | {
7 | public void PerformHealthChecks(int cycles)
8 | {
9 | CircularQueue checks = Configure();
10 |
11 | for (int i = 0; i < cycles; i++)
12 | {
13 | Deployment deployment = checks.Next();
14 | deployment.PerformHealthCheck();
15 | }
16 | }
17 |
18 | private CircularQueue Configure()
19 | {
20 | var queue = new CircularQueue(5);
21 |
22 | queue.Add(new Deployment("a"));
23 | queue.Add(new Deployment("b"));
24 | queue.Add(new Deployment("c"));
25 |
26 | return queue;
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-7/HealthChecksObjects.cs:
--------------------------------------------------------------------------------
1 | using Section_1_7.Objects;
2 |
3 | namespace Section_1_7
4 | {
5 | public class HealthChecksObjects
6 | {
7 | public void PerformHealthChecks(int cycles)
8 | {
9 | CircularQueue checks = Configure();
10 |
11 | for (int i = 0; i < cycles; i++)
12 | {
13 | Deployment deployment = (Deployment)checks.Next();
14 | deployment.PerformHealthCheck();
15 | }
16 | }
17 |
18 | private CircularQueue Configure()
19 | {
20 | var queue = new CircularQueue(5);
21 |
22 | queue.Add(new Deployment("a"));
23 | queue.Add(new Deployment("b"));
24 | queue.Add(new Deployment("c"));
25 |
26 | return queue;
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-7/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_7
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | new HealthChecksObjects().PerformHealthChecks(5);
10 | new HealthChecksGeneric().PerformHealthChecks(5);
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-7/Section-1-7.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_1_7
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-8/DeploymentService1.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_8
4 | {
5 | public class DeploymentService1 : IDeploymentService
6 | {
7 | public void Validate()
8 | {
9 | Console.WriteLine("Deployment Service 1 Validated");
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-8/DeploymentService2.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_8
4 | {
5 | public class DeploymentService2 : IDeploymentService
6 | {
7 | public void Validate()
8 | {
9 | Console.WriteLine("Deployment Service 2 Validated");
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-8/IDeploymentService.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_8
2 | {
3 | public interface IDeploymentService
4 | {
5 | void Validate();
6 | }
7 | }
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-8/Program.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Section_1_8
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | new Program().Start();
10 | }
11 |
12 | void Start()
13 | {
14 | List services = Configure();
15 |
16 | foreach (var svc in services)
17 | svc.Validate();
18 | }
19 |
20 | List Configure()
21 | {
22 | return new List
23 | {
24 | new DeploymentService1(),
25 | new DeploymentService2(),
26 | new ThirdPartyDeploymentAdapter()
27 | };
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-8/Section-1-8.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_1_8
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-8/ThirdPartyDeploymentAdapter.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_8
2 | {
3 | public class ThirdPartyDeploymentAdapter : IDeploymentService
4 | {
5 | ThirdPartyDeploymentService service = new ThirdPartyDeploymentService();
6 |
7 | public void Validate()
8 | {
9 | service.PerformValidation();
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-8/ThirdPartyDeploymentService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace Section_1_8
6 | {
7 | public class ThirdPartyDeploymentService
8 | {
9 | public void PerformValidation()
10 | {
11 | Console.WriteLine("3rd Party Deployment Service 1 Validated");
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-9/DeploymentService.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_9
2 | {
3 | public class DeploymentService
4 | {
5 | public void Validate()
6 | {
7 | throw new DeploymentValidationException(
8 | "Smoke test failed - check with qa@example.com.",
9 | ValidationFailureReason.SmokeTestFailed);
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-9/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_1_9
4 | {
5 | class Program
6 | {
7 | static void Main()
8 | {
9 | try
10 | {
11 | new DeploymentService().Validate();
12 | }
13 | catch (DeploymentValidationException ex)
14 | {
15 | Console.WriteLine(
16 | $"Message: {ex.Message}\n" +
17 | $"Reason: {ex.Reason}\n" +
18 | $"Full Description: \n {ex}");
19 | }
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-9/Section-1-9.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_1_9
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter01/Section-1-9/ValidationFailureReason.cs:
--------------------------------------------------------------------------------
1 | namespace Section_1_9
2 | {
3 | public enum ValidationFailureReason
4 | {
5 | Unknown,
6 | PreviousDeploymentFailed,
7 | SmokeTestFailed,
8 | MissingArtifacts
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-01/Section-02-01.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_02_01
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-02/Invoice.txt:
--------------------------------------------------------------------------------
1 | abc
2 | abd
3 |
4 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-02/Section-02-02.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_02_02
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-03/Section-02-03.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_02_03
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-04/BankInvoice.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_02_04
4 | {
5 | public class BankInvoice : IInvoice
6 | {
7 | public void CalculateBalance()
8 | {
9 | Console.WriteLine("Calculating balance for BankInvoice.");
10 | }
11 |
12 | public bool IsApproved()
13 | {
14 | Console.WriteLine("Checking approval for BankInvoice.");
15 | return true;
16 | }
17 |
18 | public void PopulateLineItems()
19 | {
20 | Console.WriteLine("Populating items for BankInvoice.");
21 | }
22 |
23 | public void SetDueDate()
24 | {
25 | Console.WriteLine("Setting due date for BankInvoice.");
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-04/EnterpriseInvoice.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_02_04
4 | {
5 | public class EnterpriseInvoice : IInvoice
6 | {
7 | public void CalculateBalance()
8 | {
9 | Console.WriteLine("Calculating balance for EnterpriseInvoice.");
10 | }
11 |
12 | public bool IsApproved()
13 | {
14 | Console.WriteLine("Checking approval for EnterpriseInvoice.");
15 | return true;
16 | }
17 |
18 | public void PopulateLineItems()
19 | {
20 | Console.WriteLine("Populating items for EnterpriseInvoice.");
21 | }
22 |
23 | public void SetDueDate()
24 | {
25 | Console.WriteLine("Setting due date for EnterpriseInvoice.");
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-04/GovernmentInvoice.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_02_04
4 | {
5 | public class GovernmentInvoice : IInvoice
6 | {
7 | public void CalculateBalance()
8 | {
9 | Console.WriteLine("Calculating balance for GovernmentInvoice.");
10 | }
11 |
12 | public bool IsApproved()
13 | {
14 | Console.WriteLine("Checking approval for GovernmentInvoice.");
15 | return true;
16 | }
17 |
18 | public void PopulateLineItems()
19 | {
20 | Console.WriteLine("Populating items for GovernmentInvoice.");
21 | }
22 |
23 | public void SetDueDate()
24 | {
25 | Console.WriteLine("Setting due date for GovernmentInvoice.");
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-04/IInvoice.cs:
--------------------------------------------------------------------------------
1 | namespace Section_02_04
2 | {
3 | public interface IInvoice
4 | {
5 | bool IsApproved();
6 |
7 | void PopulateLineItems();
8 |
9 | void CalculateBalance();
10 |
11 | void SetDueDate();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-04/Program.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Section_02_04
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | IEnumerable invoices = GetInvoices();
10 |
11 | foreach (var invoice in invoices)
12 | {
13 | if (invoice.IsApproved())
14 | {
15 | invoice.CalculateBalance();
16 | invoice.PopulateLineItems();
17 | invoice.SetDueDate();
18 | }
19 | }
20 | }
21 |
22 | static IEnumerable GetInvoices()
23 | {
24 | return new List
25 | {
26 | new BankInvoice(),
27 | new EnterpriseInvoice(),
28 | new GovernmentInvoice()
29 | };
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-04/Section-02-04.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_02_04
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-05/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Section_02_05
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | List allInvoices = GetAllInvoices();
11 |
12 | Console.WriteLine($"# of All Invoices: {allInvoices.Count}");
13 |
14 | var invoicesToProcess = new List();
15 |
16 | foreach (var invoice in allInvoices)
17 | {
18 | if (!invoicesToProcess.Contains(invoice))
19 | invoicesToProcess.Add(invoice);
20 | }
21 |
22 | Console.WriteLine($"# of Invoices to Process: {invoicesToProcess.Count}");
23 | }
24 |
25 | static List GetAllInvoices()
26 | {
27 | DateTime date = DateTime.Now;
28 |
29 | return new List
30 | {
31 | new Invoice { CustomerID = 1, Created = date },
32 | new Invoice { CustomerID = 2, Created = date },
33 | new Invoice { CustomerID = 1, Created = date },
34 | new Invoice { CustomerID = 3, Created = date }
35 | };
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-05/Section-02-05.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_02_05
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-06/Section-02-06.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_02_06
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-07/Section-02-07.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_02_07
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-08/IInvoiceRepository.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Section_02_08
4 | {
5 | public interface IInvoiceRepository
6 | {
7 | List GetInvoiceCategories();
8 | }
9 | }
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-08/InvoiceCategory.cs:
--------------------------------------------------------------------------------
1 | namespace Section_02_08
2 | {
3 | public class InvoiceCategory
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-08/InvoiceRepository.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Section_02_08
4 | {
5 | public class InvoiceRepository : IInvoiceRepository
6 | {
7 | static List invoiceCategories;
8 |
9 | public List GetInvoiceCategories()
10 | {
11 | if (invoiceCategories == null)
12 | invoiceCategories = GetInvoiceCategoriesFromDB();
13 |
14 | return invoiceCategories;
15 | }
16 |
17 | List GetInvoiceCategoriesFromDB()
18 | {
19 | return new List
20 | {
21 | new InvoiceCategory { ID = 1, Name = "Government" },
22 | new InvoiceCategory { ID = 2, Name = "Financial" },
23 | new InvoiceCategory { ID = 3, Name = "Enterprise" },
24 | };
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-08/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Section_02_08
5 | {
6 | class Program
7 | {
8 | readonly IInvoiceRepository invoiceRep;
9 |
10 | public Program(IInvoiceRepository invoiceRep)
11 | {
12 | this.invoiceRep = invoiceRep;
13 | }
14 |
15 | void Run()
16 | {
17 | List categories =
18 | invoiceRep.GetInvoiceCategories();
19 |
20 | foreach (var category in categories)
21 | Console.WriteLine(
22 | $"ID: {category.ID}, Name: {category.Name}");
23 | }
24 |
25 | static void Main()
26 | {
27 | new Program(new InvoiceRepository()).Run();
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-08/Section-02-08.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_02_08
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-09/IInvoiceRepository.cs:
--------------------------------------------------------------------------------
1 | namespace Section_02_09
2 | {
3 | public interface IInvoiceRepository
4 | {
5 | void AddInvoiceCategory(string category);
6 | }
7 | }
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-09/InvoiceCategory.cs:
--------------------------------------------------------------------------------
1 | namespace Section_02_09
2 | {
3 | public class InvoiceCategory
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-09/InvoiceRepository.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_02_09
4 | {
5 | public class InvoiceRepository : IInvoiceRepository
6 | {
7 | public InvoiceRepository()
8 | {
9 | Console.WriteLine("InvoiceRepository Instantiated.");
10 | }
11 |
12 | public void AddInvoiceCategory(string category)
13 | {
14 | Console.WriteLine($"for category: {category}");
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-09/Section-02-09.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_02_09
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter02/Section-02-10/Section-02-10.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_02_10
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-01/OrdersLibrary/CustomerType.cs:
--------------------------------------------------------------------------------
1 | namespace OrdersLibrary
2 | {
3 | public enum CustomerType
4 | {
5 | Bronze,
6 | Silver,
7 | Gold
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-01/OrdersLibrary/Order.cs:
--------------------------------------------------------------------------------
1 | namespace OrdersLibrary
2 | {
3 | public class Order
4 | {
5 | public decimal CalculateDiscount(
6 | CustomerType custType, decimal amount)
7 | {
8 | decimal discount;
9 |
10 | switch (custType)
11 | {
12 | case CustomerType.Silver:
13 | discount = amount * 1.05m;
14 | break;
15 | case CustomerType.Gold:
16 | discount = amount * 1.10m;
17 | break;
18 | case CustomerType.Bronze:
19 | default:
20 | discount = amount;
21 | break;
22 | }
23 |
24 | return discount;
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-01/OrdersLibrary/OrdersLibrary.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-02/CompanyOrder.cs:
--------------------------------------------------------------------------------
1 | namespace Section_03_02
2 | {
3 | public class CompanyOrder : IOrder
4 | {
5 | decimal total = 25.00m;
6 |
7 | public string PrintOrder()
8 | {
9 | return "Company Order Details";
10 | }
11 |
12 | public decimal GetRewards()
13 | {
14 | return total * 0.01m;
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-02/CustomerOrder.cs:
--------------------------------------------------------------------------------
1 | namespace Section_03_02
2 | {
3 | class CustomerOrder : IOrder
4 | {
5 | public string PrintOrder()
6 | {
7 | return "Customer Order Details";
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-02/IOrder.cs:
--------------------------------------------------------------------------------
1 | namespace Section_03_02
2 | {
3 | public interface IOrder
4 | {
5 | string PrintOrder();
6 |
7 | decimal GetRewards() => 0.00m;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-02/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Section_03_02
5 | {
6 | class Program
7 | {
8 | static void Main()
9 | {
10 | var orders = new List
11 | {
12 | new CustomerOrder(),
13 | new CompanyOrder()
14 | };
15 |
16 | foreach (var order in orders)
17 | {
18 | Console.WriteLine(order.PrintOrder());
19 | Console.WriteLine($"Rewards: {order.GetRewards()}");
20 | }
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-02/Section-03-02.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_03_02
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-03/Section-03-03.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_03_03
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-04/OrderLibraryNonNull.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Section_03_04
5 | {
6 | public class OrderLibraryNonNull
7 | {
8 | // nullable property
9 | public string DealOfTheDay { get; set; }
10 |
11 | // method with null parameter
12 | public void AddItem(string item)
13 | {
14 | Console.Write(item.ToString());
15 | }
16 |
17 | // method with null return value
18 | public List GetItems()
19 | {
20 | return null;
21 | }
22 |
23 | // method with null type parameter
24 | public void AddItems(List items)
25 | {
26 | foreach (var item in items)
27 | Console.WriteLine(item.ToString());
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-04/OrderLibraryWithNull.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Section_03_04
5 | {
6 | public class OrderLibraryWithNull
7 | {
8 | // nullable property
9 | public string? DealOfTheDay { get; set; }
10 |
11 | // method with null parameter
12 | public void AddItem(string? item)
13 | {
14 | _ = item ?? throw new ArgumentNullException(
15 | nameof(item), $"{nameof(item)} must not be null");
16 |
17 | Console.Write(item.ToString());
18 | }
19 |
20 | // method with null return value
21 | public List? GetItems()
22 | {
23 | return null;
24 | }
25 |
26 | // method with null type parameter
27 | public void AddItems(List items)
28 | {
29 | foreach (var item in items)
30 | Console.WriteLine(item?.ToString() ?? "None");
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-04/Section-03-04.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_03_04
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-05/Delivery.cs:
--------------------------------------------------------------------------------
1 | namespace Section_03_05
2 | {
3 | public class Delivery
4 | {
5 | public const string NextDay = "Next Day";
6 | public const string Standard = "Standard";
7 | public const string LowFare = "Low Fare";
8 |
9 | public const int StandardDays = 7;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-05/Order.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Section_03_05
4 | {
5 | public class Order
6 | {
7 | public string DeliveryInstructions { get; set; }
8 |
9 | public List Items { get; set; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-05/Section-03-05.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_03_05
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-06/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_03_06
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | var order = new Order
10 | {
11 | ID = 7,
12 | CustomerName = "Acme",
13 | Created = DateTime.Now,
14 | Amount = 2_718_281.83m
15 | };
16 |
17 | Console.WriteLine(order);
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-06/Section-03-06.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_03_06
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-07/OrderOrchestrator.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_03_07
4 | {
5 | public class OrderOrchestrator
6 | {
7 | public static void HandleOrdersWrong()
8 | {
9 | try
10 | {
11 | new Orders().Process();
12 | }
13 | catch (IndexOutOfRangeException ex)
14 | {
15 | throw new InvalidOperationException(ex.Message);
16 | }
17 | }
18 |
19 | public static void HandleOrdersBetter1()
20 | {
21 | try
22 | {
23 | new Orders().Process();
24 | }
25 | catch (IndexOutOfRangeException ex)
26 | {
27 | throw new InvalidOperationException("Error Processing Orders", ex);
28 | }
29 | }
30 |
31 | public static void HandleOrdersBetter2()
32 | {
33 | try
34 | {
35 | new Orders().Process();
36 | }
37 | catch (IndexOutOfRangeException)
38 | {
39 | throw;
40 | }
41 | }
42 |
43 | public static void DontHandleOrders()
44 | {
45 | new Orders().Process();
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-07/Orders.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_03_07
4 | {
5 | public class Orders
6 | {
7 | public void Process()
8 | {
9 | throw new IndexOutOfRangeException(
10 | "Expected 10 orders, but found only 9.");
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-07/Section-03-07.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_03_07
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-08/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_03_08
4 | {
5 | class Program
6 | {
7 | static void Main()
8 | {
9 | // Don't do this:
10 | //Console.WriteLine("Processing Orders Started");
11 |
12 | //ProcessOrders();
13 |
14 | //Console.WriteLine("Processing Orders Complete");
15 |
16 | try
17 | {
18 | Console.WriteLine("Processing Orders Started");
19 |
20 | ProcessOrders();
21 | }
22 | catch (ArgumentException ae)
23 | {
24 | Console.WriteLine('\n' + ae.ToString() + '\n');
25 | }
26 | finally
27 | {
28 | Console.WriteLine("Processing Orders Complete");
29 | }
30 | }
31 |
32 | static void ProcessOrders()
33 | {
34 | throw new ArgumentException();
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-08/Section-03-08.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_03_08
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-09/Section-03-09.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_03_09
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter03/Section-03-10/Section-03-10.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Section_03_10
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-01/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace Section_04_01
5 | {
6 | class Program
7 | {
8 | static void Main()
9 | {
10 | var context = new InMemoryContext();
11 |
12 | var salesPersonLookup =
13 | (from person in context.SalesPeople
14 | select (person.ID, person.Name))
15 | .ToList();
16 |
17 | Console.WriteLine("Sales People\n");
18 |
19 | salesPersonLookup.ForEach(person =>
20 | Console.WriteLine($"{person.ID}. {person.Name}"));
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-01/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_01
2 | {
3 | public class SalesPerson
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Address { get; set; }
10 |
11 | public string City { get; set; }
12 |
13 | public string PostalCode { get; set; }
14 |
15 | public string Region { get; set; }
16 |
17 | public string ProductType { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-01/Section-04-01.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_01
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-02/Product.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_02
2 | {
3 | public class Product
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Type { get; set; }
10 |
11 | public decimal Price { get; set; }
12 |
13 | public string Region { get; set; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-02/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace Section_04_02
5 | {
6 | class Program
7 | {
8 | static void Main()
9 | {
10 | var context = new InMemoryContext();
11 |
12 | var salesProducts =
13 | (from person in context.SalesPeople
14 | join product in context.Products on
15 | (person.Region, person.ProductType)
16 | equals
17 | (product.Region, product.Type)
18 | select new
19 | {
20 | Person = person.Name,
21 | Product = product.Name,
22 | product.Region,
23 | product.Type
24 | })
25 | .ToList();
26 |
27 | Console.WriteLine("Sales People\n");
28 |
29 | salesProducts.ForEach(salesProd =>
30 | Console.WriteLine(
31 | $"Person: {salesProd.Person}\n" +
32 | $"Product: {salesProd.Product}\n" +
33 | $"Region: {salesProd.Region}\n" +
34 | $"Type: {salesProd.Type}\n"));
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-02/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_02
2 | {
3 | public class SalesPerson
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Address { get; set; }
10 |
11 | public string City { get; set; }
12 |
13 | public string PostalCode { get; set; }
14 |
15 | public string Region { get; set; }
16 |
17 | public string ProductType { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-02/Section-04-02.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_02
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-03/Product.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_03
2 | {
3 | public class Product
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Type { get; set; }
10 |
11 | public decimal Price { get; set; }
12 |
13 | public string Region { get; set; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-03/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_03
2 | {
3 | public class SalesPerson
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Address { get; set; }
10 |
11 | public string City { get; set; }
12 |
13 | public string PostalCode { get; set; }
14 |
15 | public string Region { get; set; }
16 |
17 | public string ProductType { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-03/Section-04-03.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_03
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-04/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace Section_04_04
5 | {
6 | class Program
7 | {
8 | static void Main()
9 | {
10 | var context = new InMemoryContext();
11 |
12 | var salesPeopleByRegion =
13 | (from person in context.SalesPeople
14 | group person by person.Region
15 | into personGroup
16 | select personGroup)
17 | .ToList();
18 |
19 | Console.WriteLine("Sales People by Region");
20 |
21 | foreach (var region in salesPeopleByRegion)
22 | {
23 | Console.WriteLine($"\nRegion: {region.Key}");
24 |
25 | foreach (var person in region)
26 | Console.WriteLine($" {person.Name}");
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-04/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_04
2 | {
3 | public class SalesPerson
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Address { get; set; }
10 |
11 | public string City { get; set; }
12 |
13 | public string PostalCode { get; set; }
14 |
15 | public string Region { get; set; }
16 |
17 | public string ProductType { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-04/Section-04-04.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_04
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-05/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_05
2 | {
3 | public class SalesPerson
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Address { get; set; }
10 |
11 | public string City { get; set; }
12 |
13 | public string PostalCode { get; set; }
14 |
15 | public string Region { get; set; }
16 |
17 | public string ProductType { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-05/Section-04-05.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_05
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-06/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 |
5 | namespace Section_04_06
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | var salesPeopleWithoutComparer =
12 | (from person in new InMemoryContext().SalesPeople
13 | select person)
14 | .Distinct()
15 | .ToList();
16 |
17 | PrintResults(salesPeopleWithoutComparer, "Without Comparer");
18 |
19 | var salesPeopleWithComparer =
20 | (from person in new InMemoryContext().SalesPeople
21 | select person)
22 | .Distinct(new SalesPersonComparer())
23 | .ToList();
24 |
25 | PrintResults(salesPeopleWithComparer, "With Comparer");
26 | }
27 |
28 | static void PrintResults(List salesPeople, string title)
29 | {
30 | Console.WriteLine($"\n{title}\n");
31 |
32 | salesPeople.ForEach(person =>
33 | Console.WriteLine($"{person.ID}. {person.Name}"));
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-06/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Section_04_06
4 | {
5 | public class SalesPersonComparer : IEqualityComparer
6 | {
7 | public bool Equals(SalesPerson x, SalesPerson y)
8 | {
9 | return x.ID == y.ID;
10 | }
11 |
12 | public int GetHashCode(SalesPerson obj)
13 | {
14 | return obj.GetHashCode();
15 | }
16 | }
17 |
18 | public class SalesPerson
19 | {
20 | public int ID { get; set; }
21 |
22 | public string Name { get; set; }
23 |
24 | public string Address { get; set; }
25 |
26 | public string City { get; set; }
27 |
28 | public string PostalCode { get; set; }
29 |
30 | public string Region { get; set; }
31 |
32 | public string ProductType { get; set; }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-06/Section-04-06.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_06
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-07/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace Section_04_07
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | decimal TotalSales = 0;
11 |
12 | var salesPeopleWithAddresses =
13 | (from person in new InMemoryContext().SalesPeople
14 | let FullAddress =
15 | $"{person.Address}\n" +
16 | $"{person.City}, {person.PostalCode}"
17 | let salesOkay =
18 | decimal.TryParse(person.TotalSales, out TotalSales)
19 | select new
20 | {
21 | person.ID,
22 | person.Name,
23 | FullAddress,
24 | TotalSales
25 | })
26 | .ToList();
27 |
28 | Console.WriteLine($"\nSales People and Addresses\n");
29 |
30 | salesPeopleWithAddresses.ForEach(person =>
31 | Console.WriteLine(
32 | $"{person.ID}. {person.Name}: {person.TotalSales:C}\n" +
33 | $"{person.FullAddress}\n"));
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-07/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_07
2 | {
3 | public class SalesPerson
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Address { get; set; }
10 |
11 | public string City { get; set; }
12 |
13 | public string PostalCode { get; set; }
14 |
15 | public string Region { get; set; }
16 |
17 | public string ProductType { get; set; }
18 |
19 | public string TotalSales { get; set; }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-07/Section-04-07.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_07
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-08/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Section_04_08
4 | {
5 | public class SalesPerson : IEqualityComparer
6 | {
7 | public int ID { get; set; }
8 |
9 | public string Name { get; set; }
10 |
11 | public string Address { get; set; }
12 |
13 | public string City { get; set; }
14 |
15 | public string PostalCode { get; set; }
16 |
17 | public string Region { get; set; }
18 |
19 | public string ProductType { get; set; }
20 |
21 | public bool Equals(SalesPerson x, SalesPerson y)
22 | {
23 | return x.ID == y.ID;
24 | }
25 |
26 | public int GetHashCode(SalesPerson obj)
27 | {
28 | return ID.GetHashCode();
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-08/Section-04-08.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_08
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-09/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_09
2 | {
3 | public class SalesPerson
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Address { get; set; }
10 |
11 | public string City { get; set; }
12 |
13 | public string PostalCode { get; set; }
14 |
15 | public string Region { get; set; }
16 |
17 | public string ProductType { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-09/Section-04-09.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_09
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-10/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading;
5 |
6 | namespace Section_04_10
7 | {
8 | class Program
9 | {
10 | static void Main()
11 | {
12 | List salesPeople = new InMemoryContext().SalesPeople;
13 | var result =
14 | (from person in salesPeople.AsParallel()
15 | select ProcessPerson(person))
16 | .ToList();
17 | }
18 |
19 | static SalesPerson ProcessPerson(SalesPerson person)
20 | {
21 | Console.WriteLine(
22 | $"Starting sales person " +
23 | $"#{person.ID}. {person.Name}");
24 |
25 | // complex in-memory processing
26 | Thread.Sleep(500);
27 |
28 | Console.WriteLine(
29 | $"Completed sales person " +
30 | $"#{person.ID}. {person.Name}");
31 |
32 | return person;
33 | }
34 | }
35 | }
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-10/SalesPerson.cs:
--------------------------------------------------------------------------------
1 | namespace Section_04_10
2 | {
3 | public class SalesPerson
4 | {
5 | public int ID { get; set; }
6 |
7 | public string Name { get; set; }
8 |
9 | public string Address { get; set; }
10 |
11 | public string City { get; set; }
12 |
13 | public string PostalCode { get; set; }
14 |
15 | public string Region { get; set; }
16 |
17 | public string ProductType { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter04/Section-04-10/Section-04-10.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | Section_04_10
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter05/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JoeMayo/csharp-nine-cookbook/a22b77eb3e472a847035297dac9d4594a96f458f/CSharp9Cookbook/Chapter05/.DS_Store
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter05/Section-05-01/ColumnAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Section_05_01
4 | {
5 | [AttributeUsage(
6 | AttributeTargets.Property | AttributeTargets.Method,
7 | AllowMultiple = false)]
8 | public class ColumnAttribute : Attribute
9 | {
10 | public ColumnAttribute(string name)
11 | {
12 | Name = name;
13 | }
14 |
15 | public string Name { get; set; }
16 |
17 | public string Format { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter05/Section-05-01/InventoryItem.cs:
--------------------------------------------------------------------------------
1 | namespace Section_05_01
2 | {
3 | public class InventoryItem
4 | {
5 | [Column("Part #")]
6 | public string PartNumber { get; set; }
7 |
8 | [Column("Name")]
9 | public string Description { get; set; }
10 |
11 | [Column("Amount")]
12 | public int Count { get; set; }
13 |
14 | [Column("Price")]
15 | public decimal ItemPrice { get; set; }
16 |
17 | [Column("Total")]
18 | public decimal CalculateTotal()
19 | {
20 | return ItemPrice * Count;
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/CSharp9Cookbook/Chapter05/Section-05-01/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Section_05_01
5 | {
6 | class Program
7 | {
8 | static void Main()
9 | {
10 | var inventory = new List