Commit 7819603e by dingsongjie

完成 EFCore grains 全部功能

parent b615a32a
...@@ -40,5 +40,12 @@ namespace Backet.Api.Controllers ...@@ -40,5 +40,12 @@ namespace Backet.Api.Controllers
var grain = clusterClient.GetGrain<IBacketGrain>(id); var grain = clusterClient.GetGrain<IBacketGrain>(id);
return grain.AddBacketItem("55","测试3",1000); 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 ...@@ -28,11 +28,21 @@ namespace Backet.Api.Domain.AggregatesModel.BacketAggregate
{ {
foreach (var item in BacketItems) foreach (var item in BacketItems)
{ {
TotalPrice += item.Price; TotalPrice = BacketItems.Sum(m=>m.Price);
} }
} }
public string UserId { get; set; } public string UserId { get; set; }
public List<BacketItem> BacketItems { get; private set; } = new List<BacketItem>(); public List<BacketItem> BacketItems { get; private set; } = new List<BacketItem>();
public long TotalPrice { get; private set; } 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 ...@@ -11,6 +11,7 @@ namespace Backet.Api.Grains.Abstraction
Task<bool> AddBacket(BacketDto backet); Task<bool> AddBacket(BacketDto backet);
Task<bool> UpdateBacket(string userId); Task<bool> UpdateBacket(string userId);
Task<bool> AddBacketItem(string productId, string productName, long price); Task<bool> AddBacketItem(string productId, string productName, long price);
Task<bool> RemoveFirstItem();
} }
public class BacketItemDto public class BacketItemDto
{ {
......
...@@ -37,6 +37,14 @@ namespace Backet.Api.Grains ...@@ -37,6 +37,14 @@ namespace Backet.Api.Grains
return true; return true;
} }
public async Task<bool> RemoveFirstItem()
{
State.RemoveFirstItem();
Update(State);
await WriteStateAsync();
return true;
}
public async Task<bool> UpdateBacket(string userId) public async Task<bool> UpdateBacket(string userId)
{ {
if (State == null) return false; if (State == null) return false;
......
...@@ -29,14 +29,16 @@ namespace Backet.Api ...@@ -29,14 +29,16 @@ namespace Backet.Api
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) 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.AddControllers();
services.ConfigureGrainStorageOptions<BacketDbContext, BacketGrain, Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet>( services.ConfigureGrainStorageOptions<BacketDbContext, BacketGrain, Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet>(
options => options options =>
.UseQuery(context => context.Backets {
.Include(box => box.BacketItems) options.UseQuery(context => context.Backets
)); .Include(box => box.BacketItems));
options.IsRelatedData = true;
});
......
...@@ -80,16 +80,21 @@ namespace Pole.Orleans.Provider.EntityframeworkCore ...@@ -80,16 +80,21 @@ namespace Pole.Orleans.Provider.EntityframeworkCore
bool isPersisted = _options.IsPersistedFunc(entity); bool isPersisted = _options.IsPersistedFunc(entity);
if (isPersisted) if (isPersisted)
{ {
TEntity entityInDb = await _options.ReadStateAsync(context, grainReference) if (_options.IsRelatedData)
.ConfigureAwait(false); {
Copy(entity, entityInDb); TEntity entityInDb = await _options.ReadStateAsync(context, grainReference)
.ConfigureAwait(false);
Copy(entity, entityInDb);
}
else
{
context.Entry(entity).State = EntityState.Modified;
}
} }
else else
{ {
context.Set<TEntity>().Add(entity); context.Set<TEntity>().Add(entity);
} }
} }
try try
......
...@@ -50,6 +50,8 @@ namespace Pole.Orleans.Provider.EntityframeworkCore ...@@ -50,6 +50,8 @@ namespace Pole.Orleans.Provider.EntityframeworkCore
internal Func<TEntity, long> LongKeySelector { get; set; } internal Func<TEntity, long> LongKeySelector { get; set; }
public bool IsRelatedData { get; set; }
internal Func<TContext, IAddressable, Task<TEntity>> ReadStateAsync { get; set; } internal Func<TContext, IAddressable, Task<TEntity>> ReadStateAsync { get; set; }
internal Action<IGrainState, TEntity> SetEntity { 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