/// <summary> /// Purge object state cache and detach all objects added/updated/deleted from the system in current context.SaveChanges() call /// </summary> /// <param name="apiContext"></param> /// <param name="entity"></param> protected static void DetachObject(Helpers.ApiContext apiContext, Entities.EntityObject entity) { Model.OrmsContext context = (Model.OrmsContext)apiContext.CurrentContext; try { context.Detach(entity); } catch (System.InvalidOperationException) { } // NOTE : we do intentionally suppressing this exception }
/// <summary> /// SetDeleted for SoftDelete /// </summary> /// <param name="apiContext"></param> /// <param name="entity"></param> protected static void SetDeleted(Helpers.ApiContext apiContext, Entities.EntityObject entity) { Model.OrmsContext context = (Model.OrmsContext)apiContext.CurrentContext; // SET IsDeleted = true entity.GetType().GetProperty("IsDeleted").SetValue(entity, true, null); // SET IsDeleted modified System.Data.Objects.ObjectStateEntry ose = context.ObjectStateManager.GetObjectStateEntry(entity); ose.SetModifiedProperty("IsDeleted"); }
/// <summary> /// Set System Properties (CreadBy, CreatedOn, ModifiedBy, ModifiedOn, and CustomerId) /// </summary> /// <param name="apiContext"></param> /// <param name="entity"></param> /// <param name="ignoreCreated"></param> protected static void SetSystemProperties(Helpers.ApiContext apiContext, Entities.EntityObject entity, bool ignoreCreated) { // SET system level properties /* if (!ignoreCreated) { * entity.GetType().GetProperty("CreatedBy").SetValue(entity, apiContext.User.Id, null); * entity.GetType().GetProperty("CreatedOn").SetValue(entity, System.DateTime.UtcNow, null); * } * * * entity.GetType().GetProperty("ModifiedBy").SetValue(entity, apiContext.User.Id, null); * entity.GetType().GetProperty("ModifiedOn").SetValue(entity, System.DateTime.UtcNow, null); * entity.GetType().GetProperty("CustomerId").SetValue(entity, apiContext.Customer.Id, null); * */ }
protected static int SoftDeleteObjectMappings(Helpers.ApiContext apiContext, Entities.EntityObject entity) { Model.OrmsContext context = (Model.OrmsContext)apiContext.CurrentContext; System.Guid id = (System.Guid)entity.GetType().GetProperty("Id").GetValue(entity, null); System.Guid modifiedBy = (System.Guid)entity.GetType().GetProperty("ModifiedBy").GetValue(entity, null); System.DateTime modifiedOn = (System.DateTime)entity.GetType().GetProperty("ModifiedOn").GetValue(entity, null); string sqlQuery = string.Format(@"UPDATE [ObjectMappings] SET [IsDeleted] = 1, [ModifiedBy] = '{0}', [ModifiedOn] = '{1}' WHERE [ParentId] = @p0", modifiedBy, modifiedOn); int recordCount = apiContext.CurrentContext.ExecuteStoreCommand(sqlQuery, id); return(recordCount); }
private static void Translate <TEntity>(System.Data.Common.DbDataReader reader, out Collections.List <TEntity> entities) where TEntity : class { // Initilize list entities = new Collections.List <TEntity>(); System.Type type = typeof(TEntity); System.Reflection.MethodInfo methodInfo = type.GetMethod("Factory", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); while (reader.Read()) { // Create Object TEntity entity = (TEntity)methodInfo.Invoke(null, new object[0]); foreach (System.Reflection.PropertyInfo property in type.GetProperties()) { if (IsComplexType(property)) // Complex Type { Entities.EntityObject complexEntity = null; if (TryGetValue(property, reader, out complexEntity)) { property.SetValue(entity, complexEntity, null); } } else if (IsPrimitiveType(property)) // Primitive Type { object value = null; if (TryGetValue(property.Name, reader, out value)) { property.SetValue(entity, value, null); } } } entities.Add(entity); } }
/// <summary> /// SetModified for Update /// </summary> /// <param name="apiContext"></param> /// <param name="entity"></param> protected static void SetModified(Helpers.ApiContext apiContext, Entities.EntityObject entity) { Model.OrmsContext context = (Model.OrmsContext)apiContext.CurrentContext; System.Data.Objects.ObjectStateEntry ose = context.ObjectStateManager.GetObjectStateEntry(entity); System.Data.Metadata.Edm.ItemCollection colleciton = null; if (context.MetadataWorkspace.TryGetItemCollection(System.Data.Metadata.Edm.DataSpace.CSpace, out colleciton)) { System.String typeFullName = entity.GetType().ToString(); Edm.EntityType entiyType = colleciton.GetItems <Edm.EntityType>() .Where(e => e.FullName.Equals(typeFullName, System.StringComparison.InvariantCultureIgnoreCase)) .First(); // SET all properties modified except those who are in ignore list foreach (Edm.EdmProperty property in entiyType.Properties) { if (!IsInIgnoreList(property.Name)) { ose.SetModifiedProperty(property.Name); } } } }
protected static void SoftDelete(Helpers.ApiContext apiContext, System.String entitySet, Entities.EntityObject entity) { SoftDelete(apiContext, entitySet, new System.Collections.Generic.List <Entities.EntityObject> { entity }); }
/// <summary> /// ApplyOriginalValues for Concurrency check in Update and Delete /// </summary> /// <param name="apiContext"></param> /// <param name="entity"></param> protected static void ApplyOriginalValues(Helpers.ApiContext apiContext, Entities.EntityObject entity) { Model.OrmsContext context = (Model.OrmsContext)apiContext.CurrentContext; System.Data.Objects.ObjectStateEntry ose = context.ObjectStateManager.GetObjectStateEntry(entity); ose.ApplyOriginalValues(entity); }
private static bool TryGetValue(System.Reflection.PropertyInfo propertyInfo, System.Data.Common.DbDataReader reader, out Entities.EntityObject complexEntity) { complexEntity = null; System.String prefix = propertyInfo.Name + "_"; System.Type type = propertyInfo.PropertyType; System.Reflection.MethodInfo methodInfo = propertyInfo.PropertyType.GetMethod("Factory", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); foreach (System.Reflection.PropertyInfo property in type.GetProperties()) { if (IsPrimitiveType(property)) { object value = null; if (TryGetValue(prefix + property.Name, reader, out value)) { if (complexEntity == null) { complexEntity = (Entities.EntityObject)methodInfo.Invoke(null, new object[0]); } property.SetValue(complexEntity, value, null); } } } return(complexEntity != null); }
/// <summary> /// Set CustomerId, ModifiedBy and ModifiedOn properties modified for update/delete /// </summary> /// <param name="apiContext"></param> /// <param name="entity"></param> protected static void SetSystemPropertiesModified(Helpers.ApiContext apiContext, Entities.EntityObject entity) { Model.OrmsContext context = (Model.OrmsContext)apiContext.CurrentContext; System.Data.Objects.ObjectStateEntry ose = context.ObjectStateManager.GetObjectStateEntry(entity); ose.SetModifiedProperty("ModifiedBy"); ose.SetModifiedProperty("ModifiedOn"); ose.SetModifiedProperty("CustomerId"); }
/// <summary> /// Set System Properties (CreadBy, CreatedOn, ModifiedBy, ModifiedOn, and CustomerId) /// </summary> /// <param name="apiContext"></param> /// <param name="entity"></param> protected static void SetSystemProperties(Helpers.ApiContext apiContext, Entities.EntityObject entity) { SetSystemProperties(apiContext, entity, true); }
/// <summary> /// Attach Object /// </summary> /// <param name="apiContext"></param> /// <param name="entitySet"></param> /// <param name="entity"></param> protected static void AttachObject(Helpers.ApiContext apiContext, System.String entitySet, Entities.EntityObject entity) { Model.OrmsContext context = (Model.OrmsContext)apiContext.CurrentContext; // ATTACH to context context.AttachTo(entitySet, entity); // APPLY original values ApplyOriginalValues(apiContext, entity); }