base surging
本示例项目是基于 Surging https://github.com/dotnetcore/surging
运行下面脚本,生成user表
clone代码 git clone https://github.com/billyang/SurgingDemo.git
D:\git\surging
D:\git\SurgingDemo
这样做的好处:
- 是和 surging 保持最新代码
- 是方便学习surging和调试,毕竟你想使用surging、理解surging才是踏出第一步
ApiGateway 使用 surging 的例子,当然正式开发建议自己重写 ApiGateway
服务管理使用 consul,因为调试简单,只需 consul agent -dev 即可开启consul
假设你已经把SurgingDemo已运行起来了,即可对根据Dapper对User进行增删改查
public class UserRepository: BaseRepository, IBaseRepository<User>
{
/// <summary>
/// 创建一个用户
/// </summary>
/// <param name="entity">用户</param>
/// <param name="connectionString">链接字符串</param>
/// <returns></returns>
public Task<Boolean> CreateEntity(User entity, String connectionString = null)
{
using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString))
{
string insertSql = @"INSERT INTO dbo.auth_User
( TenantId ,
Name ,
Password ,
SecurityStamp ,
FullName ,
Surname ,
PhoneNumber ,
IsPhoneNumberConfirmed ,
EmailAddress ,
IsEmailConfirmed ,
EmailConfirmationCode ,
IsActive ,
PasswordResetCode ,
LastLoginTime ,
IsLockoutEnabled ,
AccessFailedCount ,
LockoutEndDateUtc
)
VALUES ( @tenantid ,
@name ,
@password ,
@securitystamp ,
@fullname ,
@surname ,
@phonenumber ,
@isphonenumberconfirmed ,
@emailaddress ,
@isemailconfirmed ,
@emailconfirmationcode ,
@isactive ,
@passwordresetcode ,
@lastlogintime ,
@islockoutenabled ,
@accessfailedcount ,
@lockoutenddateutc
);";
return Task.FromResult<Boolean>(conn.Execute(insertSql, entity) > 0);
}
}
}
Task<UserDto> GetUserById(Int64 id);
Task<Boolean> UpdateUser(UserDto user);
Task<Boolean> DeleteUser(Int64 userId);
如:
public async Task<UserDto> GetUserById(Int64 id)
{
var user = await _repository.GetEntityById(id);
return new UserDto()
{
Id = user.Id,
EmailAddress = user.EmailAddress,
Name = user.Name,
PhoneNumber = user.PhoneNumber,
Surname = user.Surname,
TenantId = user.TenantId,
FullName = user.FullName,
};
}
public async Task<Boolean> UpdateUser(UserDto user)
{
var entity = await _repository.GetEntityById(user.Id);
entity.Name = user.Name;
entity.Password = user.Password;
entity.FullName = user.FullName;
entity.Surname = user.Surname;
entity.EmailAddress = user.EmailAddress;
entity.PhoneNumber = user.PhoneNumber;
return await _repository.Update(entity);
}
public async Task<Boolean> DeleteUser(Int64 userId)
{
return await _repository.Delete(userId);
}
public async Task<IActionResult> Delete(Int64 id)
{
var service = ServiceLocator.GetService<IServiceProxyFactory>();
var userProxy = service.CreateProxy<IUserService>("User");
await userProxy.DeleteUser(id);
return RedirectToAction("User");
}
public async Task<JsonResult> GetUser(Int64 id)
{
var service = ServiceLocator.GetService<IServiceProxyFactory>();
var userProxy = service.CreateProxy<IUserService>("User");
var output= await userProxy.GetUserById(id);
return new JsonResult(output);
}
public async Task<JsonResult> Update(UserDto dto)
{
var service = ServiceLocator.GetService<IServiceProxyFactory>();
var userProxy = service.CreateProxy<IUserService>("User");
var output = await userProxy.UpdateUser(dto);
return new JsonResult(output);
}