From 1cb1a852b79026c59aa0d8a7bfcfaabf277e76f6 Mon Sep 17 00:00:00 2001 From: Huang_h <2524948355@qq.com> Date: Fri, 5 Jun 2020 00:15:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=84=E7=84=95=E7=9A=84=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DITest/Controllers/ValuesController.cs" | 11 +-- .../DITest/DITest.csproj" | 2 + .../DITest/Herper/Jsonherper.cs" | 21 ++++++ .../DITest/Interface/IRespository.cs" | 20 +++++ .../DITest/implement/EFRespository.cs" | 73 +++++++++++++++++++ 5 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 "\351\273\204\347\204\225/DITest/Herper/Jsonherper.cs" create mode 100644 "\351\273\204\347\204\225/DITest/Interface/IRespository.cs" create mode 100644 "\351\273\204\347\204\225/DITest/implement/EFRespository.cs" diff --git "a/\351\273\204\347\204\225/DITest/Controllers/ValuesController.cs" "b/\351\273\204\347\204\225/DITest/Controllers/ValuesController.cs" index d9ce69d..fa54828 100644 --- "a/\351\273\204\347\204\225/DITest/Controllers/ValuesController.cs" +++ "b/\351\273\204\347\204\225/DITest/Controllers/ValuesController.cs" @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using DITest.Damain; using DITest.Damain.Entity; +using DITest.Herper; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -32,15 +33,7 @@ namespace DITest.Controllers var res = _db.Users.ToList(); - - var x = JsonConvert.SerializeObject(res, Formatting.Indented, new JsonSerializerSettings - { - ReferenceLoopHandling = ReferenceLoopHandling.Ignore, - DateFormatString = "yyyy-MM-dd HH:mm:ss" - }); - - - return x; + return JsonHerper.SerializeObject(res); //var list = new List(); diff --git "a/\351\273\204\347\204\225/DITest/DITest.csproj" "b/\351\273\204\347\204\225/DITest/DITest.csproj" index 5da714e..133a382 100644 --- "a/\351\273\204\347\204\225/DITest/DITest.csproj" +++ "b/\351\273\204\347\204\225/DITest/DITest.csproj" @@ -6,6 +6,8 @@ + + diff --git "a/\351\273\204\347\204\225/DITest/Herper/Jsonherper.cs" "b/\351\273\204\347\204\225/DITest/Herper/Jsonherper.cs" new file mode 100644 index 0000000..a2da48c --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Herper/Jsonherper.cs" @@ -0,0 +1,21 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DITest.Herper +{ + public class JsonHerper + { + + public static string SerializeObject(object obj) + { + return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + DateFormatString = "yyyy-MM-dd HH:mm:ss" + }); + } + } +} diff --git "a/\351\273\204\347\204\225/DITest/Interface/IRespository.cs" "b/\351\273\204\347\204\225/DITest/Interface/IRespository.cs" new file mode 100644 index 0000000..1ed99b0 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Interface/IRespository.cs" @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DITest.Interface +{ + interface IRespository + { + T GetById(int id); + IEnumerable GetAllEntity(); + int Insert(T entity); + + void InserBulk(IEnumerable list); + void Update(T entity); + void Delete(T entity); + void Delete(int id); + + } +} diff --git "a/\351\273\204\347\204\225/DITest/implement/EFRespository.cs" "b/\351\273\204\347\204\225/DITest/implement/EFRespository.cs" new file mode 100644 index 0000000..4811888 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/implement/EFRespository.cs" @@ -0,0 +1,73 @@ +using DITest.Damain; +using DITest.Interface; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DITest.implement +{ + public class EFRespository : IRespository where T : BaseEntity + { + + private readonly Admin1100DbContext db; + + private DbSet entity; + + public DbSet Table + { + get + { + if(entity == null) + { + entity = db.Set(); + } + return entity; + } + } + + public EFRespository(Admin1100DbContext dbContext) + { + db = dbContext; + } + public void Delete(T entity) + { + this.Table.Remove(entity); + db.SaveChanges(); + } + public void Delete(int id) + { + var row = Table.Where(x => x.Id == id).FirstOrDefault(); + Delete(row); + } + + public T GetById(int id) + { + throw new NotImplementedException(); + } + + public IEnumerable GetAllEntity() + { + throw new NotImplementedException(); + } + + public int Insert(T entity) + { + throw new NotImplementedException(); + } + + public void InserBulk(IEnumerable list) + { + throw new NotImplementedException(); + } + + public void Update(T entity) + { + this.Table.Update(entity); + db.SaveChanges(); + } + + + } +} -- Gitee