Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
丁松杰
/
Pole
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7819603e
authored
Feb 20, 2020
by
dingsongjie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完成 EFCore grains 全部功能
parent
b615a32a
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
47 additions
and
11 deletions
samples/apis/Backet.Api/Controllers/BacketController.cs
samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/Backet.cs
samples/apis/Backet.Api/Grains/Abstraction/IBacketGrain.cs
samples/apis/Backet.Api/Grains/BacketGrain.cs
samples/apis/Backet.Api/Startup.cs
src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorage.cs
src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorageOptions.cs
samples/apis/Backet.Api/Controllers/BacketController.cs
View file @
7819603e
...
...
@@ -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
samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/Backet.cs
View file @
7819603e
...
...
@@ -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
();
}
}
}
}
samples/apis/Backet.Api/Grains/Abstraction/IBacketGrain.cs
View file @
7819603e
...
...
@@ -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
{
...
...
samples/apis/Backet.Api/Grains/BacketGrain.cs
View file @
7819603e
...
...
@@ -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
;
...
...
samples/apis/Backet.Api/Startup.cs
View file @
7819603e
...
...
@@ -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
;
});
...
...
src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorage.cs
View file @
7819603e
...
...
@@ -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
...
...
src/Pole.Orleans.Provider.EntityframeworkCore/GrainStorageOptions.cs
View file @
7819603e
...
...
@@ -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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment