public void Patch(DISPLAY_ALARM source, IEnumerable<string> fields, IEnumerable<Guid> keys = null) { #region construct anonymous fields using expression lambdas var selection = fields as IList<string> ?? fields.ToList(); Expression<Func<DISPLAY_ALARM, object>> primitives = x => new { x.Action, x.Duration, x.Trigger, x.Repeat, x.Description }; //4. Get list of selected primitives var sprimitives = primitives.GetMemberNames().Intersect(selection); #endregion construct anonymous fields using expression lambdas manager.ExecTrans(transaction => { var daclient = redis.As<DISPLAY_ALARM>(); var okeys = daclient.GetAllKeys().ToArray(); if (!okeys.NullOrEmpty()) redis.Watch(okeys); #region update-only non-relational attributes if (!sprimitives.NullOrEmpty()) { Expression<Func<DISPLAY_ALARM, object>> actionexpr = x => x.Action; Expression<Func<DISPLAY_ALARM, object>> repeatexpr = x => x.Repeat; Expression<Func<DISPLAY_ALARM, object>> durationexpr = x => x.Duration; Expression<Func<DISPLAY_ALARM, object>> triggerexpr = x => x.Trigger; Expression<Func<DISPLAY_ALARM, object>> descexpr = x => x.Description; var lkeys = keys as IList<Guid> ?? keys.ToList(); var entities = !lkeys.NullOrEmpty() ? daclient.GetByIds(lkeys).ToList() : daclient.GetAll().ToList(); entities.ForEach(x => { if (selection.Contains(actionexpr.GetMemberName())) x.Action = source.Action; if (selection.Contains(repeatexpr.GetMemberName())) x.Repeat = source.Repeat; if (selection.Contains(durationexpr.GetMemberName())) x.Duration = source.Duration; if (selection.Contains(triggerexpr.GetMemberName())) x.Trigger = source.Trigger; if (selection.Contains(descexpr.GetMemberName())) x.Description = source.Description; }); transaction.QueueCommand(x => x.StoreAll(entities)); } #endregion update-only non-relational attributes }); }
/// <summary> /// Patches fields of an entity in the repository /// </summary> /// <param name="source">The source containing patch details</param> /// <param name="fields">Specfies which fields are used for the patching. The fields are specified in an anonymous variable</param> /// <param name="keys">Filters the entities to patch by keys. No filter implies all entities are patched</param> public void Patch(DISPLAY_ALARM source, IEnumerable<string> fields, IEnumerable<Guid> keys = null) { //1. Get fields slected for patching var selection = fields as IList<string> ?? fields.ToList(); //2.Get list of all non-related event details (primitives) Expression<Func<DISPLAY_ALARM, object>> primitives = x => new { x.Action, x.Trigger, x.Duration, x.Repeat, x.Description }; //3. Get list of selected primitives var sprimitives = primitives.GetMemberNames() .Intersect(selection, StringComparer.OrdinalIgnoreCase) .Distinct(StringComparer.OrdinalIgnoreCase); var okeys = (keys != null) ? db.SelectParam<DISPLAY_ALARM>(q => q.Id, p => Sql.In(p.Id, keys.ToArray())).ToArray() : db.SelectParam<DISPLAY_ALARM>(q => q.Id).ToArray(); if (!sprimitives.NullOrEmpty()) { //4. Update matching event primitives var patchstr = $"f => new {{ {string.Join(", ", sprimitives.Select(x => $"f.{x}"))} }}"; var patchexpr = patchstr.CompileToExpressionFunc<DISPLAY_ALARM, object>( CodeDomLanguage.csharp, "System.dll", "System.Core.dll", typeof(CalendarWriter).Assembly.Location, typeof (DISPLAY_ALARM).Assembly.Location, typeof (IContainsKey<Guid>).Assembly.Location); if (!okeys.NullOrEmpty()) db.UpdateOnly(source, patchexpr, q => Sql.In(q.Id, okeys.ToArray())); else db.UpdateOnly(source, patchexpr); } }
public void Save(DISPLAY_ALARM entity) { var keys = redis.As<DISPLAY_ALARM>().GetAllKeys().ToArray(); if (!keys.NullOrEmpty()) redis.Watch(keys); manager.ExecTrans(transaction => { if (entity != null) transaction.QueueCommand(x => x.Store(entity)); }); }
/// <summary> /// Inserts a new entity or updates an existing one in the repository /// </summary> /// <param name="entity">The entity to save</param> public void Save(DISPLAY_ALARM entity) { db.Save(entity); }