diff --git "a/\347\216\213\344\277\212/WebAPI10086/Controllers/TestController.cs" "b/\347\216\213\344\277\212/WebAPI10086/Controllers/TestController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..6acff84c85dc3091c13201dd2073c482dbd5f537 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Controllers/TestController.cs" @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using WebAPI10086.Domain.Helper; +using WebAPI10086.Infterface; + +namespace WebAPI10086.Controllers +{ + [Route("[controller]")] + [ApiController] + public class TestController : ControllerBase + { + private readonly IRespository _brandRespository; + + public TestController(IRespository brandRespository) + { + _brandRespository = brandRespository; + } + + public string Get() + { + var list = _brandRespository.Table.ToList(); + return JsonHelper.SerializeObject(list); + } + + [Route("{id}")] + public string Get(int id) + { + var list = _brandRespository.GetById(id); + return JsonHelper.SerializeObject(list); + } + + + } +} \ No newline at end of file diff --git "a/\347\216\213\344\277\212/WebAPI10086/Impementation/EfRespository.cs" "b/\347\216\213\344\277\212/WebAPI10086/Impementation/EfRespository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..13910337551864410c1260176a8b15493d62329a --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Impementation/EfRespository.cs" @@ -0,0 +1,81 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebAPI10086.Infterface; + +namespace WebAPI10086.Impementation +{ + public class EfRespository : IRespository where T : BaseEntity + { + private readonly Admin10086DbContext db; + + private DbSet _entity; + + private DbSet Entity + { + get + { + if (_entity == null) + { + _entity = db.Set(); + } + return _entity; + } + + } + public IQueryable + Table + { + get + { + return Entity; + } + } + + + public EfRespository(Admin10086DbContext dbContext) + { + db = dbContext; + } + + + public void Delete(T entity) + { + this._entity.Remove(entity); + db.SaveChanges(); + } + + public void Delete(int id) + { + var rew = Table.Where(x => x.Id == id).FirstOrDefault(); + Delete(rew); + db.SaveChanges(); + } + + + public T GetById(int id) + { + return Table.Where(x => x.Id == id).FirstOrDefault(); + } + + public void Insert(T entity) + { + _entity.Add(entity); + db.SaveChanges(); + } + + public void InsertBulk(IEnumerable list) + { + _entity.AddRange(list); + db.SaveChanges(); + } + + public void Update(T entity) + { + _entity.Update(entity); + db.SaveChanges(); + } + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/Infterface/IRespository.cs" "b/\347\216\213\344\277\212/WebAPI10086/Infterface/IRespository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..de5e819b3e111e23461c23896c5952bf29134d12 --- /dev/null +++ "b/\347\216\213\344\277\212/WebAPI10086/Infterface/IRespository.cs" @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebAPI10086.Infterface +{ + public interface IRespository + { + IQueryable Table { get; } + + T GetById(int id); + + void Insert(T entity); + + void InsertBulk(IEnumerable list); + + void Update(T entity); + + void Delete(T entity); + + void Delete(int id); + + + + } +} diff --git "a/\347\216\213\344\277\212/WebAPI10086/Startup.cs" "b/\347\216\213\344\277\212/WebAPI10086/Startup.cs" index 0509f2ced7f277a9da1440ca8028d3540303a70f..292d78a4ddc1c0fffdda6af3bf51ca711791fc22 100644 --- "a/\347\216\213\344\277\212/WebAPI10086/Startup.cs" +++ "b/\347\216\213\344\277\212/WebAPI10086/Startup.cs" @@ -10,6 +10,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using WebAPI10086.Controllers; +using WebAPI10086.Impementation; +using WebAPI10086.Infterface; namespace WebAPI10086 { @@ -26,6 +28,9 @@ namespace WebAPI10086 public void ConfigureServices(IServiceCollection services) { services.AddDbContext(); + + services.AddScoped(typeof(IRespository<>), typeof(EfRespository<>)); + services.AddControllers(); } diff --git "a/\347\216\213\344\277\212/WebAPI10086/WebAPI10086.csproj" "b/\347\216\213\344\277\212/WebAPI10086/WebAPI10086.csproj" index 620192f42f7596ab4043d2b7ca4d97334a15426e..82f41b8aa18a9da6bb3d457576af50695849742a 100644 --- "a/\347\216\213\344\277\212/WebAPI10086/WebAPI10086.csproj" +++ "b/\347\216\213\344\277\212/WebAPI10086/WebAPI10086.csproj" @@ -7,6 +7,12 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + +