| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Data.Entity.Validation;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using GreenTree.Nachtragsmanagement.Core;
- using GreenTree.Nachtragsmanagement.Core.Data;
- namespace GreenTree.Nachtragsmanagement.Data
- {
- public class EfRepository<T> : IRepository<T> where T : BaseEntity
- {
- #region Fields
- private readonly IDbContext _context;
- private IDbSet<T> _entities;
- #endregion
- #region Ctor
- /// <summary>
- /// Ctor
- /// </summary>
- /// <param name="context">Object context</param>
- public EfRepository(IDbContext context)
- {
- _context = context;
- }
- #endregion
- #region Methods
- /// <summary>
- /// Get entity by identifier
- /// </summary>
- /// <param name="id">Identifier</param>
- /// <returns>Entity</returns>
- public virtual T GetById(object id)
- {
- //see some suggested performance optimization (not tested)
- //http://stackoverflow.com/questions/11686225/dbset-find-method-ridiculously-slow-compared-to-singleordefault-on-id/11688189#comment34876113_11688189
- return Entities.Find(id);
- }
- /// <summary>
- /// Insert entity
- /// </summary>
- /// <param name="entity">Entity</param>
- public virtual void Insert(T entity)
- {
- try
- {
- if (entity == null)
- throw new ArgumentNullException("entity");
- Entities.Add(entity);
- _context.SaveChanges();
- }
- catch (DbEntityValidationException dbEx)
- {
- var msg = string.Empty;
- foreach (var validationErrors in dbEx.EntityValidationErrors)
- foreach (var validationError in validationErrors.ValidationErrors)
- msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
- var fail = new Exception(msg, dbEx);
- //Debug.WriteLine(fail.Message, fail);
- throw fail;
- }
- }
- /// <summary>
- /// Insert entities
- /// </summary>
- /// <param name="entities">Entities</param>
- public virtual void Insert(IEnumerable<T> entities)
- {
- try
- {
- if (entities == null)
- throw new ArgumentNullException("entities");
- foreach (var entity in entities)
- Entities.Add(entity);
- _context.SaveChanges();
- }
- catch (DbEntityValidationException dbEx)
- {
- var msg = string.Empty;
- foreach (var validationErrors in dbEx.EntityValidationErrors)
- foreach (var validationError in validationErrors.ValidationErrors)
- msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
- var fail = new Exception(msg, dbEx);
- //Debug.WriteLine(fail.Message, fail);
- throw fail;
- }
- }
- /// <summary>
- /// Update entity
- /// </summary>
- /// <param name="entity">Entity</param>
- public virtual void Update(T entity)
- {
- try
- {
- if (entity == null)
- throw new ArgumentNullException("entity");
- _context.SaveChanges();
- }
- catch (DbEntityValidationException dbEx)
- {
- var msg = string.Empty;
- foreach (var validationErrors in dbEx.EntityValidationErrors)
- foreach (var validationError in validationErrors.ValidationErrors)
- msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
- var fail = new Exception(msg, dbEx);
- //Debug.WriteLine(fail.Message, fail);
- throw fail;
- }
- }
- /// <summary>
- /// Delete entity
- /// </summary>
- /// <param name="entity">Entity</param>
- public virtual void Delete(T entity)
- {
- try
- {
- if (entity == null)
- throw new ArgumentNullException("entity");
- Entities.Remove(entity);
- _context.SaveChanges();
- }
- catch (DbEntityValidationException dbEx)
- {
- var msg = string.Empty;
- foreach (var validationErrors in dbEx.EntityValidationErrors)
- foreach (var validationError in validationErrors.ValidationErrors)
- msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
- var fail = new Exception(msg, dbEx);
- //Debug.WriteLine(fail.Message, fail);
- throw fail;
- }
- }
- /// <summary>
- /// Delete entities
- /// </summary>
- /// <param name="entities">Entities</param>
- public virtual void Delete(IEnumerable<T> entities)
- {
- try
- {
- if (entities == null)
- throw new ArgumentNullException("entities");
- foreach (var entity in entities)
- Entities.Remove(entity);
- _context.SaveChanges();
- }
- catch (DbEntityValidationException dbEx)
- {
- var msg = string.Empty;
- foreach (var validationErrors in dbEx.EntityValidationErrors)
- foreach (var validationError in validationErrors.ValidationErrors)
- msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
- var fail = new Exception(msg, dbEx);
- //Debug.WriteLine(fail.Message, fail);
- throw fail;
- }
- }
- #endregion
- #region Properties
- /// <summary>
- /// Gets a table
- /// </summary>
- public virtual IQueryable<T> Table
- {
- get
- {
- return Entities;
- }
- }
- /// <summary>
- /// Gets a table with "no tracking" enabled (EF feature) Use it only when you load record(s) only for read-only operations
- /// </summary>
- public virtual IQueryable<T> TableNoTracking
- {
- get
- {
- return Entities.AsNoTracking();
- }
- }
- /// <summary>
- /// Entities
- /// </summary>
- protected virtual IDbSet<T> Entities
- {
- get
- {
- if (_entities == null)
- _entities = _context.Get<T>();
- return _entities;
- }
- }
- #endregion
- }
- }
|