-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilderPattern
More file actions
324 lines (276 loc) · 9.01 KB
/
BuilderPattern
File metadata and controls
324 lines (276 loc) · 9.01 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using Microsoft.EntityFrameworkCore;
using System;
using System.Data;
using System.Data.SqlClient;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public interface IProductBuilder
{
IProductBuilder SetProduct(Product product);
IProductBuilder Create();
IProductBuilder Read(int id);
IProductBuilder Update();
IProductBuilder Delete(int id);
Product GetProduct();
}
public class AdoNetProductBuilder : IProductBuilder
{
private Product _product;
private readonly string _connectionString;
public AdoNetProductBuilder(string connectionString)
{
_connectionString = connectionString;
}
public IProductBuilder SetProduct(Product product)
{
_product = product;
return this;
}
public IProductBuilder Create()
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Products (Name, Price) VALUES (@Name, @Price); SELECT SCOPE_IDENTITY()", connection);
command.Parameters.AddWithValue("@Name", _product.Name);
command.Parameters.AddWithValue("@Price", _product.Price);
connection.Open();
_product.Id = Convert.ToInt32(command.ExecuteScalar());
}
return this;
}
public IProductBuilder Read(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Products WHERE Id = @Id", connection);
command.Parameters.AddWithValue("@Id", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
_product = new Product
{
Id = (int)reader["Id"],
Name = (string)reader["Name"],
Price = (decimal)reader["Price"]
};
}
}
}
return this;
}
public IProductBuilder Update()
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Products SET Name = @Name, Price = @Price WHERE Id = @Id", connection);
command.Parameters.AddWithValue("@Id", _product.Id);
command.Parameters.AddWithValue("@Name", _product.Name);
command.Parameters.AddWithValue("@Price", _product.Price);
connection.Open();
command.ExecuteNonQuery();
}
return this;
}
public IProductBuilder Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Products WHERE Id = @Id", connection);
command.Parameters.AddWithValue("@Id", id);
connection.Open();
command.ExecuteNonQuery();
}
return this;
}
public Product GetProduct()
{
return _product;
}
}
public class EfProductBuilder : IProductBuilder
{
private Product _product;
private readonly DbContext _context;
public EfProductBuilder(DbContext context)
{
_context = context;
}
public IProductBuilder SetProduct(Product product)
{
_product = product;
return this;
}
public IProductBuilder Create()
{
_context.Set<Product>().Add(_product);
_context.SaveChanges();
return this;
}
public IProductBuilder Read(int id)
{
_product = _context.Set<Product>().Find(id);
return this;
}
public IProductBuilder Update()
{
_context.Set<Product>().Update(_product);
_context.SaveChanges();
return this;
}
public IProductBuilder Delete(int id)
{
var product = _context.Set<Product>().Find(id);
if (product != null)
{
_context.Set<Product>().Remove(product);
_context.SaveChanges();
}
return this;
}
public Product GetProduct()
{
return _product;
}
}
public class ProductBuilderFactory
{
public static IProductBuilder GetBuilder(string connectionString, bool useEfCore)
{
if (useEfCore)
{
var optionsBuilder = new DbContextOptionsBuilder<YourDbContext>();
optionsBuilder.UseSqlServer(connectionString);
var context = new YourDbContext(optionsBuilder.Options);
return new EfProductBuilder(context);
}
else
{
return new AdoNetProductBuilder(connectionString);
}
}
}
public class YourDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public YourDbContext(DbContextOptions<YourDbContext> options) : base(options)
{
}
}
// Usage
var connectionString = "YourConnectionStringHere";
var useEfCore = true; // Set to true for EF Core, false for ADO.NET
var builder = ProductBuilderFactory.GetBuilder(connectionString, useEfCore);
var product = new Product { Name = "New Product", Price = 10.99m };
builder.SetProduct(product).Create();
var createdProduct = builder.Read(product.Id).GetProduct();
createdProduct.Price = 12.99m;
builder.SetProduct(createdProduct).Update();
builder.Delete(createdProduct.Id);
////// MongoDB implementation with the above. /////
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
public class MongoProduct
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Name")]
public string Name { get; set; }
public decimal Price { get; set; }
}
public class MongoProductBuilder : IProductBuilder
{
private MongoProduct _product;
private readonly IMongoCollection<MongoProduct> _collection;
public MongoProductBuilder(string connectionString, string databaseName, string collectionName)
{
var client = new MongoClient(connectionString);
var database = client.GetDatabase(databaseName);
_collection = database.GetCollection<MongoProduct>(collectionName);
}
public IProductBuilder SetProduct(Product product)
{
_product = new MongoProduct
{
Name = product.Name,
Price = product.Price
};
return this;
}
public IProductBuilder Create()
{
_collection.InsertOne(_product);
return this;
}
public IProductBuilder Read(int id)
{
_product = _collection.Find(product => product.Id == id.ToString()).FirstOrDefault();
return this;
}
public IProductBuilder Update()
{
var filter = Builders<MongoProduct>.Filter.Eq(product => product.Id, _product.Id);
var update = Builders<MongoProduct>.Update.Set(product => product.Name, _product.Name)
.Set(product => product.Price, _product.Price);
_collection.UpdateOne(filter, update);
return this;
}
public IProductBuilder Delete(int id)
{
_collection.DeleteOne(product => product.Id == id.ToString());
return this;
}
public Product GetProduct()
{
return new Product
{
Id = Convert.ToInt32(_product.Id),
Name = _product.Name,
Price = _product.Price
};
}
}
public class ProductBuilderFactory
{
public static IProductBuilder GetBuilder(string connectionString, bool useEfCore, string databaseName = null, string collectionName = null)
{
if (useEfCore)
{
var optionsBuilder = new DbContextOptionsBuilder<YourDbContext>();
optionsBuilder.UseSqlServer(connectionString);
var context = new YourDbContext(optionsBuilder.Options);
return new EfProductBuilder(context);
}
else if (!string.IsNullOrEmpty(databaseName) && !string.IsNullOrEmpty(collectionName))
{
return new MongoProductBuilder(connectionString, databaseName, collectionName);
}
else
{
return new AdoNetProductBuilder(connectionString);
}
}
}
// Usage
var adoConnectionString = "YourADOConnectionStringHere";
var efConnectionString = "YourEFConnectionStringHere";
var mongoConnectionString = "YourMongoDBConnectionStringHere";
var useEfCore = true; // Set to true for EF Core, false for ADO.NET
var useMongoDB = false; // Set to true to use MongoDB, false otherwise
var databaseName = "YourMongoDBDatabaseName";
var collectionName = "YourMongoDBCollectionName";
var builder = ProductBuilderFactory.GetBuilder(useEfCore ? efConnectionString : adoConnectionString, useEfCore, useMongoDB ? databaseName : null, useMongoDB ? collectionName : null);
var product = new Product { Name = "New Product", Price = 10.99m };
builder.SetProduct(product).Create();
var createdProduct = builder.Read(product.Id).GetProduct();
createdProduct.Price = 12.99m;
builder.SetProduct(createdProduct).Update();
builder.Delete(createdProduct.Id);