-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
146 lines (117 loc) · 4.4 KB
/
Copy pathProgram.cs
File metadata and controls
146 lines (117 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.ComponentModel.DataAnnotations;
using JD.Domain.ManifestGeneration;
using JD.Domain.Rules;
namespace JD.Domain.Samples.CodeFirst;
/// <summary>
/// Demonstrates code-first workflow with automatic manifest generation.
/// NO manual string writing required - all metadata extracted from entity classes!
/// </summary>
public static class Program
{
public static void Main()
{
Console.WriteLine("=== JD.Domain Code-First Sample (Automatic Manifest Generation) ===\n");
// Step 1: Access auto-generated manifest
Console.WriteLine("1. Using auto-generated domain manifest...");
var manifest = ECommerceManifest.GeneratedManifest;
Console.WriteLine($" Domain: {manifest.Name} v{manifest.Version}");
Console.WriteLine($" Entities: {manifest.Entities.Count}");
Console.WriteLine($" Source: {manifest.Sources[0].Type}");
Console.WriteLine($" NO MANUAL STRING WRITING REQUIRED!");
// Step 2: Define business rules
Console.WriteLine("\n2. Defining business rules...");
var customerRules = new RuleSetBuilder<Customer>("Default")
.Invariant("Customer.Name.Required", c => !string.IsNullOrWhiteSpace(c.Name))
.WithMessage("Customer name cannot be empty")
.Invariant("Customer.Email.Required", c => !string.IsNullOrWhiteSpace(c.Email))
.WithMessage("Customer email cannot be empty")
.BuildCompiled();
var orderRules = new RuleSetBuilder<Order>("Default")
.Invariant("Order.Items.Required", o => o.Items.Count > 0)
.WithMessage("An order must contain at least one item")
.BuildCompiled();
Console.WriteLine($" Customer rules: {customerRules.Rules.Count}");
Console.WriteLine($" Order rules: {orderRules.Rules.Count}");
// Step 3: Validate sample data using compiled rules
Console.WriteLine("\n3. Validating sample data...");
// Valid customer
var validCustomer = new Customer
{
Id = Guid.NewGuid(),
Name = "John Doe",
Email = "john@example.com"
};
var validResult = customerRules.Evaluate(validCustomer);
Console.WriteLine($" Valid customer: {(validResult.IsValid ? "PASSED" : "FAILED")}");
// Invalid customer (empty name triggers rule)
var invalidCustomer = new Customer
{
Id = Guid.NewGuid(),
Name = "",
Email = "invalid-email"
};
var invalidResult = customerRules.Evaluate(invalidCustomer);
Console.WriteLine($" Invalid customer: {(invalidResult.IsValid ? "PASSED" : "FAILED")}");
foreach (var error in invalidResult.Errors)
{
Console.WriteLine($" - {error.Message}");
}
Console.WriteLine("\n=== Sample Complete ===");
Console.WriteLine("Manifest was generated automatically from entity attributes!");
}
}
// Domain entities with automatic manifest generation
[DomainEntity]
public class Customer
{
[Key]
public Guid Id { get; set; }
[Required]
[MaxLength(200)]
public string Name { get; set; } = "";
[Required]
[MaxLength(500)]
public string Email { get; set; } = "";
[ExcludeFromManifest]
public List<Order> Orders { get; set; } = new();
}
[DomainEntity]
public class Order
{
[Key]
public Guid Id { get; set; }
[Required]
public Guid CustomerId { get; set; }
public DateTime OrderDate { get; set; }
[Required]
public decimal TotalAmount { get; set; }
[ExcludeFromManifest]
public Customer Customer { get; set; } = null!;
[ExcludeFromManifest]
public List<OrderItem> Items { get; set; } = new();
}
[DomainEntity]
public class Product
{
[Key]
public Guid Id { get; set; }
[Required]
[MaxLength(200)]
public string Name { get; set; } = "";
[MaxLength(1000)]
public string? Description { get; set; }
[Required]
public decimal Price { get; set; }
public int StockQuantity { get; set; }
}
// OrderItem is not included in manifest (no [DomainEntity] attribute)
public class OrderItem
{
public Guid Id { get; set; }
public Guid OrderId { get; set; }
public Order Order { get; set; } = null!;
public Guid ProductId { get; set; }
public Product Product { get; set; } = null!;
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
}