Commit 7819603e by dingsongjie

完成 EFCore grains 全部功能

parent b615a32a
......@@ -40,5 +40,12 @@ namespace Backet.Api.Controllers
var grain = clusterClient.GetGrain<IBacketGrain>(id);
return grain.AddBacketItem("55","测试3",1000);
}
[HttpPost("api/backet/RemoveFirstItem")]
public Task<bool> RemoveFirstItem()
{
var id = "da8a489fa7b4409294ee1b358fbbfba5";
var grain = clusterClient.GetGrain<IBacketGrain>(id);
return grain.RemoveFirstItem();
}
}
}
\ No newline at end of file
......@@ -28,11 +28,21 @@ namespace Backet.Api.Domain.AggregatesModel.BacketAggregate
{
foreach (var item in BacketItems)
{
TotalPrice += item.Price;
TotalPrice = BacketItems.Sum(m=>m.Price);
}
}
public string UserId { get; set; }
public List<BacketItem> BacketItems { get; private set; } = new List<BacketItem>();
public long TotalPrice { get; private set; }
internal void RemoveFirstItem()
{
var first = BacketItems.FirstOrDefault();
if (first != null)
{
BacketItems.Remove(first);
SetBacketTotalPrice();
}
}
}
}
......@@ -11,6 +11,7 @@ namespace Backet.Api.Grains.Abstraction
Task<bool> AddBacket(BacketDto backet);
Task<bool> UpdateBacket(string userId);
Task<bool> AddBacketItem(string productId, string productName, long price);
Task<bool> RemoveFirstItem();
}
public class BacketItemDto
{
......
......@@ -37,6 +37,14 @@ namespace Backet.Api.Grains
return true;
}
public async Task<bool> RemoveFirstItem()
{
State.RemoveFirstItem();
Update(State);
await WriteStateAsync();
return true;
}
public async Task<bool> UpdateBacket(string userId)
{
if (State == null) return false;
......
......@@ -29,14 +29,16 @@ namespace Backet.Api
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<BacketDbContext>(options =>options.UseNpgsql(Configuration["postgres:write"]));
services.AddDbContextPool<BacketDbContext>(options => options.UseNpgsql(Configuration["postgres:write"]));
services.AddControllers();
services.ConfigureGrainStorageOptions<BacketDbContext, BacketGrain, Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet>(
options => options
.UseQuery(context => context.Backets
.Include(box => box.BacketItems)
));
options =>
{
options.UseQuery(context => context.Backets
.Include(box => box.BacketItems));
options.IsRelatedData = true;
});
......
......@@ -80,16 +80,21 @@ namespace Pole.Orleans.Provider.EntityframeworkCore
bool isPersisted = _options.IsPersistedFunc(entity);
if (isPersisted)
{
TEntity entityInDb = await _options.ReadStateAsync(context, grainReference)
.ConfigureAwait(false);
Copy(entity, entityInDb);
if (_options.IsRelatedData)
{
TEntity entityInDb = await _options.ReadStateAsync(context, grainReference)
.ConfigureAwait(false);
Copy(entity, entityInDb);
}
else
{
context.Entry(entity).State = EntityState.Modified;
}
}
else
{
context.Set<TEntity>().Add(entity);
}
}
try
......
......@@ -50,6 +50,8 @@ namespace Pole.Orleans.Provider.EntityframeworkCore
internal Func<TEntity, long> LongKeySelector { get; set; }
public bool IsRelatedData { get; set; }
internal Func<TContext, IAddressable, Task<TEntity>> ReadStateAsync { get; set; }
internal Action<IGrainState, TEntity> SetEntity { get; set; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment