From 9a411806996b6c0b6df1f7a8ab9a0e5ea93a5cc7 Mon Sep 17 00:00:00 2001 From: Huang_h <2524948355@qq.com> Date: Fri, 5 Jun 2020 22:34:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5=E5=B0=81?= =?UTF-8?q?=E8=A3=85=E7=9A=84=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DITest/Controllers/DemoController.cs" | 33 +++++++++++++++ .../DITest/DITest.csproj" | 1 + .../DITest/Helper/Jsonhelper.cs" | 0 .../DITest/Interface/IRespository.cs" | 7 ++-- "\351\273\204\347\204\225/DITest/Startup.cs" | 4 ++ .../DITest/implement/EFRespository.cs" | 42 ++++++++++++------- 6 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 "\351\273\204\347\204\225/DITest/Controllers/DemoController.cs" rename "\351\273\204\347\204\225/DITest/Herper/Jsonherper.cs" => "\351\273\204\347\204\225/DITest/Helper/Jsonhelper.cs" (100%) diff --git "a/\351\273\204\347\204\225/DITest/Controllers/DemoController.cs" "b/\351\273\204\347\204\225/DITest/Controllers/DemoController.cs" new file mode 100644 index 0000000..07b29a9 --- /dev/null +++ "b/\351\273\204\347\204\225/DITest/Controllers/DemoController.cs" @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DITest.Damain.Entity; +using DITest.Interface; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using DITest.Herper; + +namespace DITest.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class DemoController : ControllerBase + { + private readonly IRespository_userRespository; + + public DemoController(IRespository userRespository) + { + _userRespository = userRespository; + } + + public string Get() + { + var list = _userRespository.Table.ToList(); + return JsonHerper.SerializeObject(list); + } + + + } +} \ No newline at end of file diff --git "a/\351\273\204\347\204\225/DITest/DITest.csproj" "b/\351\273\204\347\204\225/DITest/DITest.csproj" index 133a382..d742fff 100644 --- "a/\351\273\204\347\204\225/DITest/DITest.csproj" +++ "b/\351\273\204\347\204\225/DITest/DITest.csproj" @@ -14,6 +14,7 @@ + diff --git "a/\351\273\204\347\204\225/DITest/Herper/Jsonherper.cs" "b/\351\273\204\347\204\225/DITest/Helper/Jsonhelper.cs" similarity index 100% rename from "\351\273\204\347\204\225/DITest/Herper/Jsonherper.cs" rename to "\351\273\204\347\204\225/DITest/Helper/Jsonhelper.cs" diff --git "a/\351\273\204\347\204\225/DITest/Interface/IRespository.cs" "b/\351\273\204\347\204\225/DITest/Interface/IRespository.cs" index 1ed99b0..2225afd 100644 --- "a/\351\273\204\347\204\225/DITest/Interface/IRespository.cs" +++ "b/\351\273\204\347\204\225/DITest/Interface/IRespository.cs" @@ -5,11 +5,12 @@ using System.Threading.Tasks; namespace DITest.Interface { - interface IRespository + public interface IRespository { + IQueryable Table { get; } T GetById(int id); - IEnumerable GetAllEntity(); - int Insert(T entity); + + void Insert(T entity); void InserBulk(IEnumerable list); void Update(T entity); diff --git "a/\351\273\204\347\204\225/DITest/Startup.cs" "b/\351\273\204\347\204\225/DITest/Startup.cs" index 29dc52f..ab26404 100644 --- "a/\351\273\204\347\204\225/DITest/Startup.cs" +++ "b/\351\273\204\347\204\225/DITest/Startup.cs" @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DITest.Damain; +using DITest.implement; +using DITest.Interface; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; @@ -27,6 +29,8 @@ namespace DITest { services.AddDbContext(); + services.AddScoped(typeof(IRespository<>), typeof(EFRespository<>)); + services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } diff --git "a/\351\273\204\347\204\225/DITest/implement/EFRespository.cs" "b/\351\273\204\347\204\225/DITest/implement/EFRespository.cs" index 4811888..f80b2ed 100644 --- "a/\351\273\204\347\204\225/DITest/implement/EFRespository.cs" +++ "b/\351\273\204\347\204\225/DITest/implement/EFRespository.cs" @@ -13,17 +13,27 @@ namespace DITest.implement private readonly Admin1100DbContext db; - private DbSet entity; - public DbSet Table + private DbSet _entity; + + private DbSet Entity { get { - if(entity == null) + if (_entity == null) { - entity = db.Set(); + _entity = db.Set(); } - return entity; + return _entity; + } + } + + public IQueryable Table + { + get + { + + return Entity; } } @@ -33,7 +43,7 @@ namespace DITest.implement } public void Delete(T entity) { - this.Table.Remove(entity); + this._entity.Remove(entity); db.SaveChanges(); } public void Delete(int id) @@ -44,30 +54,30 @@ namespace DITest.implement public T GetById(int id) { - throw new NotImplementedException(); + return _entity.Where(x => x.Id == id).FirstOrDefault(); } - public IEnumerable GetAllEntity() - { - throw new NotImplementedException(); - } - public int Insert(T entity) + + public void Insert(T entity) { - throw new NotImplementedException(); + _entity.Add(entity); + db.SaveChanges(); + } public void InserBulk(IEnumerable list) { - throw new NotImplementedException(); + _entity.AddRange(list); + db.SaveChanges(); } public void Update(T entity) { - this.Table.Update(entity); + _entity.Update(entity); db.SaveChanges(); } - + } } -- Gitee