Skip to content

Commit 5a065bb

Browse files
committed
review update and Ex8 code fixes
1 parent a4346ec commit 5a065bb

5 files changed

Lines changed: 35 additions & 40 deletions

File tree

DownloadableCodeProjects/standalone-lab-projects/refactor-large-functions/ECommerceOrderProcessing/src/ECommerce.ApplicationCore/Services/OrderProcessor.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace ECommerce.ApplicationCore.Services;
55

66
/// <summary>
7-
/// Main order processing service containing the large method that needs refactoring
7+
/// Main order processing service
88
/// </summary>
99
public class OrderProcessor
1010
{
@@ -32,9 +32,6 @@ public OrderProcessor(
3232
}
3333

3434
/// <summary>
35-
/// LARGE METHOD INTENTIONALLY KEPT FOR REFACTORING DEMONSTRATION
36-
/// This method contains multiple responsibilities and should be refactored into smaller methods
37-
///
3835
/// Responsibilities include:
3936
/// 1. Input Validation & Security Checks
4037
/// 2. Inventory Management
@@ -52,7 +49,7 @@ public OrderResult ProcessOrder(Order order)
5249

5350
try
5451
{
55-
// Step 1: Comprehensive Input Validation and Security Checks
52+
// Comprehensive Input Validation and Security Checks
5653
if (order == null)
5754
{
5855
_auditLogger.LogSecurityEvent("NULL_ORDER_ATTEMPT", "Attempted to process null order");
@@ -104,7 +101,7 @@ public OrderResult ProcessOrder(Order order)
104101
Console.WriteLine($"Processing Order {order.Id} for {_securityValidator.MaskEmail(order.CustomerEmail)}...");
105102
Console.WriteLine($"Order contains {order.Items.Count} items, Total: ${order.TotalAmount:F2}");
106103

107-
// Step 2: Inventory Management - Check Stock Availability and Reserve Items
104+
// Inventory Management - Check Stock Availability and Reserve Items
108105
Console.WriteLine("Checking inventory availability...");
109106
foreach (OrderItem item in order.Items)
110107
{
@@ -135,7 +132,7 @@ public OrderResult ProcessOrder(Order order)
135132
Console.WriteLine("Inventory reserved successfully.");
136133
_auditLogger.LogInventoryReserved(order.Id, order.Items.Count);
137134

138-
// Step 3: Payment Processing with Enhanced Security
135+
// Payment Processing with Enhanced Security
139136
Console.WriteLine("Processing payment...");
140137
try
141138
{
@@ -162,7 +159,7 @@ public OrderResult ProcessOrder(Order order)
162159
return OrderResult.Failure("Payment processing failed: " + ex.Message);
163160
}
164161

165-
// Step 4: Shipping and Logistics Management
162+
// Shipping and Logistics Management
166163
Console.WriteLine("Scheduling shipping...");
167164
try
168165
{
@@ -186,7 +183,7 @@ public OrderResult ProcessOrder(Order order)
186183
return OrderResult.Failure("Shipping scheduling failed: " + ex.Message);
187184
}
188185

189-
// Step 5: Customer Communication and Notifications
186+
// Customer Communication and Notifications
190187
Console.WriteLine("Sending notifications...");
191188
try
192189
{
@@ -209,7 +206,7 @@ public OrderResult ProcessOrder(Order order)
209206
Console.WriteLine($"Warning: failed to send notification: {ex.Message}");
210207
}
211208

212-
// Step 6: Order Finalization and Data Recording
209+
// Order Finalization and Data Recording
213210
Console.WriteLine("Finalizing order...");
214211
order.Status = OrderStatus.Completed;
215212
order.CompletionDate = DateTime.UtcNow;

DownloadableCodeProjects/standalone-lab-projects/refactor-large-functions/ECommerceOrderProcessing/src/ECommerce.Console/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void Main(string[] args)
4646
{
4747
CardNumber = "4111111111111111",
4848
CardCVV = "123",
49-
CardHolderName = "John Doe",
49+
CardHolderName = "Ane Pedersen",
5050
ExpiryMonth = "12",
5151
ExpiryYear = "2025",
5252
BillingAddress = "123 Main St, City, State 12345"
@@ -58,23 +58,23 @@ public static void Main(string[] args)
5858
System.Console.WriteLine("\n--- Test Case 2: Invalid Email Address ---");
5959
var invalidEmailOrder = CreateSampleOrder("ORD-002", "invalid-email", "123 Main St",
6060
new List<OrderItem> { new() { ProductId = "BOOK-001", Quantity = 1, Price = 15.99m } },
61-
new PaymentInfo { CardNumber = "4111111111111111", CardCVV = "123", CardHolderName = "Jane Smith", ExpiryMonth = "06", ExpiryYear = "2026", BillingAddress = "123 Main St" });
61+
new PaymentInfo { CardNumber = "4111111111111111", CardCVV = "123", CardHolderName = "Jennet Nazarowa", ExpiryMonth = "06", ExpiryYear = "2026", BillingAddress = "123 Main St" });
6262

6363
var result2 = processor.ProcessOrder(invalidEmailOrder);
6464
testResults.Add($"Test 2: {(result2.IsSuccess ? "FAILED" : "PASSED")} - Should reject invalid email");
6565

6666
System.Console.WriteLine("\n--- Test Case 3: Declined Payment ---");
6767
var declinedPaymentOrder = CreateSampleOrder("ORD-003", "customer@test.com", "456 Oak Ave",
6868
new List<OrderItem> { new() { ProductId = "PHONE-001", Quantity = 1, Price = 699.99m } },
69-
new PaymentInfo { CardNumber = "0000000000000000", CardCVV = "999", CardHolderName = "Test User", ExpiryMonth = "01", ExpiryYear = "2024", BillingAddress = "456 Oak Ave" });
69+
new PaymentInfo { CardNumber = "0000000000000000", CardCVV = "999", CardHolderName = "Nikki Vestergaard", ExpiryMonth = "01", ExpiryYear = "2024", BillingAddress = "456 Oak Ave" });
7070

7171
var result3 = processor.ProcessOrder(declinedPaymentOrder);
7272
testResults.Add($"Test 3: {(result3.IsSuccess ? "FAILED" : "PASSED")} - Should reject declined payment");
7373

7474
System.Console.WriteLine("\n--- Test Case 4: Suspicious Order (Security Check) ---");
75-
var suspiciousOrder = CreateSampleOrder("ORD-004", "suspicious@temp.com", "Unknown Location",
76-
new List<OrderItem> { new() { ProductId = "EXPENSIVE-001", Quantity = 10, Price = 5000.00m } },
77-
new PaymentInfo { CardNumber = "4111111111111111", CardCVV = "000", CardHolderName = "A", ExpiryMonth = "12", ExpiryYear = "2025", BillingAddress = "Unknown Location" });
75+
var suspiciousOrder = CreateSampleOrder("ORD-004", "suspicious.user@valid.com", "123 Suspicious Ave, City, State 99999",
76+
new List<OrderItem> { new() { ProductId = "EXPENSIVE-001", Quantity = 2, Price = 25000.00m } },
77+
new PaymentInfo { CardNumber = "4111111111111111", CardCVV = "123", CardHolderName = "AB", ExpiryMonth = "12", ExpiryYear = "2026", BillingAddress = "123 Suspicious Ave, City, State 99999" });
7878

7979
var result4 = processor.ProcessOrder(suspiciousOrder);
8080
testResults.Add($"Test 4: {(result4.IsSuccess ? "FAILED" : "PASSED")} - Should flag suspicious order");

Instructions/Labs/LAB_AK_07_consolidate_duplicate_code.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Use the following steps to download the sample project and open it in Visual Stu
8686
8787
1. Navigate to the Windows Desktop folder, select **GHCopilotEx7LabApps** and then select **Select Folder**.
8888
89-
1. In the Visual Studio Code SOLUTION EXPLORER view, verify the following solution structure:
89+
1. In the Visual Studio Code SOLUTION EXPLORER view, verify the following project structure:
9090
9191
- GHCopilotEx7LabApps\
9292
- ECommerceOrderAndReturn\

Instructions/Labs/LAB_AK_08_refactor_large_functions.md

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,21 @@ Use the following steps to download the sample project and open it in Visual Stu
8686
8787
1. Navigate to the Windows Desktop folder, select **GHCopilotEx8LabApps** and then select **Select Folder**.
8888
89-
1. In the Visual Studio Code SOLUTION EXPLORER view, verify the following solution structure:
89+
1. In the Visual Studio Code SOLUTION EXPLORER view, verify the following project structure:
9090
9191
- GHCopilotEx8LabApps\
9292
- ECommerceOrderProcessing\
9393
- src\
9494
- ECommerce.ApplicationCore\
95-
- ECommerce.Infrastructure\
95+
- Entities\
96+
- Interfaces\
97+
- Services\
9698
- ECommerce.Console\
97-
- ECommerceOrderProcessing.sln
99+
- order_audit_log.txt
100+
- Program.cs
101+
- ECommerce.Infrastructure\
102+
- Services\
103+
- ServerLogAnalysisUtility\
98104
99105
## Exercise scenario
100106
@@ -125,18 +131,12 @@ Use the following steps to complete this task:
125131

126132
The codebase follows a layered architecture pattern with three main projects:
127133

128-
- **ECommerce.ApplicationCore**: Contains domain entities, business logic interfaces, and the main `OrderProcessor` service
129-
- **ECommerce.Infrastructure**: Contains service implementations for external integrations (payment, shipping, inventory, etc.)
134+
- **ECommerce.ApplicationCore**: Contains domain entities, business logic interfaces, and the main `OrderProcessor` service.
130135
- **ECommerce.Console**: Contains the console application entry point and dependency injection setup
136+
- **ECommerce.Infrastructure**: Contains service implementations for external integrations (payment, shipping, inventory, etc.).
131137

132138
This structure represents a real-world .NET application using Clean Architecture principles, where business logic is separated from infrastructure concerns.
133139

134-
1. Verify that the **ECommerceOrderProcessing** solution builds successfully.
135-
136-
For example, in the SOLUTION EXPLORER view, right-click **ECommerceOrderProcessing.sln**, and then select **Build**.
137-
138-
You should see a successful build with no errors. This confirms that all project dependencies are properly configured.
139-
140140
1. Open the GitHub Copilot Chat view.
141141

142142
If the Chat view isn't already open, you can open it by selecting the **Chat** icon at the top of the Visual Studio Code window.
@@ -149,19 +149,13 @@ Use the following steps to complete this task:
149149
150150
1. In Visual Studio Code, navigate to **src/ECommerce.ApplicationCore/Services/OrderProcessor.cs**.
151151
152-
This file contains the `OrderProcessor` class with the large `ProcessOrder` method that you'll be refactoring. The method is over 200 lines long and handles multiple responsibilities including validation, security checks, inventory management, payment processing, shipping, notifications, and order finalization.
152+
1. Open the OrderProcessor.cs file in the code editor.
153153
154-
1. Locate the **ProcessOrder** method in the **OrderProcessor** class.
154+
The OrderProcessor.cs file provides the main order processing service for the app.
155155
156-
The `ProcessOrder` method represents the core business logic for processing customer orders. Notice that it handles multiple distinct operations:
156+
1. Take a minute to review the **OrderProcessor** class.
157157
158-
- **Input validation and security checks**: Email validation, address validation, payment info validation, risk assessment
159-
- **Inventory management**: Stock checking and reservation with rollback on failures
160-
- **Payment processing**: Secure payment validation and processing with fraud detection
161-
- **Shipping coordination**: Shipment scheduling and tracking number generation
162-
- **Customer notifications**: Email confirmations and high-value order alerts
163-
- **Order finalization**: Status updates, completion tracking, and audit logging
164-
- **Error handling**: Comprehensive exception management with cleanup procedures
158+
Notice the ProcessOrder method. This method represents the core business logic for processing customer orders. Notice that it handles multiple distinct operations.
165159
166160
The method is intentionally large and complex to demonstrate real-world scenarios where business logic has accumulated over time, making it difficult to read, test, and maintain.
167161
@@ -175,7 +169,11 @@ Use the following steps to complete this task:
175169

176170
1. Run the application to understand its current behavior.
177171

178-
Navigate to the **src/ECommerce.Console** folder in the terminal and run:
172+
For example:
173+
174+
In the SOLUTION EXPLORER view, right-click the **ECommerce.Console** project, select **Debug**, and then select **Start New Instance**.
175+
176+
You can also navigate to the **src/ECommerce.Console** folder in the terminal and run:
179177

180178
```bash
181179
dotnet run

Instructions/Labs/LAB_AK_09_simplify_complex_conditionals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Use the following steps to download the sample projects and open them in Visual
8686
8787
1. Navigate to the Windows Desktop folder, select **GHCopilotEx9LabApps** and then select **Select Folder**.
8888
89-
1. In the Visual Studio Code SOLUTION EXPLORER view, verify the following solution structure:
89+
1. In the Visual Studio Code SOLUTION EXPLORER view, verify the following project structure:
9090
9191
- GHCopilotEx9LabApps\
9292
- ECommercePricingEngine\

0 commit comments

Comments
 (0)