-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
54 lines (45 loc) · 1.37 KB
/
Program.cs
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
namespace DesignPatterns.Builder;
class ConnectionBuilder {
private class Connection {
public string Database;
public string Server;
public string UserId;
public string Password;
public override string ToString()
=> $"Server={Server};Database={Database};UserId={UserId};Password={Password};";
}
readonly Connection _connection;
public ConnectionBuilder() {
_connection = new Connection();
}
public ConnectionBuilder SetDatabase(String database) {
_connection.Database = database;
return this;
}
public ConnectionBuilder SetServer(String server) {
_connection.Server = server;
return this;
}
public ConnectionBuilder SetUserId(String userId) {
_connection.UserId = userId;
return this;
}
public ConnectionBuilder SetPassword(String password) {
_connection.Password = password;
return this;
}
public String Build() => _connection.ToString();
public void Build(Action<string> action) => action.Invoke(_connection.ToString());
}
class App
{
public static void Main(string[] args)
{
new ConnectionBuilder()
.SetServer("127.0.0.1")
.SetDatabase("Northwind")
.SetUserId("Nancy")
.SetPassword("123456")
.Build(Console.WriteLine);
}
}