Guest User

Untitled

a guest
May 24th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. string UserName = System.Web.HttpContext.Current.User.Identity.Name
  2.  
  3. public interface IAuditable{
  4. DateTime CreateDate{get;set;}
  5. string UserName{get;set;}
  6. }
  7.  
  8. public class MyEntity:IAuditable{
  9. public int ID{get;set;}
  10. public string Name{get;set;}
  11. public string Information{get;set;}
  12. }
  13.  
  14. public static class Auditor{
  15. public static IAuditable Stamp(IAuditable model){
  16. model.CreateDate=DateTime.UtcNow;
  17. model.CreatedBy=System.Web.HttpContext.Current.User.Identity.Name;
  18. return model;
  19. }
  20. }
  21.  
  22. public sealed class MyService:IDisposable{
  23. MyDb db=new MyDb();
  24. public async Task<int> Create(MyEntity model){
  25. Auditor.Stamp(model);
  26. db.MyEntities.Add(model);
  27. return await db.SaveAsync();
  28. }
  29. public void Dispose(){
  30. db.Dispose();
  31. }
  32. }
  33.  
  34. [HttpPost]
  35. [ValidateAntiForgeryToken]
  36. public async Task<IActionResult> Create(MyEntity model)
  37. {
  38. await service.Create(model);
  39. return RedirectToAction("Index")
  40. }
  41.  
  42. public class UserResolverService
  43. {
  44. private readonly IHttpContextAccessor _context;
  45. public UserResolverService(IHttpContextAccessor context)
  46. {
  47. _context = context;
  48. }
  49.  
  50. public string GetUser()
  51. {
  52. return await _context.HttpContext.User?.Identity?.Name;
  53. }
  54. }
  55.  
  56. public interface IAuditor {
  57. IAuditable Stamp(IAuditable model);
  58. }
  59.  
  60. public class Auditor : IAuditor {
  61. private readonly IHttpContextAccessor accessor;
  62.  
  63. public Auditor(IHttpContextAccessor accessor) {
  64. this.accessor = accessor;
  65. }
  66.  
  67. public IAuditable Stamp(IAuditable model){
  68. model.CreateDate = DateTime.UtcNow;
  69. model.CreatedBy = accessor.HttpContext.User?.Identity?.Name;
  70. return model;
  71. }
  72. }
  73.  
  74. public sealed class MyService:IDisposable {
  75. MyDb db = new MyDb();
  76. private readonly IAuditor auditor;
  77.  
  78. public MyService(IAuditor auditor) {
  79. this.auditor = auditor;
  80. }
  81.  
  82. public async Task<int> Create(MyEntity model) {
  83. auditor.Stamp(model);
  84. db.MyEntities.Add(model);
  85. return await db.SaveAsync();
  86. }
  87.  
  88. public void Dispose() {
  89. db.Dispose();
  90. }
  91. }
  92.  
  93. public static IServiceCollection AddMyLibrary(this IServiceCollection services) {
  94.  
  95. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  96. services.AddSingleton<IAuditor, Auditor>();
  97. services.AddScoped<IMyService, MyService>();
  98.  
  99. //...add other services as needed
  100.  
  101. return services.
  102. }
  103.  
  104. public void ConfigureServices(IServiceCollection services) {
  105.  
  106. //...
  107.  
  108. services.AddMyLibrary();
  109.  
  110. //...
  111. }
Add Comment
Please, Sign In to add comment