/// <summary> /// Commits the changes made on the context to the database. /// </summary> /// <returns> /// The number of objects written to the database /// </returns> public int Commit() { var modifiedEntities = DbContext.ChangeTracker.Entries() .Where(el => el.State == EntityState.Modified).ToList(); var deletedEntities = DbContext.ChangeTracker.Entries().Where(el => el.State == EntityState.Deleted).ToList(); var writtenObjectCount = DbContext.SaveChanges(); foreach (var e in modifiedEntities.OfType <DbEntity>().Where(el => el.BusinessEntity != null)) { e.BusinessEntity.Commit(); EventProxy.NotifyUpdated(e.BusinessEntity); } foreach (var e in deletedEntities.OfType <DbEntity>().Where(el => el.BusinessEntity != null)) { e.BusinessEntity.Commit(); EventProxy.NotifyDeleted(e.BusinessEntity); } var saveCounter = 0; while (DbContext.ChangeTracker.Entries().Any(el => el.State != EntityState.Unchanged)) { if (saveCounter++ > 1000) { throw new RepositoryException("Repository tried saving more than set limit and there are still changes. Check for infinite recursion events or increase the limit."); } writtenObjectCount += Commit(); } return(writtenObjectCount); }
/// <summary> /// Updates a business entity to the repository. /// </summary> /// <typeparam name="TEntity">The entity type to update.</typeparam> /// <typeparam name="TBusinessEntity">The business entity type that's updating.</typeparam> /// <param name="businessEntity">The business entity that's updating.</param> /// <exception cref="RepositoryException">Fires this exception when the Entity is not found in the context.</exception> public void Update <TEntity, TBusinessEntity>(TBusinessEntity businessEntity) where TEntity : DbEntity, new() where TBusinessEntity : BusinessEntity { IEnumerable <object> keys; var originalEntity = GetEntityFromBusinessEntity <TEntity, TBusinessEntity>(businessEntity, out keys); if (originalEntity == null) { var exception = new RepositoryException(Resources.Repository_Update_UpdateFailed_NotFound); exception.Data.Add("Entity Keys", keys); throw exception; } if (!EventProxy.NotifyUpdate(businessEntity)) { return; } var dbEntity = originalEntity as BaseDbEntity; if (dbEntity != null) { dbEntity.UpdateDate = DateTime.UtcNow.ToUniversalTime(); dbEntity.UpdaterUsername = UserName; } originalEntity.InjectFrom(businessEntity); originalEntity.WriteJsonData(businessEntity); Cryptography.EncryptProperties <TBusinessEntity>(originalEntity); }
/// <summary> /// Adds a new business entity to the repository. /// </summary> /// <typeparam name="TEntity">The type of the entity to add.</typeparam> /// <typeparam name="TBusinessEntity">The Type of the business entity to add.</typeparam> /// <param name="businessEntity">The business entity to add to the repository.</param> public void Add <TEntity, TBusinessEntity>(TBusinessEntity businessEntity) where TEntity : DbEntity, new() where TBusinessEntity : BusinessEntity { var newEntity = new TEntity(); newEntity.InjectFrom(businessEntity); newEntity.WriteJsonData(businessEntity); Cryptography.EncryptProperties <TBusinessEntity>(newEntity); var baseDbEntity = newEntity as BaseDbEntity; if (baseDbEntity != null) { baseDbEntity.Guid = Guid.NewGuid(); baseDbEntity.CreationDate = DateTime.UtcNow.ToUniversalTime(); baseDbEntity.CreatorUsername = UserName; baseDbEntity.UpdateDate = baseDbEntity.CreationDate; baseDbEntity.UpdaterUsername = UserName; baseDbEntity.IsDeleted = false; } EventProxy.NotifyAdd(businessEntity); ImmediateDbContext.Set <TEntity>().Add(newEntity); ImmediateDbContext.SaveChanges(); ImmediateDbContext.Entry(newEntity).State = EntityState.Detached; DbContext.Set <TEntity>().Attach(newEntity); //Get updated keys businessEntity.InjectFrom(newEntity); businessEntity.Init(); EventProxy.NotifyAdded(businessEntity); }
public void Delete <TEntity, TBusinessEntity>(TBusinessEntity businessEntity) where TEntity : DbEntity, new() where TBusinessEntity : BusinessEntity { IEnumerable <object> keys; var entity = GetEntityFromBusinessEntity <TEntity, TBusinessEntity>(businessEntity, out keys); if (!EventProxy.NotifyDelete(businessEntity)) { return; } DbContext.Set <TEntity>().Remove(entity); }
/// <summary> /// Registers an event proxy on a Type. /// </summary> /// <param name="type">The type to register the event proxy to.</param> /// <param name="proxy">The proxy event to register.</param> public static void Register(Type type, EventProxy proxy) { Proxies[type] = proxy; }