public static TEntity AddOrUpdate <TKey, TEntity>(this IProjectionWriter <TKey, TEntity> writer, TKey key, Func <TEntity> addFactory, Action <TEntity> update)
 {
     return(writer.AddOrUpdate(key, addFactory, entity =>
     {
         update(entity);
         return entity;
     }));
 }
 public static TEntity UpdateOrThrow <TKey, TEntity>(this IProjectionWriter <TKey, TEntity> writer, TKey key, Action <TEntity> change)
 {
     return(writer.AddOrUpdate(key, () =>
     {
         var txt = $"Failed to load '{typeof(TEntity).Name}' with key '{key}'.";
         throw new InvalidOperationException(txt);
     }, change));
 }
 public static TEntity Add <TKey, TEntity>(this IProjectionWriter <TKey, TEntity> writer, TKey key, TEntity newEntity)
 {
     return(writer.AddOrUpdate(key, newEntity, e =>
     {
         var txt = $"Entity '{typeof(TEntity).Name}' with key '{key}' should not exist.";
         throw new InvalidOperationException(txt);
     }));
 }
 public static TEntity AddOrUpdate <TKey, TEntity>(this IProjectionWriter <TKey, TEntity> writer, TKey key, TEntity newView, Action <TEntity> updateViewFactory)
 {
     return(writer.AddOrUpdate(key, () => newView, view =>
     {
         updateViewFactory(view);
         return view;
     }));
 }
 public static TView UpdateEnforcingNew <TKey, TView>(this IProjectionWriter <TKey, TView> writer, TKey key,
                                                      Action <TView> update)
     where TView : new()
 {
     return(writer.AddOrUpdate(key, () =>
     {
         var view = new TView();
         update(view);
         return view;
     }, v =>
     {
         update(v);
         return v;
     }));
 }
        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; });

            t.Should().Be(newValue);

            555.Should().Be(newValue.Value);
        }
 public static async Task Add <TId, TView>(this IProjectionWriter <TId, TView> writer, TId key, TView item) where TView : class
 {
     await writer.AddOrUpdate(key, () => item, null, false);
 }
 public static async Task UpdateEnforcingNew <TId, TView>(this IProjectionWriter <TId, TView> writer, TId key, Action <TView> update)
     where TView : class, new()
 {
     await writer.AddOrUpdate(key, () => new TView(), x => { update(x); return(x); });
 }
 public static async Task Update <TId, TView>(this IProjectionWriter <TId, TView> writer, TId key, Func <TView, TView> update) where TView : class
 {
     await writer.AddOrUpdate(key, null, update);
 }
 public static async Task Update <TId, TView>(this IProjectionWriter <TId, TView> writer, TId key, Action <TView> update) where TView : class
 {
     await writer.AddOrUpdate(key, null, x => { update(x); return(x); });
 }
 public static async Task Add <TId, TView>(this IProjectionWriter <TId, TView> writer, TId key, Func <TView> addFactory) where TView : class
 {
     await writer.AddOrUpdate(key, addFactory, null, false);
 }