Repository navigation
Expand file tree
/
Copy pathSampleModelsPerson.Builder.cs
More file actions
70 lines (59 loc) · 1.97 KB
/
Copy pathSampleModelsPerson.Builder.cs
File metadata and controls
70 lines (59 loc) · 1.97 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
using System;
using System.Collections.Generic;
using System;
using System.ComponentModel.DataAnnotations;
using BuilderCommon;
namespace Sample.Models
{
partial class Person
{
private Person(){}
public static PersonBuilder Builder => new PersonBuilder();
public class PersonBuilder
{
private string _firstName;
public PersonBuilder FirstName(string FirstName)
{
_firstName = FirstName;
return this;
}
private string _lastName;
public PersonBuilder LastName(string LastName)
{
_lastName = LastName;
return this;
}
private DateTime? _birthDate;
public PersonBuilder BirthDate(DateTime? BirthDate)
{
_birthDate = BirthDate;
return this;
}
public Person Build()
{
Validate();
return new Person
{
FirstName = _firstName,
LastName = _lastName,
BirthDate = _birthDate,
};
}
public void Validate()
{
void AddError(Dictionary<string, string> items, string property, string message)
{
if (items.TryGetValue(property, out var error))
items[property] = $"{error}\n{message}";
else
items[property] = message;
}
Dictionary<string,string> errors = new Dictionary<string, string>();
if(_firstName == default) AddError(errors, "FirstName", "Value is required");
if(_lastName == default) AddError(errors, "LastName", "Value is required");
if(errors.Count > 0)
throw new BuilderCommon.BuilderException(errors);
}
}
}
}