-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement repositories; add tests to models and repositories
- Loading branch information
Showing
107 changed files
with
2,804 additions
and
573 deletions.
There are no files selected for viewing
Binary file modified
BIN
+69.1 KB
(130%)
.vs/ProjectEvaluation/storemanagementsystemx.metadata.v7.bin
Binary file not shown.
Binary file modified
BIN
+4.46 MB
(300%)
.vs/ProjectEvaluation/storemanagementsystemx.projects.v7.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
.vs/StoreManagementSystemX/v17/TestStore/0/testlog.manifest
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using StoreManagementSystemX.Database.DAL.Interfaces; | ||
using StoreManagementSystemX.Database.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace StoreManagementSystemX.Database.DAL | ||
{ | ||
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class | ||
{ | ||
public BaseRepository(Context context) | ||
{ | ||
_context = context; | ||
_dbSet = context.Set<TEntity>(); | ||
} | ||
|
||
protected readonly Context _context; | ||
protected readonly DbSet<TEntity> _dbSet; | ||
|
||
public virtual void Delete(Guid instanceId) | ||
{ | ||
var entityToDelete = _dbSet.Find(instanceId); | ||
if (entityToDelete != null) | ||
{ | ||
Delete(entityToDelete); | ||
} | ||
|
||
} | ||
|
||
public virtual void Delete(TEntity entityToDelete) | ||
{ | ||
if (_context.Entry(entityToDelete).State == EntityState.Detached) | ||
{ | ||
_dbSet.Attach(entityToDelete); | ||
} | ||
|
||
_dbSet.Remove(entityToDelete); | ||
} | ||
|
||
|
||
|
||
public TEntity? GetById(Guid instanceId) | ||
{ | ||
|
||
return _dbSet.Find(instanceId); | ||
} | ||
|
||
public void Insert(TEntity newInstance) | ||
{ | ||
_dbSet.Add(newInstance); | ||
} | ||
|
||
public virtual void Update(TEntity entityToUpdate) | ||
{ | ||
_dbSet.Attach(entityToUpdate); | ||
_context.Entry(entityToUpdate).State = EntityState.Modified; | ||
} | ||
|
||
public void Attach(TEntity entity) | ||
{ | ||
_dbSet.Attach(entity); | ||
} | ||
|
||
public void Save() | ||
{ | ||
_context.SaveChanges(); | ||
} | ||
|
||
public IEnumerable<TEntity> Get( | ||
Expression<Func<TEntity, bool>>? filter = null, | ||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, | ||
string includeProperties = "") | ||
{ | ||
IQueryable<TEntity> query = _dbSet; | ||
|
||
if(filter != null) | ||
{ | ||
query = query.Where(filter); | ||
} | ||
|
||
foreach(var includeProperty in includeProperties.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) | ||
{ | ||
query = query.Include(includeProperty); | ||
} | ||
|
||
if (orderBy != null) | ||
{ | ||
return orderBy(query).ToList(); | ||
} else | ||
{ | ||
return query.ToList(); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
16 changes: 11 additions & 5 deletions
16
StoreManagementSystemX.Database/DAL/Interfaces/IRepository.cs
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.