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
59e9635d
Commit
59e9635d
authored
May 14, 2019
by
阿星Plus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
oook , PlusEngine 批量依赖注入 IOC管理等等
parent
71edd900
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
490 additions
and
85 deletions
+490
-85
Extensions.cs
src/Plus/Collections/Extensions.cs
+143
-0
DictionaryBasedConfig.cs
src/Plus/Configuration/DictionaryBasedConfig.cs
+68
-0
IDictionaryBasedConfig.cs
src/Plus/Configuration/IDictionaryBasedConfig.cs
+19
-0
IPlusStartupConfiguration.cs
src/Plus/Configuration/Startup/IPlusStartupConfiguration.cs
+30
-0
PlusStartupConfiguration.cs
src/Plus/Configuration/Startup/PlusStartupConfiguration.cs
+41
-0
ConventionalRegistrationConfig.cs
src/Plus/Dependency/ConventionalRegistrationConfig.cs
+0
-23
IConventionalRegistrationContext.cs
src/Plus/Dependency/IConventionalRegistrationContext.cs
+0
-25
IocManager.cs
src/Plus/Dependency/IocManager.cs
+1
-22
RegistrationContext.cs
src/Plus/Dependency/RegistrationContext.cs
+5
-6
Plus.csproj
src/Plus/Plus.csproj
+6
-0
PlusEngine.cs
src/Plus/PlusEngine.cs
+49
-4
PlusLeadershipModule.cs
src/Plus/PlusLeadershipModule.cs
+12
-0
PlusStarter.cs
src/Plus/PlusStarter.cs
+102
-5
PlusStarterOptions.cs
src/Plus/PlusStarterOptions.cs
+14
-0
No files found.
src/Plus/Collections/Extensions.cs
0 → 100644
View file @
59e9635d
using
System
;
using
System.Collections.Generic
;
namespace
Plus.Collections
{
/// <summary>
/// 扩展方法,后面要拿出来单独放一个项目中
/// </summary>
public
static
class
Extensions
{
public
static
List
<
T
>
SortByDependencies
<
T
>(
this
IEnumerable
<
T
>
source
,
Func
<
T
,
IEnumerable
<
T
>>
getDependencies
)
{
var
sorted
=
new
List
<
T
>();
var
visited
=
new
Dictionary
<
T
,
bool
>();
foreach
(
var
item
in
source
)
{
SortByDependenciesVisit
(
item
,
getDependencies
,
sorted
,
visited
);
}
return
sorted
;
}
private
static
void
SortByDependenciesVisit
<
T
>(
T
item
,
Func
<
T
,
IEnumerable
<
T
>>
getDependencies
,
List
<
T
>
sorted
,
Dictionary
<
T
,
bool
>
visited
)
{
var
alreadyVisited
=
visited
.
TryGetValue
(
item
,
out
bool
inProcess
);
if
(
alreadyVisited
)
{
if
(
inProcess
)
{
throw
new
ArgumentException
(
"Cyclic dependency found! Item: "
+
item
);
}
}
else
{
visited
[
item
]
=
true
;
var
dependencies
=
getDependencies
(
item
);
if
(
dependencies
!=
null
)
{
foreach
(
var
dependency
in
dependencies
)
{
SortByDependenciesVisit
(
dependency
,
getDependencies
,
sorted
,
visited
);
}
}
visited
[
item
]
=
false
;
sorted
.
Add
(
item
);
}
}
public
static
bool
IsNullOrEmpty
<
T
>(
this
ICollection
<
T
>
source
)
{
return
source
==
null
||
source
.
Count
<=
0
;
}
public
static
bool
AddIfNotContains
<
T
>(
this
ICollection
<
T
>
source
,
T
item
)
{
if
(
source
==
null
)
{
throw
new
ArgumentNullException
(
"source"
);
}
if
(
source
.
Contains
(
item
))
{
return
false
;
}
source
.
Add
(
item
);
return
true
;
}
/// <summary>
/// This method is used to try to get a value in a dictionary if it does exists.
/// </summary>
/// <typeparam name="T">Type of the value</typeparam>
/// <param name="dictionary">The collection object</param>
/// <param name="key">Key</param>
/// <param name="value">Value of the key (or default value if key not exists)</param>
/// <returns>True if key does exists in the dictionary</returns>
internal
static
bool
TryGetValue
<
T
>(
this
IDictionary
<
string
,
object
>
dictionary
,
string
key
,
out
T
value
)
{
object
valueObj
;
if
(
dictionary
.
TryGetValue
(
key
,
out
valueObj
)
&&
valueObj
is
T
)
{
value
=
(
T
)
valueObj
;
return
true
;
}
value
=
default
(
T
);
return
false
;
}
/// <summary>
/// Gets a value from the dictionary with given key. Returns default value if can not find.
/// </summary>
/// <param name="dictionary">Dictionary to check and get</param>
/// <param name="key">Key to find the value</param>
/// <typeparam name="TKey">Type of the key</typeparam>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <returns>Value if found, default if can not found.</returns>
public
static
TValue
GetOrDefault
<
TKey
,
TValue
>(
this
IDictionary
<
TKey
,
TValue
>
dictionary
,
TKey
key
)
{
TValue
obj
;
return
dictionary
.
TryGetValue
(
key
,
out
obj
)
?
obj
:
default
(
TValue
);
}
/// <summary>
/// Gets a value from the dictionary with given key. Returns default value if can not find.
/// </summary>
/// <param name="dictionary">Dictionary to check and get</param>
/// <param name="key">Key to find the value</param>
/// <param name="factory">A factory method used to create the value if not found in the dictionary</param>
/// <typeparam name="TKey">Type of the key</typeparam>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <returns>Value if found, default if can not found.</returns>
public
static
TValue
GetOrAdd
<
TKey
,
TValue
>(
this
IDictionary
<
TKey
,
TValue
>
dictionary
,
TKey
key
,
Func
<
TKey
,
TValue
>
factory
)
{
TValue
obj
;
if
(
dictionary
.
TryGetValue
(
key
,
out
obj
))
{
return
obj
;
}
return
dictionary
[
key
]
=
factory
(
key
);
}
/// <summary>
/// Gets a value from the dictionary with given key. Returns default value if can not find.
/// </summary>
/// <param name="dictionary">Dictionary to check and get</param>
/// <param name="key">Key to find the value</param>
/// <param name="factory">A factory method used to create the value if not found in the dictionary</param>
/// <typeparam name="TKey">Type of the key</typeparam>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <returns>Value if found, default if can not found.</returns>
public
static
TValue
GetOrAdd
<
TKey
,
TValue
>(
this
IDictionary
<
TKey
,
TValue
>
dictionary
,
TKey
key
,
Func
<
TValue
>
factory
)
{
return
dictionary
.
GetOrAdd
(
key
,
k
=>
factory
());
}
}
}
\ No newline at end of file
src/Plus/Configuration/DictionaryBasedConfig.cs
0 → 100644
View file @
59e9635d
using
Plus.Collections
;
using
System
;
using
System.Collections.Generic
;
namespace
Plus.Configuration
{
public
class
DictionaryBasedConfig
:
IDictionaryBasedConfig
{
protected
Dictionary
<
string
,
object
>
CustomSettings
{
get
;
private
set
;
}
public
object
this
[
string
name
]
{
get
{
return
CustomSettings
.
GetOrDefault
(
name
);
}
set
{
CustomSettings
[
name
]
=
value
;
}
}
protected
DictionaryBasedConfig
()
{
CustomSettings
=
new
Dictionary
<
string
,
object
>();
}
public
T
Get
<
T
>(
string
name
)
{
object
obj
=
this
[
name
];
return
(
obj
==
null
)
?
default
(
T
)
:
((
T
)
Convert
.
ChangeType
(
obj
,
typeof
(
T
)));
}
public
void
Set
<
T
>(
string
name
,
T
value
)
{
this
[
name
]
=
value
;
}
public
object
Get
(
string
name
)
{
return
Get
(
name
,
null
);
}
public
object
Get
(
string
name
,
object
defaultValue
)
{
object
obj
=
this
[
name
];
if
(
obj
==
null
)
{
return
defaultValue
;
}
return
this
[
name
];
}
public
T
Get
<
T
>(
string
name
,
T
defaultValue
)
{
return
(
T
)
Get
(
name
,
(
object
)
defaultValue
);
}
public
T
GetOrCreate
<
T
>(
string
name
,
Func
<
T
>
creator
)
{
object
obj
=
Get
(
name
);
if
(
obj
==
null
)
{
obj
=
creator
();
Set
(
name
,
obj
);
}
return
(
T
)
obj
;
}
}
}
\ No newline at end of file
src/Plus/Configuration/IDictionaryBasedConfig.cs
0 → 100644
View file @
59e9635d
using
System
;
namespace
Plus.Configuration
{
public
interface
IDictionaryBasedConfig
{
void
Set
<
T
>(
string
name
,
T
value
);
object
Get
(
string
name
);
T
Get
<
T
>(
string
name
);
object
Get
(
string
name
,
object
defaultValue
);
T
Get
<
T
>(
string
name
,
T
defaultValue
);
T
GetOrCreate
<
T
>(
string
name
,
Func
<
T
>
creator
);
}
}
\ No newline at end of file
src/Plus/Configuration/Startup/IPlusStartupConfiguration.cs
0 → 100644
View file @
59e9635d
using
Plus.Dependency
;
using
System
;
namespace
Plus.Configuration.Startup
{
/// <summary>
/// 在启动时的模块配置
/// </summary>
public
interface
IPlusStartupConfiguration
{
/// <summary>
/// 获取与此配置关联的IOC管理器
/// </summary>
IIocManager
IocManager
{
get
;
}
/// <summary>
/// 用于替换服务类型
/// </summary>
/// <param name="type"></param>
/// <param name="replaceAction"></param>
void
ReplaceService
(
Type
type
,
Action
replaceAction
);
/// <summary>
/// 获取配置对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T
Get
<
T
>();
}
}
\ No newline at end of file
src/Plus/Configuration/Startup/PlusStartupConfiguration.cs
0 → 100644
View file @
59e9635d
using
Plus.Dependency
;
using
System
;
using
System.Collections.Generic
;
namespace
Plus.Configuration.Startup
{
public
class
PlusStartupConfiguration
:
DictionaryBasedConfig
,
IPlusStartupConfiguration
,
IDictionaryBasedConfig
{
public
IIocManager
IocManager
{
get
;
private
set
;
}
public
Dictionary
<
Type
,
Action
>
ServiceReplaceActions
{
get
;
private
set
;
}
public
PlusStartupConfiguration
(
IIocManager
iocManager
)
{
IocManager
=
iocManager
;
}
public
void
Initialize
()
{
ServiceReplaceActions
=
new
Dictionary
<
Type
,
Action
>();
}
public
void
ReplaceService
(
Type
type
,
Action
replaceAction
)
{
ServiceReplaceActions
[
type
]
=
replaceAction
;
}
public
T
Get
<
T
>()
{
return
GetOrCreate
(
typeof
(
T
).
FullName
,
()
=>
IocManager
.
Resolve
<
T
>());
}
}
}
\ No newline at end of file
src/Plus/Dependency/ConventionalRegistrationConfig.cs
deleted
100644 → 0
View file @
71edd900
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
Plus.Dependency
{
public
class
ConventionalRegistrationConfig
:
DictionaryBasedConfig
{
/// <summary>
/// Install all <see cref="IInterceptor"/> implementations automatically or not.
/// Default: true.
/// </summary>
public
bool
InstallInstallers
{
get
;
set
;
}
/// <summary>
/// Creates a new <see cref="ConventionalRegistrationConfig"/> object.
/// </summary>
public
ConventionalRegistrationConfig
()
{
InstallInstallers
=
true
;
}
}
}
\ No newline at end of file
src/Plus/Dependency/IConventionalRegistrationContext.cs
deleted
100644 → 0
View file @
71edd900
using
System
;
using
System.Collections.Generic
;
using
System.Reflection
;
using
System.Text
;
namespace
Plus.Dependency
{
public
interface
IConventionalRegistrationContext
{
/// <summary>
/// Gets the registering Assembly.
/// </summary>
Assembly
Assembly
{
get
;
}
/// <summary>
/// Reference to the IOC Container to register types.
/// </summary>
IIocManager
IocManager
{
get
;
}
/// <summary>
/// Registration configuration.
/// </summary>
ConventionalRegistrationConfig
Config
{
get
;
}
}
}
\ No newline at end of file
src/Plus/Dependency/IocManager.cs
View file @
59e9635d
...
...
@@ -4,7 +4,6 @@ using System;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Reflection
;
using
System.Text
;
namespace
Plus.Dependency
{
...
...
@@ -39,7 +38,7 @@ namespace Plus.Dependency
public
void
RegisterAssembly
(
Assembly
assembly
)
{
ConventionalRegistrationContext
context
=
new
Conventional
RegistrationContext
(
assembly
,
this
);
var
context
=
new
RegistrationContext
(
assembly
,
this
);
foreach
(
IDependencyRegistrar
conventionalRegistrar
in
_conventionalRegistrars
)
{
conventionalRegistrar
.
RegisterAssembly
(
context
);
...
...
@@ -91,36 +90,16 @@ namespace Plus.Dependency
return
IocContainer
.
Resolve
(
type
);
}
//public object Resolve(Type type, object argumentsAsAnonymousType)
//{
// return IocContainer.Resolve(type, argumentsAsAnonymousType);
//}
//public T Resolve<T>(object argumentsAsAnonymousType)
//{
// return IocContainer.Resolve<T>(argumentsAsAnonymousType);
//}
public
T
[]
ResolveAll
<
T
>()
{
return
IocContainer
.
ResolveAll
<
T
>();
}
//public T[] ResolveAll<T>(object argumentsAsAnonymousType)
//{
// return IocContainer.ResolveAll<T>(argumentsAsAnonymousType);
//}
public
object
[]
ResolveAll
(
Type
type
)
{
return
IocContainer
.
ResolveAll
(
type
).
Cast
<
object
>().
ToArray
();
}
//public object[] ResolveAll(Type type, object argumentsAsAnonymousType)
//{
// return IocContainer.ResolveAll(type, argumentsAsAnonymousType).Cast<object>().ToArray();
//}
public
void
Release
(
object
obj
)
{
IocContainer
.
Release
(
obj
);
...
...
src/Plus/Dependency/
Conventional
RegistrationContext.cs
→
src/Plus/Dependency/RegistrationContext.cs
View file @
59e9635d
...
...
@@ -2,7 +2,7 @@
namespace
Plus.Dependency
{
internal
class
ConventionalRegistrationContext
:
IConventional
RegistrationContext
internal
class
RegistrationContext
:
I
RegistrationContext
{
/// <summary>
/// Gets the registering Assembly.
...
...
@@ -15,15 +15,14 @@ namespace Plus.Dependency
public
IIocManager
IocManager
{
get
;
private
set
;
}
/// <summary>
///
Registration configuration.
///
/// </summary>
public
ConventionalRegistrationConfig
Config
{
get
;
private
set
;
}
internal
ConventionalRegistrationContext
(
Assembly
assembly
,
IIocManager
iocManager
,
ConventionalRegistrationConfig
config
)
/// <param name="assembly"></param>
/// <param name="iocManager"></param>
internal
RegistrationContext
(
Assembly
assembly
,
IIocManager
iocManager
)
{
Assembly
=
assembly
;
IocManager
=
iocManager
;
Config
=
config
;
}
}
}
\ No newline at end of file
src/Plus/Plus.csproj
View file @
59e9635d
...
...
@@ -4,4 +4,10 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.4.0" />
<PackageReference Include="Castle.Windsor" Version="5.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
</ItemGroup>
</Project>
src/Plus/PlusEngine.cs
View file @
59e9635d
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
Castle.Core.Logging
;
using
Plus.Dependency
;
namespace
Plus
{
public
class
PlusEngine
{
private
readonly
bool
_initialized
=
false
;
private
bool
_initialized
=
false
;
public
IIocManager
IocManager
{
get
;
private
set
;
}
public
ILogger
Logger
{
get
;
private
set
;
}
public
static
PlusEngine
Instance
{
get
;
private
set
;
}
static
PlusEngine
()
{
Instance
=
new
PlusEngine
();
}
public
void
Initialize
(
IIocManager
iocManage
)
{
if
(!
_initialized
)
{
Logger
=
NullLogger
.
Instance
;
IocManager
=
iocManage
;
_initialized
=
true
;
return
;
}
throw
new
PlusException
(
"PlusEngine 初始化"
);
}
public
void
PostInitialize
()
{
if
(
_initialized
)
{
}
}
public
void
Register
<
T
>(
DependencyLifeStyle
lifeStyle
=
DependencyLifeStyle
.
Singleton
)
where
T
:
class
{
IocManager
.
Register
(
typeof
(
T
),
typeof
(
T
),
lifeStyle
);
}
public
void
Register
<
TType
,
TImpl
>(
DependencyLifeStyle
lifeStyle
=
DependencyLifeStyle
.
Singleton
)
{
IocManager
.
Register
(
typeof
(
TType
),
typeof
(
TImpl
),
lifeStyle
);
}
public
T
Resolve
<
T
>()
where
T
:
class
{
return
IocManager
.
Resolve
<
T
>();
}
}
}
\ No newline at end of file
src/Plus/PlusLeadershipModule.cs
0 → 100644
View file @
59e9635d
using
Plus.Modules
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
Plus
{
public
class
PlusLeadershipModule
:
PlusModule
{
}
}
\ No newline at end of file
src/Plus/PlusStarter.cs
View file @
59e9635d
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
Castle.Core.Logging
;
using
Castle.MicroKernel.Registration
;
using
Plus.Configuration.Startup
;
using
Plus.Dependency
;
using
Plus.Dependency.Installers
;
using
Plus.Modules
;
using
System
;
using
System.Reflection
;
namespace
Plus
{
public
class
PlusStarter
:
IDisposable
{
public
void
Dispose
()
protected
bool
IsDisposed
;
private
ILogger
_logger
;
private
IPlusModuleManager
_moduleManager
;
public
Type
StartupModule
{
get
;
}
public
IIocManager
IocManager
{
get
;
}
private
PlusStarter
(
Type
startupModule
,
Action
<
PlusStarterOptions
>
optionsAction
=
null
)
{
PlusStarterOptions
uPrimeStarterOptions
=
new
PlusStarterOptions
();
optionsAction
?.
Invoke
(
uPrimeStarterOptions
);
if
(!((
TypeInfo
)
typeof
(
PlusModule
)).
IsAssignableFrom
(
startupModule
))
{
throw
new
ArgumentException
(
"startupModule should be derived from PlusModule."
);
}
StartupModule
=
startupModule
;
IocManager
=
uPrimeStarterOptions
.
IocManager
;
_logger
=
NullLogger
.
Instance
;
AddInterceptorRegistrars
();
}
public
static
PlusStarter
Create
<
TStartupModule
>(
Action
<
PlusStarterOptions
>
optionsAction
=
null
)
where
TStartupModule
:
PlusModule
{
return
new
PlusStarter
(
typeof
(
TStartupModule
),
optionsAction
);
}
public
static
PlusStarter
Create
(
Type
startupModule
,
Action
<
PlusStarterOptions
>
optionsAction
=
null
)
{
return
new
PlusStarter
(
startupModule
,
optionsAction
);
}
public
virtual
void
Initialize
()
{
PlusEngine
.
Instance
.
Initialize
(
IocManager
);
ResolveLogger
();
try
{
RegisterStarter
();
_logger
.
Debug
(
"PlusStarter 初始化."
);
PlusEngine
.
Instance
.
IocManager
.
IocContainer
.
Install
(
new
PlusCoreInstaller
());
PlusEngine
.
Instance
.
PostInitialize
();
IocManager
.
Resolve
<
PlusStartupConfiguration
>().
Initialize
();
_moduleManager
=
PlusEngine
.
Instance
.
Resolve
<
PlusModuleManager
>();
_moduleManager
.
Initialize
(
StartupModule
);
_moduleManager
.
StartModules
();
_logger
.
Debug
(
"PlusStarter 初始化完成."
);
}
catch
(
Exception
ex
)
{
_logger
.
Fatal
(
ex
.
ToString
(),
ex
);
throw
;
}
}
public
virtual
void
Dispose
()
{
if
(!
IsDisposed
)
{
IsDisposed
=
true
;
_moduleManager
?.
ShutdownModules
();
}
}
private
void
ResolveLogger
()
{
if
(
IocManager
.
IsRegistered
<
ILoggerFactory
>())
{
_logger
=
IocManager
.
Resolve
<
ILoggerFactory
>().
Create
(
typeof
(
PlusStarter
));
}
}
private
void
RegisterStarter
()
{
if
(!
IocManager
.
IsRegistered
<
PlusStarter
>())
{
IocManager
.
IocContainer
.
Register
((
IRegistration
[])
new
IRegistration
[
1
]
{
Component
.
For
<
PlusStarter
>().
Instance
(
this
)
});
}
}
public
void
AddInterceptorRegistrars
()
{
throw
new
NotImplementedException
();
//ValidationInterceptorRegistrar.Initialize(IocManager);
//UnitOfWorkRegistrar.Initialize(IocManager);
}
}
}
\ No newline at end of file
src/Plus/PlusStarterOptions.cs
0 → 100644
View file @
59e9635d
using
Plus.Dependency
;
namespace
Plus
{
public
class
PlusStarterOptions
{
public
IIocManager
IocManager
{
get
;
set
;
}
public
PlusStarterOptions
()
{
IocManager
=
Dependency
.
IocManager
.
Instance
;
}
}
}
\ 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