Skip to content

Commit d5095f4

Browse files
committed
Use dapper methods and fix dependencies
1 parent 7796be5 commit d5095f4

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

Configuration.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
using System.Data.SqlClient;
12
using SimpleInventoryManagementSystem.Controller;
23
using SimpleInventoryManagementSystem.DAO;
34
using SimpleInventoryManagementSystem.Interfaces;
4-
using SimpleInventoryManagementSystem.Mapper;
55
using SimpleInventoryManagementSystem.Repository;
66

77
namespace SimpleInventoryManagementSystem;
@@ -13,21 +13,20 @@ public static IUserController BuildUserController()
1313
return new UserController(
1414
new ProductRepository(
1515
new SqlProductDao(
16-
GetSqlDataSource(),
17-
new SqlProductMapper()
16+
GetSqlConnection()
1817
)
1918
)
2019
);
2120
}
2221

23-
private static ISqlDataSource GetSqlDataSource()
22+
private static SqlConnection GetSqlConnection()
2423
{
2524
var connectionString = Environment.GetEnvironmentVariable("SQL_CONNECTION_STRING");
2625
if (connectionString == null)
2726
{
2827
throw new IOException("SQL_CONNECTION_STRING environmental variable was not found");
2928
}
3029

31-
return new SqlDataSource(connectionString);
30+
return new SqlConnection(connectionString);
3231
}
3332
}

src/DAO/SqlProductDao.cs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Data;
2-
using System.Data.SqlClient;
32
using Dapper;
43
using SimpleInventoryManagementSystem.Domain;
54
using SimpleInventoryManagementSystem.Interfaces;
@@ -8,54 +7,35 @@ namespace SimpleInventoryManagementSystem.DAO;
87

98
public class SqlProductDao : IProductDao
109
{
11-
private readonly ISqlDataSource _dataSource;
12-
private readonly ISqlProductMapper _mapper;
10+
private readonly IDbConnection _connection;
1311

14-
public SqlProductDao(ISqlDataSource dataSource, ISqlProductMapper mapper)
12+
public SqlProductDao(IDbConnection connection)
1513
{
16-
_dataSource = dataSource;
17-
_mapper = mapper;
14+
_connection = connection;
1815
}
1916

2017
public async Task<IEnumerable<Product>> GetAllProductsAsync()
2118
{
2219
const string sql = "SELECT * FROM Products";
23-
await using var reader = await _dataSource.ExecuteQueryAsync(sql, new List<SqlParameter>());
24-
25-
var products = new List<Product>();
26-
while (await reader.ReadAsync())
27-
{
28-
products.Add(_mapper.MapToDomain(reader));
29-
}
30-
31-
return products;
20+
return await _connection.QueryAsync<Product>(sql);
3221
}
3322

3423
public async Task<Product?> GetProductByNameAsync(string productName)
3524
{
3625
const string sql = "SELECT * FROM Products WHERE Name = @Name";
37-
var parameters = new List<SqlParameter>
38-
{
39-
new("@Name", productName)
40-
};
41-
await using var reader = await _dataSource.ExecuteQueryAsync(sql, parameters);
42-
return reader.Read() ? _mapper.MapToDomain(reader) : null;
26+
var results = await _connection.QueryAsync<Product>(sql, new { Name = productName });
27+
return results.FirstOrDefault();
4328
}
4429

4530
public async Task DeleteProductByNameAsync(string productName)
4631
{
4732
const string sql = "DELETE FROM Products WHERE Name = @Name";
48-
var parameters = new List<SqlParameter>
49-
{
50-
new("@Name", productName)
51-
};
52-
await _dataSource.ExecuteNonQueryAsync(sql, parameters);
33+
await _connection.ExecuteAsync(sql, new { Name = productName });
5334
}
5435

5536
public async Task AddProductAsync(Product product)
5637
{
5738
const string sql = "INSERT INTO Products (Name, Price, Quantity) VALUES (@Name, @Price, @Quantity)";
58-
var parameters = _mapper.MapToParameters(product);
59-
await _dataSource.ExecuteNonQueryAsync(sql, parameters);
39+
await _connection.ExecuteAsync(sql, product);
6040
}
6141
}

0 commit comments

Comments
 (0)