Skip to content

Commit

Permalink
Create RepositoyBase class and IReposoryBase Interface. Rename databa…
Browse files Browse the repository at this point in the history
…se context class and create its interface.
  • Loading branch information
clodoalves committed Jun 15, 2022
1 parent 36753fe commit 0e70393
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 159 deletions.
2 changes: 1 addition & 1 deletion WPFSample/WPFSample.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override async void OnStartup(StartupEventArgs e)
base.OnStartup(e);

//criacao de banco de dados
using (var db = new WPFSampleDb())
using (var db = new WPFSampleDbContext())
{
await db.Database.EnsureCreatedAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace WPFSample.Repository.Context
{
class FakeWPFSampleDbContext : IWPFSampleDbContext
public class FakeWPFSampleDbContext : IDbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductImage> ProductImages { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

namespace WPFSample.Repository.Context
{
public interface IWPFSampleDbContext
public interface IDbContext
{
DbSet<Product> Products { get;}
DbSet<ProductImage> ProductImages { get;}
DbSet<Product> Products { get; }
DbSet<ProductImage> ProductImages { get; }

int SaveChanges();
}
Expand Down
12 changes: 0 additions & 12 deletions WPFSample/WPFSample.Repository/Context/TesteGit.cs

This file was deleted.

12 changes: 0 additions & 12 deletions WPFSample/WPFSample.Repository/Context/TesteGit2.cs

This file was deleted.

17 changes: 0 additions & 17 deletions WPFSample/WPFSample.Repository/Context/WPFSampleDb.cs

This file was deleted.

4 changes: 2 additions & 2 deletions WPFSample/WPFSample.Repository/Context/WPFSampleDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace WPFSample.Repository.Context
{
public class WPFSampleDbContext : DbContext, IWPFSampleDbContext
public class WPFSampleDbContext : DbContext, IDbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductImage> ProductImages { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
{
optionsBuilder.UseSqlite(@"Data Source=D:\data.db");
}
}
Expand Down
18 changes: 10 additions & 8 deletions WPFSample/WPFSample.Repository/Contract/IRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFSample.Repository.Context;

namespace WPFSample.Repository.Contract
{
public interface IRepositoryBase <TEntity> where TEntity:class
public interface IRepositoryBase<T> where T : class
{
TEntity Add(TEntity obj);
TEntity GetById(int id);
IEnumerable<TEntity> GetAll();
void Update(TEntity obj);
void Remove(TEntity obj);
Task<IEnumerable<TEntity>> GetAllAsync();
void Add(T register);

void Delete(T register);

void Update(T register);

IEnumerable<T> GetAll();

T GetById(int id);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ProductImageRepository : IProductImageRepository
{
public ProductImage GetFirstImage(int idProduct)
{
using (var db = new WPFSampleDb())
using (var db = new WPFSampleDbContext())
{
return db.ProductImages.Where(p => p.ProductId == idProduct).FirstOrDefault();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ProductRepository : IProductRepository
{
public void AddProduct(Product product)
{
using (var db = new WPFSampleDb())
using (var db = new WPFSampleDbContext())
{
db.Products.Add(product);
db.SaveChanges();
Expand All @@ -21,7 +21,7 @@ public void AddProduct(Product product)

public void DeleteProduct(Product product)
{
using (var db = new WPFSampleDb())
using (var db = new WPFSampleDbContext())
{
db.Products.Remove(product);
db.SaveChanges();
Expand All @@ -30,23 +30,23 @@ public void DeleteProduct(Product product)

public IList<Product> GetAllProducts()
{
using (var db = new WPFSampleDb())
using (var db = new WPFSampleDbContext())
{
return db.Products.ToList();
}
}

public Product GetProductById(int id)
{
using (var db = new WPFSampleDb())
using (var db = new WPFSampleDbContext())
{
return db.Products.Where(x => x.Id == id).FirstOrDefault();
}
}

public void UpdateProduct(Product product)
{
using (var db = new WPFSampleDb())
using (var db = new WPFSampleDbContext())
{
db.Products.Update(product);

Expand Down
44 changes: 16 additions & 28 deletions WPFSample/WPFSample.Repository/Implementation/RepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -9,46 +8,35 @@

namespace WPFSample.Repository.Implementation
{
public class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : class
public class RepositoryBase<T> where T : class, IRepositoryBase<T>
{
public WPFSampleDbContext Db => new WPFSampleDbContext();

public TEntity Add(TEntity obj)
{
Db.Set<TEntity>().Add(obj);
Db.SaveChanges();

return obj;
}

public IEnumerable<TEntity> GetAll()
protected WPFSampleDbContext dbContext;
public void Add(T register)
{
return Db.Set<TEntity>().ToList();
dbContext.Add(register);
dbContext.SaveChanges();
}

public Task<IEnumerable<TEntity>> GetAllAsync()
public void Delete(T register)
{
return Task<IEnumerable<TEntity>>.Factory.StartNew(() =>
{
return Db.Set<TEntity>().AsParallel().ToList();
});
dbContext.Remove(register);
dbContext.SaveChanges();
}

public TEntity GetById(int id)
public IEnumerable<T> GetAll()
{
return Db.Set<TEntity>().Find(id);
return dbContext.Set<T>().ToList();
}

public void Remove(TEntity obj)
public T GetById(int id)
{
Db.Set<TEntity>().Remove(obj);
Db.SaveChanges();
return dbContext.Set<T>().Find(id);
}

public void Update(TEntity obj)
public void Update(T register)
{
Db.Entry(obj).State = EntityState.Modified;
Db.SaveChanges();
dbContext.Update(register);
dbContext.SaveChanges();
}
}
}
6 changes: 5 additions & 1 deletion WPFSample/WPFSample.Repository/WPFSample.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,15 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Context\WPFSampleDb.cs" />
<Compile Include="Context\FakeWPFSampleDbContext.cs" />
<Compile Include="Context\IDbContext.cs" />
<Compile Include="Context\WPFSampleDbContext.cs" />
<Compile Include="Contract\IProductRepository.cs" />
<Compile Include="Contract\IProductImageRepository.cs" />
<Compile Include="Contract\IRepositoryBase.cs" />
<Compile Include="Implementation\ProductImageRepository.cs" />
<Compile Include="Implementation\ProductRepository.cs" />
<Compile Include="Implementation\RepositoryBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
12 changes: 0 additions & 12 deletions WPFSample/WPFSample.Test/Class1.cs

This file was deleted.

1 change: 0 additions & 1 deletion WPFSample/WPFSample.Test/WPFSample.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Service\ProductServiceTest.cs" />
</ItemGroup>
Expand Down

0 comments on commit 0e70393

Please sign in to comment.