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
5495256f
authored
Jan 16, 2020
by
dingsongjie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完成 unitofwork 部分改造
parent
06f7c1ca
Hide whitespace changes
Inline
Side-by-side
Showing
31 changed files
with
246 additions
and
188 deletions
samples/apis/Product.Api/Application/Command/AddProductTypeCommand.cs
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
samples/apis/Product.Api/Grpc/ProductTypeService.cs
samples/apis/Product.Api/Infrastructure/ProductDbContext.cs
samples/apis/Product.Api/Infrastructure/Repository/ProductRepository.cs
samples/apis/Product.Api/Infrastructure/Repository/ProductTypeRepository.cs
samples/apis/Product.Api/ProductDbContextDesignFactory.cs
samples/apis/Product.Api/Startup.cs
src/Pole.Application/Cqrs/ICommand.cs → src/Pole.Application/Command/ICommand.cs
src/Pole.Application/Cqrs/ICommandBus.cs → src/Pole.Application/Command/ICommandBus.cs
src/Pole.Application/Cqrs/ICommandHandler.cs → src/Pole.Application/Command/ICommandHandler.cs
src/Pole.Application/Cqrs/Internal/DefaultCommandBus.cs → src/Pole.Application/Command/Internal/DefaultCommandBus.cs
src/Pole.Application/PoleApplicationOptions.cs → src/Pole.Application/PoleOptions.cs
src/Pole.Application/PoleApplicationOptionsExtensions.cs → src/Pole.Application/PoleOptionsExtensions.cs
src/Pole.Application/Cqrs/IQueries.cs → src/Pole.Application/Query/IQueries.cs
src/Pole.Application/ServiceCollectionExtensions.cs
src/Pole.Domain.EntityframeworkCore/DbContextBase.cs
src/Pole.Domain.EntityframeworkCore/EFCoreRepository.cs
src/Pole.Domain.EntityframeworkCore/Pole.Domain.EntityframeworkCore.csproj
src/Pole.Domain/ServiceCollectionExtensions.cs → src/Pole.Domain.EntityframeworkCore/ServiceCollectionExtension.cs
src/Pole.Domain.EntityframeworkCore/UnitOfWork/EntityFrameworkCoreUnitOfWork.cs
src/Pole.Domain.EntityframeworkCore/UnitOfWork/EntityFrameworkCoreUnitOfWorkManager.cs
src/Pole.Domain/IRepository.cs
src/Pole.Domain/UnitOfWork/IUnitOfWork.cs
src/Pole.Domain/UnitOfWork/IUnitOfWorkManager.cs
src/Pole.Domain/UnitOfWork/CompleteResult.cs → src/Pole.Domain/UnitOfWork/UnitOfWorkResult.cs
src/Pole.Grpc/ExtraType/CommonCommandResponse.cs
src/Pole.Grpc/PoleGrpcOptionExtensions.cs
src/Pole.Grpc/ServiceCollectionExtensions.cs
samples/apis/Product.Api/Application/Command/AddProductTypeCommand.cs
View file @
5495256f
using
Pole.Application.Cqrs
;
using
Pole.Application.Command
;
using
Pole.Application.Cqrs
;
using
Pole.Grpc.ExtraType
;
using
Pole.Grpc.ExtraType
;
using
PoleSample.Apis.Product
;
using
PoleSample.Apis.Product
;
using
System
;
using
System
;
...
...
samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs
View file @
5495256f
using
NewArchitectureLab.Apps.Product
;
using
NewArchitectureLab.Apps.Product
;
using
Pole.Application.Command
;
using
Pole.Application.Cqrs
;
using
Pole.Application.Cqrs
;
using
Pole.Domain.UnitOfWork
;
using
Pole.Grpc.ExtraType
;
using
Pole.Grpc.ExtraType
;
using
PoleSample.Apis.Product
;
using
PoleSample.Apis.Product
;
using
Product.Api.Domain.Event
;
using
Product.Api.Domain.Event
;
...
@@ -15,9 +17,11 @@ namespace Product.Api.Application.Command.CommandHandler
...
@@ -15,9 +17,11 @@ namespace Product.Api.Application.Command.CommandHandler
public
class
AddProductTypeCommandHandler
:
ICommandHandler
<
AddProductTypeCommand
,
CommonCommandResponse
>
public
class
AddProductTypeCommandHandler
:
ICommandHandler
<
AddProductTypeCommand
,
CommonCommandResponse
>
{
{
private
readonly
IProductTypeRepository
_productTypeRepository
;
private
readonly
IProductTypeRepository
_productTypeRepository
;
public
AddProductTypeCommandHandler
(
IProductTypeRepository
productTypeRepository
)
private
readonly
IUnitOfWorkManager
_unitOfWorkManager
;
public
AddProductTypeCommandHandler
(
IProductTypeRepository
productTypeRepository
,
IUnitOfWorkManager
unitOfWorkManager
)
{
{
_productTypeRepository
=
productTypeRepository
;
_productTypeRepository
=
productTypeRepository
;
_unitOfWorkManager
=
unitOfWorkManager
;
}
}
public
async
Task
<
CommonCommandResponse
>
Handle
(
AddProductTypeCommand
request
,
CancellationToken
cancellationToken
)
public
async
Task
<
CommonCommandResponse
>
Handle
(
AddProductTypeCommand
request
,
CancellationToken
cancellationToken
)
{
{
...
@@ -30,9 +34,14 @@ namespace Product.Api.Application.Command.CommandHandler
...
@@ -30,9 +34,14 @@ namespace Product.Api.Application.Command.CommandHandler
ProductTypeId
=
productType
.
Id
,
ProductTypeId
=
productType
.
Id
,
ProductTypeName
=
productType
.
Name
ProductTypeName
=
productType
.
Name
};
};
productType
.
AddDomainEvent
(
productTypeAddedDomainEvent
);
using
(
var
unitOfWork
=
await
_unitOfWorkManager
.
BeginUnitOfWork
())
var
result
=
await
_productTypeRepository
.
UnitOfWork
.
CompeleteAsync
();
{
return
result
;
productType
.
AddDomainEvent
(
productTypeAddedDomainEvent
);
var
result
=
await
_productTypeRepository
.
SaveEntitiesAsync
();
await
unitOfWork
.
CompeleteAsync
();
return
CommonCommandResponse
.
SuccessResponse
;
}
}
}
}
}
}
}
samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs
View file @
5495256f
...
@@ -21,7 +21,7 @@ namespace Product.Api.Application.DomainEventHandler
...
@@ -21,7 +21,7 @@ namespace Product.Api.Application.DomainEventHandler
{
{
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
product
=
new
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
(
Guid
.
NewGuid
().
ToString
(
"N"
),
request
.
ProductTypeName
,
100
,
request
.
ProductTypeId
);
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
product
=
new
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
(
Guid
.
NewGuid
().
ToString
(
"N"
),
request
.
ProductTypeName
,
100
,
request
.
ProductTypeId
);
_productRepository
.
Add
(
product
);
_productRepository
.
Add
(
product
);
await
_productRepository
.
UnitOfWork
.
Compelete
Async
();
await
_productRepository
.
SaveEntities
Async
();
}
}
}
}
}
}
samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs
View file @
5495256f
...
@@ -21,7 +21,7 @@ namespace Product.Api.Application.DomainEventHandler
...
@@ -21,7 +21,7 @@ namespace Product.Api.Application.DomainEventHandler
{
{
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
product
=
new
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
(
Guid
.
NewGuid
().
ToString
(
"N"
),
request
.
ProductTypeName
,
100
,
request
.
ProductTypeId
);
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
product
=
new
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
(
Guid
.
NewGuid
().
ToString
(
"N"
),
request
.
ProductTypeName
,
100
,
request
.
ProductTypeId
);
_productRepository
.
Add
(
product
);
_productRepository
.
Add
(
product
);
await
_productRepository
.
UnitOfWork
.
Compelete
Async
();
await
_productRepository
.
SaveEntities
Async
();
}
}
}
}
}
}
samples/apis/Product.Api/Grpc/ProductTypeService.cs
View file @
5495256f
using
Grpc.Core
;
using
Grpc.Core
;
using
NewArchitectureLab.Apps.Product
;
using
NewArchitectureLab.Apps.Product
;
using
Pole.Application.Command
;
using
Pole.Application.Cqrs
;
using
Pole.Application.Cqrs
;
using
Pole.Grpc.ExtraType
;
using
Pole.Grpc.ExtraType
;
using
PoleSample.Apis.Product
;
using
PoleSample.Apis.Product
;
using
Product.Api.Application.Command
;
using
Product.Api.Application.Command
;
using
Product.Api.Infrastructure
;
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Linq
;
...
...
samples/apis/Product.Api/Infrastructure/ProductDbContext.cs
View file @
5495256f
...
@@ -9,11 +9,11 @@ using System.Threading.Tasks;
...
@@ -9,11 +9,11 @@ using System.Threading.Tasks;
namespace
Product.Api.Infrastructure
namespace
Product.Api.Infrastructure
{
{
public
class
ProductDbContext
:
DbContext
Base
public
class
ProductDbContext
:
DbContext
{
{
public
DbSet
<
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
>
Products
{
get
;
set
;
}
public
DbSet
<
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
>
Products
{
get
;
set
;
}
public
DbSet
<
Product
.
Api
.
Domain
.
ProductTypeAggregate
.
ProductType
>
ProductTypes
{
get
;
set
;
}
public
DbSet
<
Product
.
Api
.
Domain
.
ProductTypeAggregate
.
ProductType
>
ProductTypes
{
get
;
set
;
}
public
ProductDbContext
(
DbContextOptions
options
,
IMediator
mediator
)
:
base
(
options
,
mediator
)
public
ProductDbContext
(
DbContextOptions
<
ProductDbContext
>
options
)
:
base
(
options
)
{
{
}
}
...
...
samples/apis/Product.Api/Infrastructure/Repository/ProductRepository.cs
View file @
5495256f
using
Pole.Domain.UnitOfWork
;
using
Pole.Domain.EntityframeworkCore
;
using
Pole.Domain.UnitOfWork
;
using
Product.Api.Domain.ProductAggregate
;
using
Product.Api.Domain.ProductAggregate
;
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
...
@@ -7,35 +8,11 @@ using System.Threading.Tasks;
...
@@ -7,35 +8,11 @@ using System.Threading.Tasks;
namespace
Product.Api.Infrastructure.Repository
namespace
Product.Api.Infrastructure.Repository
{
{
public
class
ProductRepository
:
IProductRepository
public
class
ProductRepository
:
EFCoreRepository
<
Product
.
Api
.
Domain
.
ProductAggregate
.
Product
>,
IProductRepository
{
{
private
readonly
ProductDbContext
_productDbContext
;
public
ProductRepository
(
IServiceProvider
serviceProvider
)
:
base
(
serviceProvider
)
public
ProductRepository
(
ProductDbContext
productDbContext
)
{
{
_productDbContext
=
productDbContext
;
}
public
IUnitOfWork
UnitOfWork
=>
_productDbContext
;
public
void
Add
(
Domain
.
ProductAggregate
.
Product
entity
)
{
_productDbContext
.
Products
.
Add
(
entity
);
}
public
void
Delete
(
Domain
.
ProductAggregate
.
Product
entity
)
{
throw
new
NotImplementedException
();
}
public
Task
<
Domain
.
ProductAggregate
.
Product
>
Get
(
string
id
)
{
throw
new
NotImplementedException
();
}
public
void
Update
(
Domain
.
ProductAggregate
.
Product
entity
)
{
throw
new
NotImplementedException
();
}
}
}
}
}
}
samples/apis/Product.Api/Infrastructure/Repository/ProductTypeRepository.cs
View file @
5495256f
using
Pole.Domain.UnitOfWork
;
using
Pole.Domain.EntityframeworkCore
;
using
Pole.Domain.UnitOfWork
;
using
Product.Api.Domain.ProductTypeAggregate
;
using
Product.Api.Domain.ProductTypeAggregate
;
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
...
@@ -7,34 +8,10 @@ using System.Threading.Tasks;
...
@@ -7,34 +8,10 @@ using System.Threading.Tasks;
namespace
Product.Api.Infrastructure.Repository
namespace
Product.Api.Infrastructure.Repository
{
{
public
class
ProductTypeRepository
:
IProductTypeRepository
public
class
ProductTypeRepository
:
EFCoreRepository
<
ProductType
>,
IProductTypeRepository
{
{
private
readonly
ProductDbContext
_productDbContext
;
public
ProductTypeRepository
(
IServiceProvider
serviceProvider
)
:
base
(
serviceProvider
)
public
ProductTypeRepository
(
ProductDbContext
productDbContext
)
{
_productDbContext
=
productDbContext
;
}
public
IUnitOfWork
UnitOfWork
=>
_productDbContext
;
public
void
Add
(
ProductType
entity
)
{
_productDbContext
.
ProductTypes
.
Add
(
entity
);
}
public
void
Delete
(
ProductType
entity
)
{
throw
new
NotImplementedException
();
}
public
Task
<
ProductType
>
Get
(
string
id
)
{
throw
new
NotImplementedException
();
}
public
void
Update
(
ProductType
entity
)
{
{
throw
new
NotImplementedException
();
}
}
}
}
}
}
samples/apis/Product.Api/ProductDbContextDesignFactory.cs
View file @
5495256f
...
@@ -22,9 +22,7 @@ namespace Product.Api
...
@@ -22,9 +22,7 @@ namespace Product.Api
var
optionsBuilder
=
new
DbContextOptionsBuilder
<
ProductDbContext
>()
var
optionsBuilder
=
new
DbContextOptionsBuilder
<
ProductDbContext
>()
.
UseNpgsql
(
configuration
[
"postgres:main"
]);
.
UseNpgsql
(
configuration
[
"postgres:main"
]);
return
new
ProductDbContext
(
optionsBuilder
.
Options
,
new
NoMediator
()
);
return
new
ProductDbContext
(
optionsBuilder
.
Options
);
}
}
}
}
}
}
samples/apis/Product.Api/Startup.cs
View file @
5495256f
...
@@ -28,17 +28,27 @@ namespace Product.Api
...
@@ -28,17 +28,27 @@ namespace Product.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
.
AddDbContext
<
ProductDbContext
>(
options
=>
services
.
AddDbContext
<
ProductDbContext
>(
options
=>
options
.
UseNpgsql
(
Configuration
[
"postgres:main"
]));
options
.
UseNpgsql
(
Configuration
[
"postgres:main"
]));
services
.
AddGrpc
(
option
=>
{
services
.
AddGrpc
(
option
=>
{
if
(
Environment
.
IsDevelopment
())
if
(
Environment
.
IsDevelopment
())
{
{
option
.
EnableDetailedErrors
=
true
;
option
.
EnableDetailedErrors
=
true
;
}
}
});
});
services
.
AddPoleGrpc
(
this
.
GetType
().
Assembly
);
services
.
AddGrpcValidation
();
services
.
AddGrpcRequestValidator
(
this
.
GetType
().
Assembly
);
services
.
AddPole
(
option
=>
{
option
.
AddManageredAssemblies
(
this
.
GetType
().
Assembly
);
option
.
AutoInjectionDependency
();
option
.
AutoInjectionCommandHandlersAndDomainEventHandlers
();
option
.
AddPoleEntityFrameworkCoreDomain
();
});
services
.
AddPoleReliableMessage
(
option
=>
services
.
AddPoleReliableMessage
(
option
=>
{
{
...
...
src/Pole.Application/C
qrs
/ICommand.cs
→
src/Pole.Application/C
ommand
/ICommand.cs
View file @
5495256f
...
@@ -3,7 +3,7 @@ using System;
...
@@ -3,7 +3,7 @@ using System;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Text
;
namespace
Pole.Application.C
qrs
namespace
Pole.Application.C
ommand
{
{
public
interface
ICommand
<
TResponse
>:
IRequest
<
TResponse
>
public
interface
ICommand
<
TResponse
>:
IRequest
<
TResponse
>
{
{
...
...
src/Pole.Application/C
qrs
/ICommandBus.cs
→
src/Pole.Application/C
ommand
/ICommandBus.cs
View file @
5495256f
...
@@ -5,7 +5,7 @@ using System.Text;
...
@@ -5,7 +5,7 @@ using System.Text;
using
System.Threading
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
namespace
Pole.Application.C
qrs
namespace
Pole.Application.C
ommand
{
{
public
interface
ICommandBus
public
interface
ICommandBus
{
{
...
...
src/Pole.Application/C
qrs
/ICommandHandler.cs
→
src/Pole.Application/C
ommand
/ICommandHandler.cs
View file @
5495256f
...
@@ -3,7 +3,7 @@ using System;
...
@@ -3,7 +3,7 @@ using System;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Text
;
namespace
Pole.Application.C
qrs
namespace
Pole.Application.C
ommand
{
{
public
interface
ICommandHandler
<
TCommand
,
TResult
>:
IRequestHandler
<
TCommand
,
TResult
>
where
TCommand
:
ICommand
<
TResult
>
public
interface
ICommandHandler
<
TCommand
,
TResult
>:
IRequestHandler
<
TCommand
,
TResult
>
where
TCommand
:
ICommand
<
TResult
>
{
{
...
...
src/Pole.Application/C
qrs
/Internal/DefaultCommandBus.cs
→
src/Pole.Application/C
ommand
/Internal/DefaultCommandBus.cs
View file @
5495256f
...
@@ -4,6 +4,7 @@ using System.Text;
...
@@ -4,6 +4,7 @@ using System.Text;
using
System.Threading
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
MediatR
;
using
MediatR
;
using
Pole.Application.Command
;
namespace
Pole.Application.Cqrs.Internal
namespace
Pole.Application.Cqrs.Internal
{
{
...
...
src/Pole.Application/Pole
Application
Options.cs
→
src/Pole.Application/PoleOptions.cs
View file @
5495256f
...
@@ -6,14 +6,13 @@ using System.Text;
...
@@ -6,14 +6,13 @@ using System.Text;
namespace
Pole.Application
namespace
Pole.Application
{
{
public
class
Pole
Application
Options
public
class
PoleOptions
{
{
public
Pole
ApplicationOptions
(
IServiceCollection
services
,
params
Assembly
[]
applicationAssembli
es
)
public
Pole
Options
(
IServiceCollection
servic
es
)
{
{
Services
=
services
;
Services
=
services
;
ApplicationAssemblies
=
applicationAssemblies
;
}
}
public
IServiceCollection
Services
{
get
;
set
;
}
public
IServiceCollection
Services
{
get
;
private
set
;
}
public
IEnumerable
<
Assembly
>
ApplicationAssemblies
{
get
;
set
;
}
public
IEnumerable
<
Assembly
>
ApplicationAssemblies
{
get
;
set
;
}
}
}
...
...
src/Pole.Application/Pole
Application
OptionsExtensions.cs
→
src/Pole.Application/PoleOptionsExtensions.cs
View file @
5495256f
using
MediatR
;
using
MediatR
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.DependencyInjection
;
using
Pole.Application
;
using
Pole.Application.Cqrs
;
using
Pole.Application.Cqrs
;
using
Pole.Application.MediatR
;
using
Pole.Application.MediatR
;
using
Pole.Core.DependencyInjection
;
using
Pole.Core.DependencyInjection
;
...
@@ -9,11 +10,16 @@ using System.Linq;
...
@@ -9,11 +10,16 @@ using System.Linq;
using
System.Reflection
;
using
System.Reflection
;
using
System.Text
;
using
System.Text
;
namespace
Pole.Applica
tion
namespace
Microsoft.Extensions.DependencyInjec
tion
{
{
public
static
class
Pole
Application
OptionsExtensions
public
static
class
PoleOptionsExtensions
{
{
public
static
PoleApplicationOptions
AutoInjectionDependency
(
this
PoleApplicationOptions
options
)
public
static
PoleOptions
AddManageredAssemblies
(
this
PoleOptions
options
,
params
Assembly
[]
assemblies
)
{
options
.
ApplicationAssemblies
=
assemblies
;
return
options
;
}
public
static
PoleOptions
AutoInjectionDependency
(
this
PoleOptions
options
)
{
{
var
assemblies
=
options
.
ApplicationAssemblies
;
var
assemblies
=
options
.
ApplicationAssemblies
;
...
@@ -26,7 +32,7 @@ namespace Pole.Application
...
@@ -26,7 +32,7 @@ namespace Pole.Application
return
options
;
return
options
;
}
}
public
static
Pole
ApplicationOptions
AutoInjectionCommandHandlersAndDomainEventHandlers
(
this
PoleApplication
Options
options
,
ServiceLifetime
lifetime
=
ServiceLifetime
.
Scoped
)
public
static
Pole
Options
AutoInjectionCommandHandlersAndDomainEventHandlers
(
this
Pole
Options
options
,
ServiceLifetime
lifetime
=
ServiceLifetime
.
Scoped
)
{
{
options
.
Services
.
AddMediatR
(
config
=>
options
.
Services
.
AddMediatR
(
config
=>
{
{
...
@@ -35,35 +41,43 @@ namespace Pole.Application
...
@@ -35,35 +41,43 @@ namespace Pole.Application
return
options
;
return
options
;
}
}
private
static
void
AddScoped
(
Pole
Application
Options
options
,
Assembly
assembly
)
private
static
void
AddScoped
(
PoleOptions
options
,
Assembly
assembly
)
{
{
var
queriesI
mplements
=
assembly
.
GetTypes
().
Where
(
m
=>
typeof
(
IScopedDenpendency
).
IsAssignableFrom
(
m
)
&&
m
.
IsClass
&&
!
m
.
IsAbstract
);
var
i
mplements
=
assembly
.
GetTypes
().
Where
(
m
=>
typeof
(
IScopedDenpendency
).
IsAssignableFrom
(
m
)
&&
m
.
IsClass
&&
!
m
.
IsAbstract
);
foreach
(
var
queriesImplement
in
queriesI
mplements
)
foreach
(
var
implement
in
i
mplements
)
{
{
var
queriesService
=
queriesImplement
.
GetInterfaces
().
FirstOrDefault
();
var
services
=
implement
.
GetInterfaces
();
options
.
Services
.
AddScoped
(
queriesService
,
queriesImplement
);
foreach
(
var
queriesService
in
services
)
{
options
.
Services
.
AddScoped
(
queriesService
,
implement
);
}
}
}
}
}
private
static
void
AddTransient
(
Pole
Application
Options
options
,
Assembly
assembly
)
private
static
void
AddTransient
(
PoleOptions
options
,
Assembly
assembly
)
{
{
var
queriesI
mplements
=
assembly
.
GetTypes
().
Where
(
m
=>
typeof
(
ITransientDependency
).
IsAssignableFrom
(
m
)
&&
m
.
IsClass
&&
!
m
.
IsAbstract
);
var
i
mplements
=
assembly
.
GetTypes
().
Where
(
m
=>
typeof
(
ITransientDependency
).
IsAssignableFrom
(
m
)
&&
m
.
IsClass
&&
!
m
.
IsAbstract
);
foreach
(
var
queriesImplement
in
queriesI
mplements
)
foreach
(
var
implement
in
i
mplements
)
{
{
var
queriesService
=
queriesImplement
.
GetInterfaces
().
FirstOrDefault
();
var
services
=
implement
.
GetInterfaces
();
options
.
Services
.
AddTransient
(
queriesService
,
queriesImplement
);
foreach
(
var
queriesService
in
services
)
{
options
.
Services
.
AddTransient
(
queriesService
,
implement
);
}
}
}
}
}
private
static
void
AddSingleton
(
Pole
Application
Options
options
,
Assembly
assembly
)
private
static
void
AddSingleton
(
PoleOptions
options
,
Assembly
assembly
)
{
{
var
queriesI
mplements
=
assembly
.
GetTypes
().
Where
(
m
=>
typeof
(
ISingleDependency
).
IsAssignableFrom
(
m
)
&&
m
.
IsClass
&&
!
m
.
IsAbstract
);
var
i
mplements
=
assembly
.
GetTypes
().
Where
(
m
=>
typeof
(
ISingleDependency
).
IsAssignableFrom
(
m
)
&&
m
.
IsClass
&&
!
m
.
IsAbstract
);
foreach
(
var
queriesImplement
in
queriesI
mplements
)
foreach
(
var
implement
in
i
mplements
)
{
{
var
queriesService
=
queriesImplement
.
GetInterfaces
().
FirstOrDefault
();
var
services
=
implement
.
GetInterfaces
();
options
.
Services
.
AddSingleton
(
queriesService
,
queriesImplement
);
foreach
(
var
queriesService
in
services
)
{
options
.
Services
.
AddSingleton
(
queriesService
,
implement
);
}
}
}
}
}
}
}
}
}
src/Pole.Application/
Cqrs
/IQueries.cs
→
src/Pole.Application/
Query
/IQueries.cs
View file @
5495256f
...
@@ -3,7 +3,7 @@ using System;
...
@@ -3,7 +3,7 @@ using System;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Text
;
namespace
Pole.Application.
Cqrs
namespace
Pole.Application.
Query
{
{
public
interface
IQueries
:
IScopedDenpendency
public
interface
IQueries
:
IScopedDenpendency
{
{
...
...
src/Pole.Application/ServiceCollectionExtensions.cs
View file @
5495256f
...
@@ -5,17 +5,18 @@ using MediatR;
...
@@ -5,17 +5,18 @@ using MediatR;
using
System.Reflection
;
using
System.Reflection
;
using
Pole.Application.Cqrs
;
using
Pole.Application.Cqrs
;
using
Pole.Application.Cqrs.Internal
;
using
Pole.Application.Cqrs.Internal
;
using
Microsoft.Extensions.DependencyInjection
;
using
Pole.Application.Command
;
using
Pole.Application
;
namespace
Pole.Applica
tion
namespace
Microsoft.Extensions.DependencyInjec
tion
{
{
public
static
class
ServiceCollectionExtensions
public
static
class
ServiceCollectionExtensions
{
{
public
static
IServiceCollection
AddPole
Application
(
this
IServiceCollection
services
,
Action
<
PoleApplicationOptions
>
config
,
params
Assembly
[]
assemblies
)
public
static
IServiceCollection
AddPole
(
this
IServiceCollection
services
,
Action
<
PoleOptions
>
config
)
{
{
Pole
ApplicationOptions
poleApplicationOptions
=
new
PoleApplicationOptions
(
services
,
assembli
es
);
Pole
Options
poleOptions
=
new
PoleOptions
(
servic
es
);
config
(
pole
Application
Options
);
config
(
poleOptions
);
services
.
AddScoped
<
ICommandBus
,
DefaultCommandBus
>();
services
.
AddScoped
<
ICommandBus
,
DefaultCommandBus
>();
...
...
src/Pole.Domain.EntityframeworkCore/DbContextBase.cs
deleted
100644 → 0
View file @
06f7c1ca
using
MediatR
;
using
Microsoft.EntityFrameworkCore
;
using
Pole.Domain
;
using
Pole.Domain.UnitOfWork
;
using
Pole.EntityframeworkCore.MediatR
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Threading
;
using
System.Threading.Tasks
;
namespace
Pole.EntityframeworkCore
{
public
class
DbContextBase
:
DbContext
,
IUnitOfWork
{
private
readonly
IMediator
_mediator
;
public
DbContextBase
(
DbContextOptions
options
,
IMediator
mediator
)
:
base
(
options
)
{
_mediator
=
mediator
;
}
public
async
Task
<
CompleteResult
>
CompeleteAsync
(
CancellationToken
cancellationToken
=
default
)
{
var
result
=
CompleteResult
.
SuccessResult
;
await
_mediator
.
DispatchDomainEventsAsync
(
this
);
await
base
.
SaveChangesAsync
(
cancellationToken
);
return
result
;
}
}
}
src/Pole.Domain.EntityframeworkCore/EFCoreRepository.cs
0 → 100644
View file @
5495256f
using
MediatR
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.DependencyInjection
;
using
Pole.EntityframeworkCore.MediatR
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Threading
;
using
System.Threading.Tasks
;
namespace
Pole.Domain.EntityframeworkCore
{
public
class
EFCoreRepository
<
TEntity
>
:
IRepository
<
TEntity
>
where
TEntity
:
Entity
,
IAggregateRoot
{
protected
readonly
DbContext
_dbContext
;
private
readonly
IMediator
_mediator
;
public
EFCoreRepository
(
IServiceProvider
serviceProvider
)
{
var
dbContextOptions
=
serviceProvider
.
GetRequiredService
<
DbContextOptions
>();
_dbContext
=
serviceProvider
.
GetRequiredService
(
dbContextOptions
.
ContextType
)
as
DbContext
;
_mediator
=
serviceProvider
.
GetRequiredService
<
IMediator
>();
}
public
void
Add
(
TEntity
entity
)
{
_dbContext
.
Set
<
TEntity
>().
Add
(
entity
);
}
public
void
Delete
(
TEntity
entity
)
{
_dbContext
.
Set
<
TEntity
>().
Remove
(
entity
);
}
public
virtual
Task
<
TEntity
>
Get
(
string
id
)
{
return
_dbContext
.
Set
<
TEntity
>().
FirstOrDefaultAsync
(
m
=>
m
.
Id
==
id
);
}
public
async
Task
<
bool
>
SaveEntitiesAsync
(
CancellationToken
cancellationToken
=
default
)
{
await
_mediator
.
DispatchDomainEventsAsync
(
_dbContext
);
await
_dbContext
.
SaveChangesAsync
(
cancellationToken
);
return
true
;
}
public
void
Update
(
TEntity
entity
)
{
throw
new
NotImplementedException
();
}
}
}
src/Pole.Domain.EntityframeworkCore/Pole.Domain.EntityframeworkCore.csproj
View file @
5495256f
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
</ItemGroup>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Pole.Application\Pole.Application.csproj" />
<ProjectReference Include="..\Pole.Domain\Pole.Domain.csproj" />
<ProjectReference Include="..\Pole.Domain\Pole.Domain.csproj" />
</ItemGroup>
</ItemGroup>
...
...
src/Pole.Domain
/ServiceCollectionExtensions
.cs
→
src/Pole.Domain
.EntityframeworkCore/ServiceCollectionExtension
.cs
View file @
5495256f
using
M
ediatR
;
using
M
icrosoft.EntityFrameworkCore
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.DependencyInjection
;
using
Pole.Application
;
using
Pole.Domain.EntityframeworkCore
;
using
Pole.Domain.EntityframeworkCore.UnitOfWork
;
using
Pole.Domain.UnitOfWork
;
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Text
;
namespace
Pole.Domai
n
namespace
Microsoft.Extensions.DependencyInjectio
n
{
{
public
static
class
ServiceCollectionExtensions
public
static
class
PoleApplicationOptionsExtension
{
{
public
static
IServiceCollection
AddPoleDomain
(
this
IServiceCollection
service
)
public
static
PoleOptions
AddPoleEntityFrameworkCoreDomain
(
this
PoleOptions
options
)
{
{
service
.
AddMediatR
();
options
.
Services
.
AddScoped
<
IUnitOfWorkManager
,
EntityFrameworkCoreUnitOfWorkManager
>
();
return
service
;
return
options
;
}
}
}
}
}
}
src/Pole.Domain.EntityframeworkCore/UnitOfWork/EntityFrameworkCoreUnitOfWork.cs
0 → 100644
View file @
5495256f
using
Microsoft.EntityFrameworkCore.Storage
;
using
Pole.Domain.UnitOfWork
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Threading
;
using
System.Threading.Tasks
;
namespace
Pole.Domain.EntityframeworkCore.UnitOfWork
{
public
class
EntityFrameworkCoreUnitOfWork
:
IUnitOfWork
{
private
readonly
IDbContextTransaction
_dbContextTransaction
;
public
EntityFrameworkCoreUnitOfWork
(
IDbContextTransaction
dbContextTransaction
)
{
_dbContextTransaction
=
dbContextTransaction
;
}
public
Task
CompeleteAsync
(
CancellationToken
cancellationToken
=
default
)
{
return
_dbContextTransaction
.
CommitAsync
(
cancellationToken
);
}
public
void
Dispose
()
{
_dbContextTransaction
?.
Dispose
();
}
public
Task
RollbackAsync
(
CancellationToken
cancellationToken
=
default
)
{
return
_dbContextTransaction
.
RollbackAsync
();
}
}
}
src/Pole.Domain.EntityframeworkCore/UnitOfWork/EntityFrameworkCoreUnitOfWorkManager.cs
0 → 100644
View file @
5495256f
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.DependencyInjection
;
using
Pole.Domain.UnitOfWork
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Pole.Domain.EntityframeworkCore.UnitOfWork
{
public
class
EntityFrameworkCoreUnitOfWorkManager
:
IUnitOfWorkManager
{
private
readonly
DbContext
_dbContext
;
public
EntityFrameworkCoreUnitOfWorkManager
(
DbContextOptions
dbContextOptions
,
IServiceProvider
serviceProvider
)
{
_dbContext
=
serviceProvider
.
GetRequiredService
(
dbContextOptions
.
ContextType
)
as
DbContext
;
}
public
async
Task
<
IUnitOfWork
>
BeginUnitOfWork
()
{
var
transaction
=
await
_dbContext
.
Database
.
BeginTransactionAsync
();
EntityFrameworkCoreUnitOfWork
entityFrameworkCoreUnitOfWork
=
new
EntityFrameworkCoreUnitOfWork
(
transaction
);
return
entityFrameworkCoreUnitOfWork
;
}
}
}
src/Pole.Domain/IRepository.cs
View file @
5495256f
...
@@ -14,7 +14,7 @@ namespace Pole.Domain
...
@@ -14,7 +14,7 @@ namespace Pole.Domain
void
Delete
(
T
entity
);
void
Delete
(
T
entity
);
void
Add
(
T
entity
);
void
Add
(
T
entity
);
Task
<
T
>
Get
(
string
id
);
Task
<
T
>
Get
(
string
id
);
IUnitOfWork
UnitOfWork
{
get
;
}
Task
<
bool
>
SaveEntitiesAsync
(
CancellationToken
cancellationToken
=
default
);
}
}
public
interface
IRepository
:
IScopedDenpendency
public
interface
IRepository
:
IScopedDenpendency
{
{
...
...
src/Pole.Domain/UnitOfWork/IUnitOfWork.cs
View file @
5495256f
...
@@ -8,8 +8,8 @@ namespace Pole.Domain.UnitOfWork
...
@@ -8,8 +8,8 @@ namespace Pole.Domain.UnitOfWork
{
{
public
interface
IUnitOfWork
:
IDisposable
public
interface
IUnitOfWork
:
IDisposable
{
{
//Task<int> SaveChanges
Async(CancellationToken cancellationToken = default);
Task
Compelete
Async
(
CancellationToken
cancellationToken
=
default
);
Task
<
CompleteResult
>
Compelete
Async
(
CancellationToken
cancellationToken
=
default
);
Task
Rollback
Async
(
CancellationToken
cancellationToken
=
default
);
}
}
}
}
src/Pole.Domain/UnitOfWork/IUnitOfWorkManager.cs
0 → 100644
View file @
5495256f
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Threading
;
using
System.Threading.Tasks
;
namespace
Pole.Domain.UnitOfWork
{
public
interface
IUnitOfWorkManager
{
Task
<
IUnitOfWork
>
BeginUnitOfWork
();
}
}
src/Pole.Domain/UnitOfWork/
Complete
Result.cs
→
src/Pole.Domain/UnitOfWork/
UnitOfWork
Result.cs
View file @
5495256f
...
@@ -4,10 +4,11 @@ using System.Text;
...
@@ -4,10 +4,11 @@ using System.Text;
namespace
Pole.Domain.UnitOfWork
namespace
Pole.Domain.UnitOfWork
{
{
public
class
Complete
Result
public
class
UnitOfWork
Result
{
{
public
static
CompleteResult
SuccessResult
=
new
CompleteResult
(
1
,
"保存成功"
);
public
static
UnitOfWorkResult
SuccessResult
=
new
UnitOfWorkResult
(
1
,
"保存成功"
);
public
CompleteResult
(
int
status
,
string
message
)
public
static
UnitOfWorkResult
FaildResult
=
new
UnitOfWorkResult
(
1
,
"保存失败"
);
public
UnitOfWorkResult
(
int
status
,
string
message
)
{
{
Status
=
status
;
Status
=
status
;
Message
=
message
;
Message
=
message
;
...
...
src/Pole.Grpc/ExtraType/CommonCommandResponse.cs
View file @
5495256f
...
@@ -9,9 +9,10 @@ namespace Pole.Grpc.ExtraType
...
@@ -9,9 +9,10 @@ namespace Pole.Grpc.ExtraType
{
{
public
partial
class
CommonCommandResponse
public
partial
class
CommonCommandResponse
{
{
public
static
implicit
operator
CommonCommandResponse
(
CompleteResult
domainHandleResult
)
public
static
CommonCommandResponse
SuccessResponse
=
new
CommonCommandResponse
(
)
{
{
return
new
CommonCommandResponse
{
Status
=
domainHandleResult
.
Status
,
Message
=
domainHandleResult
.
Message
};
Message
=
"执行成功"
,
}
Status
=
1
};
}
}
}
}
src/Pole.Grpc/PoleGrpcOptionExtensions.cs
deleted
100644 → 0
View file @
06f7c1ca
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
Pole.Application
;
using
Pole.Domain
;
namespace
Pole.Grpc
{
public
static
class
PoleGrpcOptionExtensions
{
public
static
PoleGrpcOptions
AddPoleApplication
(
this
PoleGrpcOptions
poleGrpcOptions
)
{
poleGrpcOptions
.
Services
.
AddPoleApplication
(
options
=>
{
options
.
AutoInjectionCommandHandlersAndDomainEventHandlers
();
if
(
poleGrpcOptions
.
AutoInject
)
{
options
.
AutoInjectionDependency
();
}
},
poleGrpcOptions
.
ApplicationAssemblies
.
ToArray
());
return
poleGrpcOptions
;
}
public
static
PoleGrpcOptions
AddPoleDomain
(
this
PoleGrpcOptions
poleGrpcOptions
)
{
poleGrpcOptions
.
Services
.
AddPoleDomain
();
return
poleGrpcOptions
;
}
}
}
src/Pole.Grpc/ServiceCollectionExtensions.cs
View file @
5495256f
...
@@ -9,13 +9,14 @@ namespace Microsoft.Extensions.DependencyInjection
...
@@ -9,13 +9,14 @@ namespace Microsoft.Extensions.DependencyInjection
{
{
public
static
class
ServiceCollectionExtensions
public
static
class
ServiceCollectionExtensions
{
{
public
static
IServiceCollection
AddPoleGrpc
(
this
IServiceCollection
services
,
params
Assembly
[]
assemblies
)
//public static IServiceCollection AddPoleGrpc(this IServiceCollection services, params Assembly[] assemblies)
{
//{
PoleGrpcOptions
poleGrpcOptions
=
new
PoleGrpcOptions
(
services
,
assemblies
);
// // PoleGrpcOptions poleGrpcOptions = new PoleGrpcOptions(services, assemblies);
poleGrpcOptions
.
AddPoleApplication
();
poleGrpcOptions
.
AddPoleDomain
();
// //poleGrpcOptions.Services.AddGrpcValidation();
// poleGrpcOptions.Services.AddGrpcValidation();
return
services
;
// // poleGrpcOptions.Services.addv
}
// return services;
//}
}
}
}
}
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