Created
January 6, 2020 13:51
-
-
Save merken/5071d014fd7a20bb794ee365566aaef3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Contract; | |
using Microsoft.EntityFrameworkCore; | |
using Prise.Plugin; | |
namespace SQLPlugin | |
{ | |
[Plugin(PluginType = typeof(IProductsRepository))] | |
public class SqlProductsRepository : IProductsRepository | |
{ | |
private readonly ProductsDbContext dbContext; | |
internal SqlProductsRepository(ProductsDbContext dbContext) | |
{ | |
this.dbContext = dbContext; | |
} | |
[PluginFactory] | |
public static SqlProductsRepository ThisIsTheFactoryMethod(IServiceProvider serviceProvider) | |
{ | |
var service = serviceProvider.GetService(typeof(ProductsDbContext)); | |
return new SqlProductsRepository(service as ProductsDbContext); | |
} | |
public async Task<IEnumerable<Product>> All() | |
{ | |
return await dbContext.Products | |
.AsNoTracking() | |
.ToListAsync(); | |
} | |
public Task<Product> Get(int productId) | |
{ | |
return this.dbContext.Products | |
.AsNoTracking() | |
.SingleOrDefaultAsync(p => p.Id == productId); | |
} | |
public async Task<Product> Create(Product product) | |
{ | |
await this.dbContext.Products.AddAsync(product); | |
return product; | |
} | |
public async Task<Product> Update(Product product) | |
{ | |
this.dbContext.Products.Attach(product); | |
await this.dbContext.SaveChangesAsync(); | |
return product; | |
} | |
public async Task Delete(int productId) | |
{ | |
var product = new Product { Id = productId }; | |
this.dbContext.Products.Attach(product); | |
this.dbContext.Products.Remove(product); | |
await this.dbContext.SaveChangesAsync(); | |
} | |
public async Task<IEnumerable<Product>> Search(string term) | |
{ | |
return await dbContext.Products | |
.AsNoTracking() | |
.Where(p => p.Description.Contains(term)) | |
.ToListAsync(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment