public void created_new_entity() { //GIVEN var key = Guid.NewGuid(); //WHEN Assert.AreEqual(1, _writer.AddOrUpdate(key, () => 1, i => 5, AddOrUpdateHint.ProbablyDoesNotExist)); }
/// <summary> /// Saves new entity, using the provided <param name="key"></param> and throws /// <exception cref="InvalidOperationException"></exception> if the entity actually already exists /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="writer">The self.</param> /// <param name="key">The key.</param> /// <param name="newEntity">The new entity.</param> /// <returns></returns> public static TEntity Add <TKey, TEntity>(this IDocumentWriter <TKey, TEntity> writer, TKey key, TEntity newEntity) { return(writer.AddOrUpdate(key, newEntity, e => { throw new InvalidOperationException(string.Format("Entity '{0}' with key '{1}' should not exist.", typeof(TEntity).Name, key)); }, AddOrUpdateHint.ProbablyDoesNotExist)); }
/// <summary> /// Updates already existing entity, throwing exception, if it does not already exist. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="self">The self.</param> /// <param name="key">The key.</param> /// <param name="change">The change.</param> /// <returns></returns> public static TEntity UpdateOrThrow <TKey, TEntity>(this IDocumentWriter <TKey, TEntity> self, TKey key, Action <TEntity> change) { return(self.AddOrUpdate(key, () => { var txt = String.Format("Failed to load '{0}' with key '{1}'.", typeof(TEntity).Name, key); throw new InvalidOperationException(txt); }, change, AddOrUpdateHint.ProbablyExists)); }
/// <summary> /// Given a <paramref name="key"/> either adds a new <typeparamref name="TEntity"/> OR updates an existing one. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="self">The self.</param> /// <param name="key">The key.</param> /// <param name="addFactory">The add factory (used to create a new entity, if it is not found).</param> /// <param name="update">The update method (called to update an existing entity, if it exists).</param> /// <param name="hint">The hint.</param> /// <returns></returns> public static TEntity AddOrUpdate <TKey, TEntity>(this IDocumentWriter <TKey, TEntity> self, TKey key, Func <TEntity> addFactory, Action <TEntity> update, AddOrUpdateHint hint = AddOrUpdateHint.ProbablyExists) { return(self.AddOrUpdate(key, addFactory, entity => { update(entity); return entity; }, hint)); }
/// <summary> /// Given a <paramref name="key"/> either adds a new <typeparamref name="TEntity"/> OR updates an existing one. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="self">The self.</param> /// <param name="key">The key.</param> /// <param name="newView">The new view that will be saved, if entity does not already exist</param> /// <param name="updateViewFactory">The update method (called to update an existing entity, if it exists).</param> /// <param name="hint">The hint.</param> /// <returns></returns> public static TEntity AddOrUpdate <TKey, TEntity>(this IDocumentWriter <TKey, TEntity> self, TKey key, TEntity newView, Action <TEntity> updateViewFactory, AddOrUpdateHint hint = AddOrUpdateHint.ProbablyExists) { return(self.AddOrUpdate(key, () => newView, view => { updateViewFactory(view); return view; }, hint)); }
/// <summary> /// Saves new entity, using the provided <param name="key"></param> and throws /// <exception cref="InvalidOperationException"></exception> if the entity actually already exists /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="self">The self.</param> /// <param name="key">The key.</param> /// <param name="newEntity">The new entity.</param> /// <returns></returns> public static TEntity Add <TKey, TEntity>(this IDocumentWriter <TKey, TEntity> self, TKey key, TEntity newEntity) { return(self.AddOrUpdate(key, () => newEntity, e => { _logger.Error("Did not expect to encounter entity '{0}' with key '{1}'. Overwriting.", typeof(TEntity).Name, key); return newEntity; }, AddOrUpdateHint.ProbablyDoesNotExist)); }
/// <summary> /// Given a <paramref name="key"/> either adds a new <typeparamref name="TEntity"/> OR updates an existing one. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="writer">The self.</param> /// <param name="key">The key.</param> /// <param name="entity">The new view that will be saved, if entity does not already exist</param> /// <param name="updateFactory">The update method (called to update an existing entity, if it exists).</param> /// <param name="hint">The hint.</param> /// <returns></returns> public static TEntity AddOrUpdate <TKey, TEntity>(this IDocumentWriter <TKey, TEntity> writer, TKey key, TEntity entity, Action <TEntity> updateFactory, AddOrUpdateHint hint = AddOrUpdateHint.ProbablyExists) { return(writer.AddOrUpdate(key, () => entity, e => { updateFactory(e); return e; }, hint)); }
protected static void HandleAtomic(AtomicMessage msg, SimpleMessageSender sender, IDocumentWriter<unit, int> arg3) { var count = arg3.AddOrUpdate(unit.it, () => 1, i => i + 1); if (count > 2) { sender.SendBatch(new object[] {}, e => e.AddString("ok")); return; } sender.SendOne(new AtomicMessage()); }
protected static void HandleAtomic(AtomicMessage msg, SimpleMessageSender sender, IDocumentWriter <unit, int> arg3) { var count = arg3.AddOrUpdate(unit.it, () => 1, i => i + 1); if (count > 2) { sender.SendBatch(new object[] {}, e => e.AddString("ok")); return; } sender.SendOne(new AtomicMessage()); }
public void When(GoalSet e) { Func <UserGoalsView> CreateView; CreateView = () => { var view = new UserGoalsView(); view.Goals.Add(e.Id); view.User = e.User; return(view); }; _writer.AddOrUpdate(e.User, CreateView(), g => g.Goals.Add(e.Id)); }
/// <summary> /// Updates an entity, creating a new instance before that, if needed. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TView">The type of the view.</typeparam> /// <param name="self">The self.</param> /// <param name="key">The key.</param> /// <param name="update">The update.</param> /// <param name="hint">The hint.</param> /// <returns></returns> public static TView UpdateEnforcingNew <TKey, TView>(this IDocumentWriter <TKey, TView> self, TKey key, Action <TView> update, AddOrUpdateHint hint = AddOrUpdateHint.ProbablyExists) where TView : new() { return(self.AddOrUpdate(key, () => { var view = new TView(); update(view); return view; }, v => { update(v); return v; }, hint)); }
public void when_not_found_key_get_new_view_and_not_call_action() { Test1 t = new Test1 { Value = 555 }; var key = Guid.NewGuid(); var newValue = _guidKeyClassWriter.AddOrUpdate(key, t, tv => { tv.Value += 1; }); Assert.AreEqual(t, newValue); Assert.AreEqual(555, newValue.Value); }
public AgentsStatusesProjection(IDocumentWriter<unit, AgentsStatus> writer) { this.writer = writer; writer.AddOrUpdate(unit.it, () => new AgentsStatus(), status => { }); }