From 3f6e36c0df0b9e42894d0c17db8c8498449d06dd Mon Sep 17 00:00:00 2001 From: wangxianhui <1666843663@qq.com> Date: Wed, 17 Jun 2020 20:08:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=9D=E8=B3=B4=E6=B3=A8=E5=85=A5=E5=92=8CJs?= =?UTF-8?q?on=E5=AE=8C=E6=95=B4=E7=89=88=E5=84=AA=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.gitignore" | 0 ...WebAPI\342\200\224\342\200\224DI_json.sln" | 0 .../Controllers/TestController.cs" | 23 ++++++++++++++---- .../Controllers/WeatherForecastController.cs" | 0 .../DoMain/AdminDbContext.cs" | 0 .../DoMain/BaseEntity.cs" | 0 .../DoMain/DbInitializer.cs" | 24 ++++++++++++------- .../DoMain/Entity/Role.cs" | 0 .../DoMain/Entity/User.cs" | 0 .../Helper/JsonHelper.cs" | 0 .../Implementation/EfRespository.cs" | 22 ++++++++--------- .../Interface/IRespository.cs" | 6 ++--- .../Program.cs" | 0 .../Properties/launchSettings.json" | 0 .../Startup.cs" | 4 ++-- .../WeatherForecast.cs" | 0 ...API\342\200\224\342\200\224DI_json.csproj" | 0 .../appsettings.Development.json" | 0 .../appsettings.json" | 0 19 files changed, 49 insertions(+), 30 deletions(-) rename "\347\216\213\347\216\260\350\276\211/.gitignore" => "\347\216\213\347\217\276\347\205\207/.gitignore" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json.sln" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json.sln" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" (42%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" (59%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" (74%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" (78%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Program.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Program.cs" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" (93%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" (100%) rename "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.json" => "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.json" (100%) diff --git "a/\347\216\213\347\216\260\350\276\211/.gitignore" "b/\347\216\213\347\217\276\347\205\207/.gitignore" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/.gitignore" rename to "\347\216\213\347\217\276\347\205\207/.gitignore" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json.sln" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json.sln" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json.sln" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json.sln" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" similarity index 42% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" index bfc12e0..01403fc 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/TestController.cs" @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -15,17 +16,29 @@ namespace WebAPI__DI_json.Controllers [ApiController] public class TestController : ControllerBase { - private readonly IRespository _UserResitory; + private readonly IRespository _UserRespository; + private readonly IRespository _RoleRespository; - public TestController(IRespository respository) + public TestController(IRespository Urespository, IRespository Rrespository) { - _UserResitory = respository; + _UserRespository = Urespository; + _RoleRespository = Rrespository; } public string Get() { - var list = _UserResitory.Table.ToList(); - return JsonHelper.JsonSerializeSetting(list); + var users = _UserRespository.Table.ToList(); + + //var roles = _RoleRespository.Table.FirstOrDefault(); + + //_UserRespository.Insert(new User + //{ + // UserName = "www", + // PassWord = ".com", + // RoleId = roles.Ide + //}); + + return JsonHelper.JsonSerializeSetting(users); } } } diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Controllers/WeatherForecastController.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/AdminDbContext.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/BaseEntity.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" similarity index 59% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" index 9ccec36..82b543a 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/DbInitializer.cs" @@ -1,19 +1,25 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; using WebAPI__DI_json.DoMain.Entity; +using WebAPI__DI_json.Implementation; +using WebAPI__DI_json.Interface; namespace WebAPI__DI_json.DoMain { public class DbInitializer { - public static void Seed() + public static void Seed(IServiceProvider serviceProvider) { - var db = new AdminDbContext(); - using (db) + using (var scope = serviceProvider.CreateScope()) { + var db = scope.ServiceProvider.GetService(typeof(AdminDbContext)) as AdminDbContext;//强制轉換類型 + var _userRespository = scope.ServiceProvider.GetService(typeof(IRespository)) as EfRespository; + var _RoleRespository = scope.ServiceProvider.GetService(typeof(IRespository)) as EfRespository; + db.Database.EnsureCreated(); var HasUser = db.Users.Any(); @@ -25,10 +31,9 @@ namespace WebAPI__DI_json.DoMain Description = "此处省略一百字" }; - db.Roles.Add(role); - db.SaveChanges(); + _RoleRespository.Insert(role); - db.Users.AddRange(new User[] + _userRespository.InsertBulk(new User[] { new User { @@ -45,13 +50,14 @@ namespace WebAPI__DI_json.DoMain } }); - db.SaveChanges(); + } + } + //var db = new AdminDbContext(); - } } } } diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/Role.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/DoMain/Entity/User.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Helper/JsonHelper.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" similarity index 74% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" index f6dc5cd..39f4474 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Implementation/EfRespository.cs" @@ -18,9 +18,9 @@ namespace WebAPI__DI_json.Implementation db = dbContext; } - public DbSet _entity; + private DbSet _entity; - public IQueryable Entity + protected DbSet Entity { get { @@ -44,28 +44,28 @@ namespace WebAPI__DI_json.Implementation public T GetById(int Id) { - return _entity.Where(x => x.Id == Id).FirstOrDefault(); + return Table.Where(x => x.Id == Id).FirstOrDefault(); } //删 - public void Delete(T Entity) + public void Delete(T entity) { - this._entity.Remove(Entity); + Entity.Remove(entity); db.SaveChanges(); } public void Delete(int Id) { - var Del = Entity.Where(x => x.Id == Id).FirstOrDefault(); + var Del = Table.Where(x => x.Id == Id).FirstOrDefault(); Delete(Del); } //增 - public void Insert(T Entity) + public void Insert(T entity) { - _entity.Add(Entity); + Entity.Add(entity); db.SaveChanges(); } @@ -73,14 +73,14 @@ namespace WebAPI__DI_json.Implementation public void InsertBulk(IEnumerable list) { - _entity.AddRange(list); + Entity.AddRange(list); db.SaveChanges(); } //改 - public void Update(T Entity) + public void Update(T entity) { - _entity.Update(Entity); + Entity.Update(entity); db.SaveChanges(); } } diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" similarity index 78% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" index b53d50e..f9a5e60 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Interface/IRespository.cs" @@ -11,13 +11,13 @@ namespace WebAPI__DI_json.Interface IQueryable Table { get; } - void Insert(T Entity); + void Insert(T entity); void InsertBulk(IEnumerable list); - void Update(T Entity); + void Update(T entity); - void Delete(T Entity); + void Delete(T entity); void Delete(int Id); diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Program.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Program.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Program.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Program.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Properties/launchSettings.json" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" similarity index 93% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" index 61f5072..951d7ed 100644 --- "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" +++ "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/Startup.cs" @@ -28,7 +28,7 @@ namespace WebAPI__DI_json public void ConfigureServices(IServiceCollection services) { services.AddDbContext(); - services.AddScoped(typeof(IRespository<>), typeof(EfRespository<>)); + services.AddScoped(typeof(IRespository<>), typeof(EfRespository<>));//עķҲһN services.AddControllers(); } @@ -49,7 +49,7 @@ namespace WebAPI__DI_json endpoints.MapControllers(); }); - DbInitializer.Seed(); + DbInitializer.Seed(app.ApplicationServices);//һӿ } } } diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WeatherForecast.cs" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/WebAPI\342\200\224\342\200\224DI_json.csproj" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.Development.json" diff --git "a/\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.json" "b/\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.json" similarity index 100% rename from "\347\216\213\347\216\260\350\276\211/WebAPI\342\200\224\342\200\224DI_json/appsettings.json" rename to "\347\216\213\347\217\276\347\205\207/WebAPI\342\200\224\342\200\224DI_json/appsettings.json" -- Gitee