Skip to content

Commit 09badc8

Browse files
Part 3.
1 parent c601062 commit 09badc8

File tree

7 files changed

+191
-0
lines changed

7 files changed

+191
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace UserManagement.Application.Common.Interfaces
2+
{
3+
public interface IConfigConstants
4+
{
5+
string FullStackConnection { get; }
6+
string TestFullStackConnection { get; }
7+
int LongRunningProcessMilliseconds { get; }
8+
string MSG_USER_NULLUSERID { get; }
9+
string MSG_USER_NULLFIRSTNAME { get; }
10+
string MSG_USER_NULLLASTNAME { get; }
11+
string MSG_USER_NULLDOB { get; }
12+
string MSG_USER_NULLGENDER { get; }
13+
string MSG_USER_GENDER_LEN { get; }
14+
string MSG_USER_NULLEMAILADDR { get; }
15+
string MSG_USER_NULLPHNUM { get; }
16+
string MSG_USER_NULLCITY { get; }
17+
string MSG_USER_NULLSTATE { get; }
18+
string MSG_USER_NULLCOUNTRY { get; }
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace UserManagement.Persistence.Constant
2+
{
3+
using Microsoft.Extensions.Configuration;
4+
using UserManagement.Application.Common.Interfaces;
5+
public class ConfigConstants : IConfigConstants
6+
{
7+
public IConfiguration Configuration { get; }
8+
public ConfigConstants(IConfiguration configuration)
9+
{
10+
this.Configuration = configuration;
11+
}
12+
public string FullStackConnection => this.Configuration.GetConnectionString("FullStackConnection");
13+
14+
public string TestFullStackConnection => this.Configuration.GetConnectionString("TestFullStackConnection");
15+
public int LongRunningProcessMilliseconds => int.Parse(this.Configuration["AppSettings:LongRunningProcessMilliseconds"]);
16+
17+
public string MSG_USER_NULLUSERID => this.Configuration["AppSettings:MSG_USER_NULLUSERID"];
18+
public string MSG_USER_NULLFIRSTNAME => this.Configuration["AppSettings:MSG_USER_NULLFIRSTNAME"];
19+
public string MSG_USER_NULLLASTNAME => this.Configuration["AppSettings:MSG_USER_NULLLASTNAME"];
20+
public string MSG_USER_NULLDOB => this.Configuration["AppSettings:MSG_USER_NULLDOB"];
21+
public string MSG_USER_NULLGENDER => this.Configuration["AppSettings:MSG_USER_NULLGENDER"];
22+
public string MSG_USER_GENDER_LEN => this.Configuration["AppSettings:MSG_USER_GENDER_LEN"];
23+
public string MSG_USER_NULLEMAILADDR => this.Configuration["AppSettings:MSG_USER_NULLEMAILADDR"];
24+
public string MSG_USER_NULLPHNUM => this.Configuration["AppSettings:MSG_USER_NULLPHNUM"];
25+
public string MSG_USER_NULLCITY => this.Configuration["AppSettings:MSG_USER_NULLCITY"];
26+
public string MSG_USER_NULLSTATE => this.Configuration["AppSettings:MSG_USER_NULLSTATE"];
27+
public string MSG_USER_NULLCOUNTRY => this.Configuration["AppSettings:MSG_USER_NULLCOUNTRY"];
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace UserManagement.Persistence
2+
{
3+
using Microsoft.Extensions.DependencyInjection;
4+
using System.Data;
5+
using System.Data.SqlClient;
6+
using UserManagement.Application.Common.Interfaces;
7+
using UserManagement.Domain.UnitOfWork;
8+
using UserManagement.Persistence.Constant;
9+
public static class DependencyInjection
10+
{
11+
public static IServiceCollection AddPersistance(this IServiceCollection services)
12+
{
13+
services.AddSingleton<IConfigConstants, ConfigConstants>();
14+
services.AddSingleton<IDbConnection>(conn => new SqlConnection(conn.GetService<IConfigConstants>().FullStackConnection));
15+
services.AddTransient<IUnitOfWork>(uof => new UnitOfWork.UnitOfWork(uof.GetService<IDbConnection>()));
16+
return services;
17+
}
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace UserManagement.Persistence.Repositories
2+
{
3+
using System.Data;
4+
public class Repository
5+
{
6+
protected IDbConnection DbConnection { get; }
7+
protected IDbTransaction DbTransaction { get; }
8+
public Repository(IDbConnection dbConnection, IDbTransaction dbTransaction)
9+
{
10+
DbTransaction = dbTransaction;
11+
DbConnection = dbConnection;
12+
}
13+
}
14+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace UserManagement.Persistence.Repositories
2+
{
3+
using Dapper.Contrib.Extensions;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Data;
7+
using System.Threading.Tasks;
8+
using UserManagement.Domain.Entities;
9+
using UserManagement.Domain.Repositories;
10+
11+
public class UserRepository : Repository, IUserRepository
12+
{
13+
public UserRepository(IDbConnection dbConnection, IDbTransaction dbtransaction)
14+
: base(dbConnection, dbtransaction)
15+
{
16+
}
17+
public Task<int> AddUser(User user)
18+
{
19+
user.DateAdded = DateTime.Now;
20+
user.DateUpdated = null;
21+
return DbConnection.InsertAsync(user, DbTransaction);
22+
}
23+
24+
public Task<bool> DeleteUser(int userId)
25+
{
26+
return DbConnection.DeleteAsync(new User { UserID = userId }, DbTransaction);
27+
}
28+
29+
public Task<IEnumerable<User>> GetAllUsers()
30+
{
31+
return DbConnection.GetAllAsync<User>();
32+
}
33+
34+
public Task<User> GetUser(long userId)
35+
{
36+
return DbConnection.GetAsync<User>(userId);
37+
}
38+
39+
public Task<bool> UpdateUser(User user)
40+
{
41+
user.DateUpdated = DateTime.Now;
42+
return DbConnection.UpdateAsync(user, DbTransaction);
43+
}
44+
45+
public Task<bool> DeleteAllUser()
46+
{
47+
return DbConnection.DeleteAllAsync<User>();
48+
}
49+
}
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace UserManagement.Persistence.UnitOfWork
2+
{
3+
using System.Data;
4+
using UserManagement.Domain.Repositories;
5+
using UserManagement.Domain.UnitOfWork;
6+
using UserManagement.Persistence.Repositories;
7+
8+
public class UnitOfWork : IUnitOfWork
9+
{
10+
private IDbConnection dbConnection;
11+
private IDbTransaction transaction;
12+
public UnitOfWork(IDbConnection dbConnection)
13+
{
14+
this.dbConnection = dbConnection;
15+
this.ManageConnection();
16+
}
17+
public IUserRepository Users => new UserRepository(this.dbConnection, this.transaction);
18+
19+
public void StartTransaction()
20+
{
21+
22+
if (this.transaction == null)
23+
{
24+
this.transaction = this.dbConnection.BeginTransaction();
25+
}
26+
}
27+
public void Commit()
28+
{
29+
try
30+
{
31+
this.transaction.Commit();
32+
}
33+
catch
34+
{
35+
this.transaction.Rollback();
36+
}
37+
}
38+
39+
private void ManageConnection()
40+
{
41+
if (this.dbConnection.State == ConnectionState.Closed)
42+
{
43+
this.dbConnection.Open();
44+
}
45+
}
46+
}
47+
}

UserManagement.Persistence/UserManagement.Persistence.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,16 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Dapper.Contrib" Version="2.0.35" />
9+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.9" />
10+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.9" />
11+
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\UserManagement.Application\UserManagement.Application.csproj" />
16+
<ProjectReference Include="..\UserManagement.Domain\UserManagement.Domain.csproj" />
17+
</ItemGroup>
18+
719
</Project>

0 commit comments

Comments
 (0)