Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
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
CAP
Commits
de1715b7
Commit
de1715b7
authored
May 29, 2017
by
yangxiaodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modify exensions
parent
6cd22867
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
27 deletions
+18
-27
ConsistencyEntityFrameworkBuilderExtensions.cs
...meworkCore/ConsistencyEntityFrameworkBuilderExtensions.cs
+18
-27
No files found.
src/Cap.Consistency.EntityFrameworkCore/ConsistencyEntityFrameworkBuilderExtensions.cs
View file @
de1715b7
using
System
;
using
System
;
using
System.Reflection
;
using
Cap.Consistency
;
using
Cap.Consistency.EntityFrameworkCore
;
using
Cap.Consistency.Infrastructure
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.DependencyInjection.Extensions
;
namespace
Cap.Consistency.EntityFrameworkCore
namespace
Microsoft.Extensions.DependencyInjection
{
{
/// <summary>
/// <summary>
/// Contains extension methods to <see cref="ConsistencyBuilder"/> for adding entity framework stores.
/// Contains extension methods to <see cref="ConsistencyBuilder"/> for adding entity framework stores.
...
@@ -18,34 +20,23 @@ namespace Cap.Consistency.EntityFrameworkCore
...
@@ -18,34 +20,23 @@ namespace Cap.Consistency.EntityFrameworkCore
/// <returns>The <see cref="ConsistencyBuilder"/> instance this method extends.</returns>
/// <returns>The <see cref="ConsistencyBuilder"/> instance this method extends.</returns>
public
static
ConsistencyBuilder
AddEntityFrameworkStores
<
TContext
>(
this
ConsistencyBuilder
builder
)
public
static
ConsistencyBuilder
AddEntityFrameworkStores
<
TContext
>(
this
ConsistencyBuilder
builder
)
where
TContext
:
DbContext
{
where
TContext
:
DbContext
{
builder
.
Services
.
TryAdd
(
GetDefaultServices
(
builder
.
MessageType
,
typeof
(
TContext
)));
return
builder
;
}
/// <summary>
builder
.
Services
.
AddScoped
(
typeof
(
IConsistencyMessageStore
<>).
MakeGenericType
(
builder
.
MessageType
),
/// Adds an Entity Framework implementation of message stores.
typeof
(
ConsistencyMessageStore
<,>).
MakeGenericType
(
typeof
(
ConsistencyMessage
),
typeof
(
TContext
)));
/// </summary>
/// <typeparam name="TContext">The Entity Framework database context to use.</typeparam>
/// <typeparam name="TKey">The type of the primary key used for the users and roles.</typeparam>
/// <param name="builder">The <see cref="ConsistencyBuilder"/> instance this method extends.</param>
/// <returns>The <see cref="ConsistencyBuilder"/> instance this method extends.</returns>
public
static
ConsistencyBuilder
AddEntityFrameworkStores
<
TContext
,
TKey
>(
this
ConsistencyBuilder
builder
)
where
TContext
:
DbContext
where
TKey
:
IEquatable
<
TKey
>
{
builder
.
Services
.
TryAdd
(
GetDefaultServices
(
builder
.
MessageType
,
typeof
(
TContext
),
typeof
(
TKey
)));
return
builder
;
return
builder
;
}
}
private
static
IServiceCollection
GetDefaultServices
(
Type
messageType
,
Type
contextType
,
Type
keyType
=
null
)
{
private
static
TypeInfo
FindGenericBaseType
(
Type
currentType
,
Type
genericBaseType
)
{
Type
messageStoreType
;
var
type
=
currentType
.
GetTypeInfo
()
;
keyType
=
keyType
??
typeof
(
string
);
while
(
type
.
BaseType
!=
null
)
{
messageStoreType
=
typeof
(
IConsistencyMessageStore
<>).
MakeGenericType
(
messageType
,
contextType
,
keyType
);
type
=
type
.
BaseType
.
GetTypeInfo
(
);
var
genericType
=
type
.
IsGenericType
?
type
.
GetGenericTypeDefinition
()
:
null
;
var
services
=
new
ServiceCollection
();
if
(
genericType
!=
null
&&
genericType
==
genericBaseType
)
{
services
.
AddScoped
(
return
type
;
typeof
(
IConsistencyMessageStore
<>).
MakeGenericType
(
messageStoreType
),
}
messageStoreType
);
}
return
services
;
return
null
;
}
}
}
}
}
}
\ 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