Skip to content

Commit

Permalink
Configuration of WPFSampleDbContext Singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
clodoalves committed Jun 29, 2022
1 parent 5e45427 commit e823e39
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
6 changes: 1 addition & 5 deletions WPFSample/WPFSample.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

//criacao de banco de dados
using (var db = new WPFSampleDbContext())
{
await db.Database.EnsureCreatedAsync();
}
await WPFSampleDbContext.GetInstance().Database.EnsureCreatedAsync();

new Bootstrapper().Run();
}
Expand Down
15 changes: 15 additions & 0 deletions WPFSample/WPFSample.Repository/Context/WPFSampleDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ namespace WPFSample.Repository.Context
{
public class WPFSampleDbContext : DbContext, IDbContext
{
private static WPFSampleDbContext _instance;

private WPFSampleDbContext()
{

}

public DbSet<Product> Products { get; set; }
public DbSet<ProductImage> ProductImages { get; set; }

Expand All @@ -19,5 +26,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new ProductMap());
modelBuilder.ApplyConfiguration(new ProductImageMap());
}

public static WPFSampleDbContext GetInstance()
{
if (_instance == null)
_instance = new WPFSampleDbContext();

return _instance;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

namespace WPFSample.Repository.Implementation
{
public class ProductImageRepository: RepositoryBase<ProductImage>, IProductImageRepository
public class ProductImageRepository : RepositoryBase<ProductImage>, IProductImageRepository
{
public ProductImage GetFirstImage(int idProduct)
{
using (var db = new WPFSampleDbContext())
{
return db.ProductImages.Where(p => p.ProductId == idProduct).FirstOrDefault();
}
//using (var db = new WPFSampleDbContext())
//{
return dbContext.ProductImages.Where(p => p.ProductId == idProduct).FirstOrDefault();
//}
}
}
}
12 changes: 6 additions & 6 deletions WPFSample/WPFSample.Repository/Implementation/RepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace WPFSample.Repository.Implementation
{
public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
{
protected WPFSampleDbContext dbContext = new WPFSampleDbContext();
protected WPFSampleDbContext dbContext = WPFSampleDbContext.GetInstance();
public void Add(T register)
{
dbContext.Add(register);
dbContext.Add(register);
dbContext.SaveChanges();
}

Expand All @@ -22,11 +22,11 @@ public void Delete(T register)

public IEnumerable<T> GetAll()
{
//TODO: To find a way to refresh the global dbContext correctly
using (var dbContext = new WPFSampleDbContext())
{
//TODO: To find a way to refresh the class dbContext correctly
//using (var dbContext = new WPFSampleDbContext())
//{
return dbContext.Set<T>().ToList();
}
//}
}

public T GetById(int id)
Expand Down

0 comments on commit e823e39

Please sign in to comment.