Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
netcoreplus
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
netcoreplus
Commits
bba639d0
Commit
bba639d0
authored
May 16, 2019
by
阿星Plus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go to bed
parent
42bb457c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
345 additions
and
8 deletions
+345
-8
NullUnitOfWork.cs
src/Plus/Domain/Uow/NullUnitOfWork.cs
+10
-0
UnitOfWorkBase.cs
src/Plus/Domain/Uow/UnitOfWorkBase.cs
+324
-0
PlusLeadershipModule.cs
src/Plus/PlusLeadershipModule.cs
+11
-8
No files found.
src/Plus/Domain/Uow/NullUnitOfWork.cs
0 → 100644
View file @
bba639d0
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
Plus.Domain.Uow
{
public
sealed
class
NullUnitOfWork
:
UnitOfWorkBase
{
}
}
\ No newline at end of file
src/Plus/Domain/Uow/UnitOfWorkBase.cs
0 → 100644
View file @
bba639d0
using
Castle.Core
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Immutable
;
using
System.Linq
;
using
System.Threading.Tasks
;
namespace
Plus.Domain.Uow
{
/// <summary>
/// 工作单元基类
/// </summary>
public
abstract
class
UnitOfWorkBase
:
IUnitOfWork
,
IActiveUnitOfWork
,
IUnitOfWorkCompleteHandle
,
IDisposable
{
private
readonly
List
<
DataFilterConfiguration
>
_filters
;
private
bool
_isBeginCalledBefore
;
private
bool
_isCompleteCalledBefore
;
private
bool
_succeed
;
private
Exception
_exception
;
public
string
Id
{
get
;
}
[
DoNotWire
]
public
IUnitOfWork
Outer
{
get
;
set
;
}
public
UnitOfWorkOptions
Options
{
get
;
private
set
;
}
public
IReadOnlyList
<
DataFilterConfiguration
>
Filters
=>
_filters
.
ToImmutableList
();
public
Dictionary
<
string
,
object
>
Items
{
get
;
set
;
}
protected
IUnitOfWorkDefaultOptions
DefaultOptions
{
get
;
}
protected
IConnectionStringResolver
ConnectionStringResolver
{
get
;
}
public
bool
IsDisposed
{
get
;
private
set
;
}
protected
IUnitOfWorkFilterExecuter
FilterExecuter
{
get
;
}
public
event
EventHandler
Completed
;
public
event
EventHandler
<
UnitOfWorkFailedEventArgs
>
Failed
;
public
event
EventHandler
Disposed
;
protected
UnitOfWorkBase
(
IConnectionStringResolver
connectionStringResolver
,
IUnitOfWorkDefaultOptions
defaultOptions
,
IUnitOfWorkFilterExecuter
filterExecuter
)
{
FilterExecuter
=
filterExecuter
;
DefaultOptions
=
defaultOptions
;
ConnectionStringResolver
=
connectionStringResolver
;
Id
=
Guid
.
NewGuid
().
ToString
(
"N"
);
_filters
=
defaultOptions
.
Filters
.
ToList
();
Items
=
new
Dictionary
<
string
,
object
>();
}
public
void
Begin
(
UnitOfWorkOptions
options
)
{
PreventMultipleBegin
();
Options
=
options
;
SetFilters
(
options
.
FilterOverrides
);
BeginUow
();
}
public
abstract
void
SaveChanges
();
public
abstract
Task
SaveChangesAsync
();
public
IDisposable
DisableFilter
(
params
string
[]
filterNames
)
{
List
<
string
>
disabledFilters
=
new
List
<
string
>();
foreach
(
string
text
in
filterNames
)
{
int
filterIndex
=
GetFilterIndex
(
text
);
if
(
_filters
[
filterIndex
].
IsEnabled
)
{
disabledFilters
.
Add
(
text
);
_filters
[
filterIndex
]
=
new
DataFilterConfiguration
(
_filters
[
filterIndex
],
false
);
}
}
disabledFilters
.
ForEach
(
ApplyDisableFilter
);
return
new
DisposeAction
(
delegate
{
EnableFilter
(
disabledFilters
.
ToArray
());
});
}
public
IDisposable
EnableFilter
(
params
string
[]
filterNames
)
{
List
<
string
>
enabledFilters
=
new
List
<
string
>();
foreach
(
string
text
in
filterNames
)
{
int
filterIndex
=
GetFilterIndex
(
text
);
if
(!
_filters
[
filterIndex
].
IsEnabled
)
{
enabledFilters
.
Add
(
text
);
_filters
[
filterIndex
]
=
new
DataFilterConfiguration
(
_filters
[
filterIndex
],
true
);
}
}
enabledFilters
.
ForEach
(
ApplyEnableFilter
);
return
new
DisposeAction
(
delegate
{
DisableFilter
(
enabledFilters
.
ToArray
());
});
}
public
bool
IsFilterEnabled
(
string
filterName
)
{
return
GetFilter
(
filterName
).
IsEnabled
;
}
public
IDisposable
SetFilterParameter
(
string
filterName
,
string
parameterName
,
object
value
)
{
int
filterIndex
=
GetFilterIndex
(
filterName
);
DataFilterConfiguration
dataFilterConfiguration
=
new
DataFilterConfiguration
(
_filters
[
filterIndex
]);
object
oldValue
=
null
;
bool
hasOldValue
=
dataFilterConfiguration
.
FilterParameters
.
ContainsKey
(
parameterName
);
if
(
hasOldValue
)
{
oldValue
=
dataFilterConfiguration
.
FilterParameters
[
parameterName
];
}
dataFilterConfiguration
.
FilterParameters
[
parameterName
]
=
value
;
_filters
[
filterIndex
]
=
dataFilterConfiguration
;
ApplyFilterParameterValue
(
filterName
,
parameterName
,
value
);
return
new
DisposeAction
(
delegate
{
if
(
hasOldValue
)
{
SetFilterParameter
(
filterName
,
parameterName
,
oldValue
);
}
});
}
public
void
Complete
()
{
PreventMultipleComplete
();
try
{
CompleteUow
();
_succeed
=
true
;
OnCompleted
();
}
catch
(
Exception
exception
)
{
Exception
ex
=
_exception
=
exception
;
throw
;
}
}
public
async
Task
CompleteAsync
()
{
PreventMultipleComplete
();
try
{
await
CompleteUowAsync
();
_succeed
=
true
;
OnCompleted
();
}
catch
(
Exception
exception
)
{
Exception
ex
=
_exception
=
exception
;
throw
;
}
}
public
void
Dispose
()
{
if
(
_isBeginCalledBefore
&&
!
IsDisposed
)
{
IsDisposed
=
true
;
if
(!
_succeed
)
{
OnFailed
(
_exception
);
}
DisposeUow
();
OnDisposed
();
}
}
protected
virtual
void
BeginUow
()
{
}
protected
abstract
void
CompleteUow
();
protected
abstract
Task
CompleteUowAsync
();
protected
abstract
void
DisposeUow
();
protected
virtual
void
ApplyDisableFilter
(
string
filterName
)
{
FilterExecuter
.
ApplyDisableFilter
(
this
,
filterName
);
}
protected
virtual
void
ApplyEnableFilter
(
string
filterName
)
{
FilterExecuter
.
ApplyEnableFilter
(
this
,
filterName
);
}
protected
virtual
void
ApplyFilterParameterValue
(
string
filterName
,
string
parameterName
,
object
value
)
{
FilterExecuter
.
ApplyFilterParameterValue
(
this
,
filterName
,
parameterName
,
value
);
}
protected
virtual
string
ResolveConnectionString
(
ConnectionStringResolveArgs
args
)
{
return
ConnectionStringResolver
.
GetNameOrConnectionString
(
args
);
}
protected
virtual
void
OnCompleted
()
{
Extensions
.
InvokeSafely
(
this
.
Completed
,
(
object
)
this
);
}
protected
virtual
void
OnFailed
(
Exception
exception
)
{
Extensions
.
InvokeSafely
<
UnitOfWorkFailedEventArgs
>(
this
.
Failed
,
(
object
)
this
,
new
UnitOfWorkFailedEventArgs
(
exception
));
}
protected
virtual
void
OnDisposed
()
{
Extensions
.
InvokeSafely
(
this
.
Disposed
,
(
object
)
this
);
}
private
void
PreventMultipleBegin
()
{
if
(
_isBeginCalledBefore
)
{
throw
new
UPrimeException
(
"This unit of work has started before. Can not call Start method more than once."
);
}
_isBeginCalledBefore
=
true
;
}
private
void
PreventMultipleComplete
()
{
if
(
_isCompleteCalledBefore
)
{
throw
new
UPrimeException
(
"Complete is called before!"
);
}
_isCompleteCalledBefore
=
true
;
}
private
void
SetFilters
(
List
<
DataFilterConfiguration
>
filterOverrides
)
{
int
i
;
for
(
i
=
0
;
i
<
_filters
.
Count
;
i
++)
{
DataFilterConfiguration
dataFilterConfiguration
=
filterOverrides
.
FirstOrDefault
((
DataFilterConfiguration
f
)
=>
f
.
FilterName
==
_filters
[
i
].
FilterName
);
if
(
dataFilterConfiguration
!=
null
)
{
_filters
[
i
]
=
dataFilterConfiguration
;
}
}
}
private
void
ChangeFilterIsEnabledIfNotOverrided
(
List
<
DataFilterConfiguration
>
filterOverrides
,
string
filterName
,
bool
isEnabled
)
{
if
(!
filterOverrides
.
Any
((
DataFilterConfiguration
f
)
=>
f
.
FilterName
==
filterName
))
{
int
num
=
_filters
.
FindIndex
((
DataFilterConfiguration
f
)
=>
f
.
FilterName
==
filterName
);
if
(
num
>=
0
&&
_filters
[
num
].
IsEnabled
!=
isEnabled
)
{
_filters
[
num
]
=
new
DataFilterConfiguration
(
filterName
,
isEnabled
);
}
}
}
private
DataFilterConfiguration
GetFilter
(
string
filterName
)
{
DataFilterConfiguration
dataFilterConfiguration
=
_filters
.
FirstOrDefault
((
DataFilterConfiguration
f
)
=>
f
.
FilterName
==
filterName
);
if
(
dataFilterConfiguration
==
null
)
{
throw
new
UPrimeException
(
"Unknown filter name: "
+
filterName
+
". Be sure this filter is registered before."
);
}
return
dataFilterConfiguration
;
}
private
int
GetFilterIndex
(
string
filterName
)
{
int
num
=
_filters
.
FindIndex
((
DataFilterConfiguration
f
)
=>
f
.
FilterName
==
filterName
);
if
(
num
<
0
)
{
throw
new
UPrimeException
(
"Unknown filter name: "
+
filterName
+
". Be sure this filter is registered before."
);
}
return
num
;
}
public
override
string
ToString
()
{
return
"[UnitOfWork "
+
Id
+
"]"
;
}
}
}
\ No newline at end of file
src/Plus/PlusLeadershipModule.cs
View file @
bba639d0
using
Castle.MicroKernel.Registration
;
using
Castle.MicroKernel.Registration
;
using
Plus.Configuration.Startup
;
using
Plus.Configuration.Startup
;
using
Plus.Dependency
;
using
Plus.Dependency
;
using
Plus.Domain.Uow
;
using
Plus.Event.Bus
;
using
Plus.Event.Bus
;
using
Plus.Modules
;
using
Plus.Modules
;
using
System
;
using
System
;
...
@@ -9,6 +10,9 @@ using System.Linq.Expressions;
...
@@ -9,6 +10,9 @@ using System.Linq.Expressions;
namespace
Plus
namespace
Plus
{
{
/// <summary>
/// Plus Leadership Module
/// </summary>
public
class
PlusLeadershipModule
:
PlusModule
public
class
PlusLeadershipModule
:
PlusModule
{
{
public
override
void
PreInitialize
()
public
override
void
PreInitialize
()
...
@@ -52,14 +56,14 @@ namespace Plus
...
@@ -52,14 +56,14 @@ namespace Plus
);
);
}
}
//
IocManager.RegisterIfNot<IUnitOfWork, NullUnitOfWork>(DependencyLifeStyle.Transient);
IocManager
.
RegisterIfNot
<
IUnitOfWork
,
NullUnitOfWork
>(
DependencyLifeStyle
.
Transient
);
//
IocManager.RegisterIfNot<IUnitOfWorkFilterExecuter, NullUnitOfWorkFilterExecuter>();
IocManager
.
RegisterIfNot
<
IUnitOfWorkFilterExecuter
,
NullUnitOfWorkFilterExecuter
>();
}
}
private
void
AddMethodParameterValidators
()
private
void
AddMethodParameterValidators
()
{
{
//
Configuration.Validation.Validators.Add<DataAnnotationsValidator>();
Configuration
.
Validation
.
Validators
.
Add
<
DataAnnotationsValidator
>();
//
Configuration.Validation.Validators.Add<ValidatableObjectValidator>();
Configuration
.
Validation
.
Validators
.
Add
<
ValidatableObjectValidator
>();
}
}
private
void
AddIgnoredTypes
()
private
void
AddIgnoredTypes
()
...
@@ -72,20 +76,19 @@ namespace Plus
...
@@ -72,20 +76,19 @@ namespace Plus
foreach
(
var
ignoredType
in
commonIgnoredTypes
)
foreach
(
var
ignoredType
in
commonIgnoredTypes
)
{
{
//Configuration.Auditing.IgnoredTypes.AddIfNotContains(ignoredType);
Configuration
.
Validation
.
IgnoredTypes
.
AddIfNotContains
(
ignoredType
);
//Configuration.Validation.IgnoredTypes.AddIfNotContains(ignoredType);
}
}
var
validationIgnoredTypes
=
new
[]
{
typeof
(
Type
)
};
var
validationIgnoredTypes
=
new
[]
{
typeof
(
Type
)
};
foreach
(
var
ignoredType
in
validationIgnoredTypes
)
foreach
(
var
ignoredType
in
validationIgnoredTypes
)
{
{
//
Configuration.Validation.IgnoredTypes.AddIfNotContains(ignoredType);
Configuration
.
Validation
.
IgnoredTypes
.
AddIfNotContains
(
ignoredType
);
}
}
}
}
private
void
ConfigureCaches
()
private
void
ConfigureCaches
()
{
{
}
}
}
}
}
}
\ No newline at end of file
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