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
2d8ef67d
authored
Jan 15, 2020
by
dingsongjie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构 domain event
parent
450dd303
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
38 additions
and
71 deletions
samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs
samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs
samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs
src/Pole.Application/Cqrs/CommandResult.cs
src/Pole.Domain.EntityframeworkCore/DbContextBase.cs
src/Pole.Domain.EntityframeworkCore/MediatR/MediatorExtension.cs
src/Pole.Domain/DomainHandleResult.cs
src/Pole.Domain/IDomainEvent.cs
src/Pole.Domain/IDomainEventHandler.cs
src/Pole.Domain/UnitOfWork/CompleteResult.cs
src/Pole.Grpc/ExtraType/CommonCommandResponse.cs
samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs
View file @
2d8ef67d
...
...
@@ -31,7 +31,8 @@ namespace Product.Api.Application.Command.CommandHandler
ProductTypeName
=
productType
.
Name
};
productType
.
AddDomainEvent
(
productTypeAddedDomainEvent
);
return
await
_productTypeRepository
.
UnitOfWork
.
CompeleteAsync
();
var
result
=
await
_productTypeRepository
.
UnitOfWork
.
CompeleteAsync
();
return
result
;
}
}
}
samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs
0 → 100644
View file @
2d8ef67d
using
Pole.Domain
;
using
Product.Api.Domain.Event
;
using
Product.Api.Domain.ProductAggregate
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading
;
using
System.Threading.Tasks
;
namespace
Product.Api.Application.DomainEventHandler
{
public
class
AddDefaultProductWhenProductTypeAdded2DomainEventHandler
:
IDomainEventHandler
<
ProductTypeAddedDomainEvent
>
{
private
readonly
IProductRepository
_productRepository
;
public
AddDefaultProductWhenProductTypeAdded2DomainEventHandler
(
IProductRepository
productRepository
)
{
_productRepository
=
productRepository
;
}
public
async
Task
Handle
(
ProductTypeAddedDomainEvent
request
,
CancellationToken
cancellationToken
)
{
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
product
=
new
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
(
Guid
.
NewGuid
().
ToString
(
"N"
),
request
.
ProductTypeName
,
100
,
request
.
ProductTypeId
);
_productRepository
.
Add
(
product
);
await
_productRepository
.
UnitOfWork
.
CompeleteAsync
();
}
}
}
samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs
View file @
2d8ef67d
...
...
@@ -17,12 +17,11 @@ namespace Product.Api.Application.DomainEventHandler
_productRepository
=
productRepository
;
}
public
async
Task
<
DomainHandleResult
>
Handle
(
ProductTypeAddedDomainEvent
request
,
CancellationToken
cancellationToken
)
public
async
Task
Handle
(
ProductTypeAddedDomainEvent
request
,
CancellationToken
cancellationToken
)
{
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
product
=
new
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
(
Guid
.
NewGuid
().
ToString
(
"N"
),
request
.
ProductTypeName
,
100
,
request
.
ProductTypeId
);
_productRepository
.
Add
(
product
);
return
await
_productRepository
.
UnitOfWork
.
CompeleteAsync
();
await
_productRepository
.
UnitOfWork
.
CompeleteAsync
();
}
}
}
src/Pole.Application/Cqrs/CommandResult.cs
deleted
100644 → 0
View file @
450dd303
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
Pole.Application.Cqrs
{
//public class CommandResult
//{
// public CommandResult(int status,string message)
// {
// Status = status;
// Message = message;
// }
// public static CommandResult SuccessResult = new CommandResult(1, "操作成功");
// /// <summary>
// /// 1 Command Success 2 Command Faild ...
// /// </summary>
// public int Status { get;private set; }
// public string Message { get;private set; }
//}
}
src/Pole.Domain.EntityframeworkCore/DbContextBase.cs
View file @
2d8ef67d
...
...
@@ -23,12 +23,8 @@ namespace Pole.EntityframeworkCore
{
var
result
=
CompleteResult
.
SuccessResult
;
var
eventHnadlersResult
=
await
_mediator
.
DispatchDomainEventsAsync
(
this
);
if
(
eventHnadlersResult
.
Status
!=
1
)
{
return
new
CompleteResult
(
eventHnadlersResult
);
}
var
dbResult
=
await
base
.
SaveChangesAsync
(
cancellationToken
);
await
_mediator
.
DispatchDomainEventsAsync
(
this
);
await
base
.
SaveChangesAsync
(
cancellationToken
);
return
result
;
}
...
...
src/Pole.Domain.EntityframeworkCore/MediatR/MediatorExtension.cs
View file @
2d8ef67d
...
...
@@ -11,10 +11,8 @@ namespace Pole.EntityframeworkCore.MediatR
{
public
static
class
MediatorExtension
{
public
static
async
Task
<
DomainHandleResult
>
DispatchDomainEventsAsync
(
this
IMediator
mediator
,
DbContext
ctx
)
public
static
async
Task
DispatchDomainEventsAsync
(
this
IMediator
mediator
,
DbContext
ctx
)
{
var
result
=
DomainHandleResult
.
SuccessResult
;
var
domainEntities
=
ctx
.
ChangeTracker
.
Entries
<
Entity
>()
.
Where
(
x
=>
x
.
Entity
.
DomainEvents
!=
null
&&
x
.
Entity
.
DomainEvents
.
Any
());
...
...
@@ -28,14 +26,8 @@ namespace Pole.EntityframeworkCore.MediatR
foreach
(
var
domainEvent
in
domainEvents
)
{
var
currentDomainHandleResult
=
await
mediator
.
Send
(
domainEvent
);
if
(
currentDomainHandleResult
.
Status
!=
1
)
{
result
=
currentDomainHandleResult
;
break
;
}
await
mediator
.
Publish
(
domainEvent
);
}
return
result
;
}
}
}
src/Pole.Domain/DomainHandleResult.cs
deleted
100644 → 0
View file @
450dd303
using
Pole.Domain.UnitOfWork
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
Pole.Domain
{
public
class
DomainHandleResult
{
public
DomainHandleResult
(
int
status
,
string
message
)
{
Status
=
status
;
Message
=
message
;
}
public
static
DomainHandleResult
SuccessResult
=
new
DomainHandleResult
(
1
,
"处理成功"
);
public
static
implicit
operator
DomainHandleResult
(
CompleteResult
completeResult
)
{
return
new
DomainHandleResult
(
completeResult
.
Status
,
completeResult
.
Message
);
}
/// <summary>
/// 1 Success 2 Faild ...
/// </summary>
public
int
Status
{
get
;
private
set
;
}
public
string
Message
{
get
;
private
set
;
}
}
}
src/Pole.Domain/IDomainEvent.cs
View file @
2d8ef67d
...
...
@@ -5,7 +5,7 @@ using System.Text;
namespace
Pole.Domain
{
public
interface
IDomainEvent
:
I
Request
<
DomainHandleResult
>
public
interface
IDomainEvent
:
I
Notification
{
}
...
...
src/Pole.Domain/IDomainEventHandler.cs
View file @
2d8ef67d
...
...
@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace
Pole.Domain
{
public
interface
IDomainEventHandler
<
TCommand
>
:
I
RequestHandler
<
TCommand
,
DomainHandleResult
>
where
TCommand
:
IRequest
<
DomainHandleResult
>
public
interface
IDomainEventHandler
<
TCommand
>
:
I
NotificationHandler
<
TCommand
>
where
TCommand
:
IDomainEvent
{
}
...
...
src/Pole.Domain/UnitOfWork/CompleteResult.cs
View file @
2d8ef67d
...
...
@@ -12,7 +12,6 @@ namespace Pole.Domain.UnitOfWork
Status
=
status
;
Message
=
message
;
}
public
CompleteResult
(
DomainHandleResult
domainHandleResult
)
:
this
(
domainHandleResult
.
Status
,
domainHandleResult
.
Message
)
{
}
/// <summary>
/// 1 Success 2 Faild ...
...
...
src/Pole.Grpc/ExtraType/CommonCommandResponse.cs
View file @
2d8ef67d
...
...
@@ -11,7 +11,7 @@ namespace Pole.Grpc.ExtraType
{
public
static
implicit
operator
CommonCommandResponse
(
CompleteResult
domainHandleResult
)
{
return
new
Com
pleteResult
(
domainHandleResult
.
Status
,
domainHandleResult
.
Message
)
;
return
new
Com
monCommandResponse
{
Status
=
domainHandleResult
.
Status
,
Message
=
domainHandleResult
.
Message
}
;
}
}
}
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