Skip to content

Commit 9267cce

Browse files
committed
adding e-commerce code consolidation starter code
1 parent 0b1bde0 commit 9267cce

16 files changed

Lines changed: 1260 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Configuration.cs - Application configuration settings
2+
namespace EcommerceApp.Configuration
3+
{
4+
public static class AppConfig
5+
{
6+
// Shipping configuration
7+
public static decimal BaseShippingRate { get; } = 5.99m;
8+
public static decimal WeightBasedRatePerPound { get; } = 1.25m;
9+
public static decimal FreeShippingThreshold { get; } = 50.00m;
10+
public static decimal ReturnProcessingFee { get; } = 2.99m;
11+
12+
// Security configuration
13+
public static int MaxIdLength { get; } = 20;
14+
public static int MinIdLength { get; } = 6;
15+
16+
// Business rules
17+
public static int MaxReturnDays { get; } = 30;
18+
public static decimal MaxRefundAmount { get; } = 1000.00m;
19+
}
20+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Enhanced Duplicate Code Summary
2+
3+
## Overview
4+
5+
The E-Commerce Order and Return Processing System has been significantly enhanced to include **5 different types of duplicate code patterns** that are commonly found in real-world e-commerce applications.
6+
7+
## Duplicate Code Patterns Implemented
8+
9+
### 1. Core Business Logic Duplication (Original)
10+
**Location**: `OrderProcessor.cs` and `ReturnProcessor.cs`
11+
- `Validate(string id)` - Identical validation logic
12+
- `CalculateShipping()` - Similar shipping calculation patterns
13+
14+
### 2. Email Communication Duplication (Very Common)
15+
**Location**: `Services/EmailService.cs`
16+
- `BuildEmailTemplate()` - Template building for orders vs returns
17+
- `FormatEmailSubject()` - Subject line formatting
18+
- `SendEmail()` - Email sending mechanism
19+
- `LogEmailActivity()` - Email audit logging
20+
21+
**Why this is realistic**: Email notifications are frequently copy-pasted between different business processes in e-commerce systems.
22+
23+
### 3. Audit and Logging Duplication (Extremely Common)
24+
**Location**: `Services/AuditService.cs`
25+
- `CreateAuditEntry()` - Audit entry creation pattern
26+
- `ValidateAuditEntry()` - Entry validation logic
27+
- `StoreAuditEntry()` - Storage mechanism
28+
- `CheckComplianceRequirements()` - Compliance checking
29+
30+
**Why this is realistic**: Audit logging is one of the most frequently duplicated patterns in enterprise applications, often copy-pasted for different transaction types.
31+
32+
### 4. Inventory Management Duplication (Common)
33+
**Location**: `Services/InventoryService.cs`
34+
- `ValidateInventoryAvailability()` - Stock validation
35+
- `UpdateInventoryLevel()` - Inventory updates (+ for returns, - for orders)
36+
- `LogInventoryTransaction()` - Transaction logging
37+
38+
**Why this is realistic**: Inventory operations are often duplicated between order processing, returns, adjustments, and transfers.
39+
40+
### 5. Cross-Cutting Concerns Duplication (Hidden)
41+
**Throughout the application**:
42+
- Payment processing with audit logging (in both processors)
43+
- Status updates with logging
44+
- Error handling patterns
45+
46+
## Why These Examples Are Better
47+
48+
### More Realistic Patterns
49+
1. **Service Layer Duplication**: Most duplicate code exists in service layers, not just core business logic
50+
2. **Cross-System Integration**: Email, audit, and inventory patterns reflect real integration points
51+
3. **Multiple Abstraction Levels**: Shows duplication at different architectural layers
52+
53+
### Better Learning Experience
54+
1. **Various Refactoring Strategies**: Different patterns require different consolidation approaches
55+
2. **Real-World Complexity**: Mirrors actual enterprise application challenges
56+
3. **Multiple Solutions**: Students can choose between shared services, base classes, or utility methods
57+
58+
### Progressive Difficulty
59+
1. **Basic**: Start with simple validation/shipping logic
60+
2. **Intermediate**: Extract email and audit services
61+
3. **Advanced**: Consolidate cross-cutting concerns and create architectural improvements
62+
63+
## Lab Exercise Progression
64+
65+
### Phase 1: Core Duplications (Easy)
66+
- Extract validation logic
67+
- Consolidate shipping calculations
68+
69+
### Phase 2: Service Duplications (Medium)
70+
- Consolidate email template logic
71+
- Extract common audit entry management
72+
- Merge inventory operation patterns
73+
74+
### Phase 3: Architectural Improvements (Advanced)
75+
- Create shared interfaces
76+
- Implement dependency injection
77+
- Design common base classes
78+
79+
## Expected Refactoring Outcomes
80+
81+
Students should be able to:
82+
1. **Identify** 5+ distinct duplicate code patterns
83+
2. **Prioritize** which duplications to address first
84+
3. **Choose** appropriate refactoring strategies for each pattern
85+
4. **Implement** consolidated solutions using GitHub Copilot
86+
5. **Verify** that functionality remains unchanged
87+
88+
## Real-World Application
89+
90+
These patterns directly translate to:
91+
- **E-commerce platforms** (order/return/exchange processing)
92+
- **Financial systems** (transaction processing with audit trails)
93+
- **Healthcare systems** (patient record management with compliance)
94+
- **Supply chain systems** (inventory and logistics management)
95+
96+
The enhanced codebase now provides a much more comprehensive and realistic foundation for learning code consolidation techniques with GitHub Copilot.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Expected output for verification after refactoring
2+
// This file documents the expected behavior that should be maintained after consolidating duplicate code
3+
4+
/*
5+
EXPECTED OUTPUT WHEN RUNNING THE APPLICATION:
6+
7+
=== E-Commerce Order and Return Processing System ===
8+
Starting application tests...
9+
10+
TEST 1: Processing a valid order
11+
- Should show order validation steps
12+
- Should calculate shipping (free shipping for $75.50 order)
13+
- Should process payment successfully
14+
- Should reserve inventory for all items
15+
- Should complete without errors
16+
17+
TEST 2: Processing a valid return
18+
- Should show return validation steps
19+
- Should validate return eligibility
20+
- Should calculate return shipping (processing fee only for high-value return)
21+
- Should process refund successfully
22+
- Should update inventory
23+
- Should complete without errors
24+
25+
TEST 3: Processing an invalid order (security test)
26+
- Should fail ID format validation
27+
- Should show security check failure
28+
- Should handle error gracefully
29+
30+
TEST 4: Processing an invalid return (security test)
31+
- Should detect XSS attempt
32+
- Should fail length validation
33+
- Should handle error gracefully
34+
35+
TEST 5: Processing with empty ID
36+
- Should fail null/empty validation
37+
- Should handle error gracefully
38+
39+
VALIDATION POINTS:
40+
✓ All security validations work correctly
41+
✓ Shipping calculations are accurate
42+
✓ Error handling is robust
43+
✓ Business rules are enforced
44+
✓ Output is consistent and informative
45+
46+
AFTER REFACTORING:
47+
- The exact same output should be produced
48+
- All test scenarios should behave identically
49+
- No functionality should be lost
50+
- Performance should be maintained or improved
51+
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Order.cs - Represents an e-commerce order
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace EcommerceApp.Models
6+
{
7+
public class Order
8+
{
9+
public string OrderId { get; set; } = string.Empty;
10+
public string CustomerId { get; set; } = string.Empty;
11+
public DateTime OrderDate { get; set; }
12+
public List<OrderItem> Items { get; set; } = new List<OrderItem>();
13+
public decimal TotalAmount { get; set; }
14+
public string ShippingAddress { get; set; } = string.Empty;
15+
public OrderStatus Status { get; set; }
16+
}
17+
18+
public class OrderItem
19+
{
20+
public string ProductId { get; set; } = string.Empty;
21+
public string ProductName { get; set; } = string.Empty;
22+
public int Quantity { get; set; }
23+
public decimal UnitPrice { get; set; }
24+
public decimal Weight { get; set; } // in pounds, for shipping calculation
25+
}
26+
27+
public enum OrderStatus
28+
{
29+
Pending,
30+
Processing,
31+
Shipped,
32+
Delivered,
33+
Cancelled
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Return.cs - Represents a product return
2+
using System;
3+
4+
namespace EcommerceApp.Models
5+
{
6+
public class Return
7+
{
8+
public string ReturnId { get; set; } = string.Empty;
9+
public string OriginalOrderId { get; set; } = string.Empty;
10+
public string CustomerId { get; set; } = string.Empty;
11+
public DateTime ReturnDate { get; set; }
12+
public string ProductId { get; set; } = string.Empty;
13+
public string ProductName { get; set; } = string.Empty;
14+
public int Quantity { get; set; }
15+
public decimal RefundAmount { get; set; }
16+
public string Reason { get; set; } = string.Empty;
17+
public ReturnStatus Status { get; set; }
18+
public decimal Weight { get; set; } // in pounds, for return shipping calculation
19+
}
20+
21+
public enum ReturnStatus
22+
{
23+
Pending,
24+
Approved,
25+
Rejected,
26+
Processing,
27+
Completed
28+
}
29+
}

0 commit comments

Comments
 (0)