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
3c7acca0
Commit
3c7acca0
authored
Jul 19, 2019
by
阿星Plus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MongoDb support
parent
b4171fc1
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
155 additions
and
43 deletions
+155
-43
IMongoDatabaseProvider.cs
src/Plus.MongoDB/IMongoDatabaseProvider.cs
+4
-5
IntIdGeneratorBase.cs
src/Plus.MongoDB/IdGenerator/IntIdGeneratorBase.cs
+30
-0
Plus.MongoDb.csproj
src/Plus.MongoDB/Plus.MongoDb.csproj
+41
-0
PlusMongoDbModule.cs
src/Plus.MongoDB/PlusMongoDbModule.cs
+2
-0
MongoDbRepositoryBase.cs
src/Plus.MongoDB/Repositories/MongoDbRepositoryBase.cs
+68
-25
UnitOfWorkMongoDatabaseProvider.cs
src/Plus.MongoDB/Uow/UnitOfWorkMongoDatabaseProvider.cs
+10
-13
No files found.
src/Plus.MongoDB/IMongoDatabaseProvider.cs
View file @
3c7acca0
...
...
@@ -3,13 +3,12 @@
namespace
Plus.MongoDb
{
/// <summary>
///
Defines interface to obtain a <see cref="MongoDatabase"/> object.
///
IMongoDatabaseProvider
/// </summary>
public
interface
IMongoDatabaseProvider
{
/// <summary>
/// Gets the <see cref="MongoDatabase"/>.
/// </summary>
MongoDatabase
Database
{
get
;
}
IMongoClient
Client
{
get
;
}
IMongoDatabase
Database
{
get
;
}
}
}
\ No newline at end of file
src/Plus.MongoDB/IdGenerator/IntIdGeneratorBase.cs
0 → 100644
View file @
3c7acca0
using
MongoDB.Bson.Serialization
;
using
System
;
namespace
Plus.MongoDb.IdGenerator
{
public
abstract
class
IntIdGeneratorBase
:
IIdGenerator
{
private
readonly
string
_idCollectionName
;
protected
IntIdGeneratorBase
(
string
idCollectionName
)
{
_idCollectionName
=
idCollectionName
;
}
protected
IntIdGeneratorBase
()
:
this
(
"IDs"
)
{
}
public
object
GenerateId
(
object
container
,
object
document
)
{
throw
new
NotImplementedException
();
}
public
bool
IsEmpty
(
object
id
)
{
throw
new
NotImplementedException
();
}
}
}
\ No newline at end of file
src/Plus.MongoDB/Plus.MongoDb.csproj
0 → 100644
View file @
3c7acca0
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Copyright>https://meowv.com</Copyright>
<Authors>阿星Plus</Authors>
<Company>https://meowv.com</Company>
<Product>Plus.MongoDb</Product>
<Title>Plus.MongoDb - .netcoreplus</Title>
<Description>.netcoreplus(Plus.MongoDb) - .NET Core 简易版快速开发框架</Description>
<RepositoryUrl>https://github.com/Meowv/.netcoreplus</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/Meowv/.netcoreplus</PackageProjectUrl>
<PackageIconUrl>https://avatars2.githubusercontent.com/u/13010050</PackageIconUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>plus;plus.mongodb;.netcoreplus;</PackageTags>
<PackageReleaseNotes>Plus.MongoDb</PackageReleaseNotes>
<Version>1.0.3</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1591</NoWarn>
<DocumentationFile>Plus.MongoDb.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;1591</NoWarn>
<DocumentationFile>Plus.MongoDb.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="mongocsharpdriver" Version="2.8.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Plus\Plus.csproj" />
</ItemGroup>
</Project>
src/Plus.MongoDB/PlusMongoDbModule.cs
View file @
3c7acca0
using
Plus.Modules
;
using
Plus.MongoDb.Configuration
;
using
Plus.MongoDb.Uow
;
using
System.Reflection
;
namespace
Plus.MongoDb
...
...
@@ -17,6 +18,7 @@ namespace Plus.MongoDb
public
override
void
Initialize
()
{
IocManager
.
Register
<
IMongoDatabaseProvider
,
UnitOfWorkMongoDatabaseProvider
>();
IocManager
.
RegisterAssembly
(
Assembly
.
GetExecutingAssembly
());
}
}
...
...
src/Plus.MongoDB/Repositories/MongoDbRepositoryBase.cs
View file @
3c7acca0
using
MongoDB.Driver
;
using
MongoDB.Driver.Linq
;
using
Plus.Dependency
;
using
Plus.Domain.Entities
;
using
Plus.Domain.Entities.Auditing
;
using
Plus.Domain.Repositories
;
using
System
;
using
System.Linq
;
using
System.Reflection
;
using
System.Threading.Tasks
;
namespace
Plus.MongoDb.Repositories
{
...
...
@@ -10,8 +15,7 @@ namespace Plus.MongoDb.Repositories
/// MongoDB Repository
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public
class
MongoDbRepositoryBase
<
TEntity
>
:
MongoDbRepositoryBase
<
TEntity
,
int
>,
IRepository
<
TEntity
>
where
TEntity
:
class
,
IEntity
<
int
>
public
abstract
class
MongoDbRepositoryBase
<
TEntity
>
:
MongoDbRepositoryBase
<
TEntity
,
int
>,
IRepository
<
TEntity
>,
IRepository
<
TEntity
,
int
>,
IRepository
,
ITransientDependency
where
TEntity
:
class
,
IEntity
<
int
>
{
public
MongoDbRepositoryBase
(
IMongoDatabaseProvider
databaseProvider
)
:
base
(
databaseProvider
)
...
...
@@ -24,24 +28,27 @@ namespace Plus.MongoDb.Repositories
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TPrimaryKey"></typeparam>
public
class
MongoDbRepositoryBase
<
TEntity
,
TPrimaryKey
>
:
PlusRepositoryBase
<
TEntity
,
TPrimaryKey
>
where
TEntity
:
class
,
IEntity
<
TPrimaryKey
>
public
abstract
class
MongoDbRepositoryBase
<
TEntity
,
TPrimaryKey
>
:
PlusRepositoryBase
<
TEntity
,
TPrimaryKey
>
where
TEntity
:
class
,
IEntity
<
TPrimaryKey
>
{
public
virtual
MongoDatabase
Database
{
get
{
return
_databaseProvider
.
Database
;
}
}
private
readonly
IMongoDatabaseProvider
_databaseProvider
;
public
virtual
IMongoDatabase
Database
=>
_databaseProvider
.
Database
;
public
virtual
MongoCollection
<
TEntity
>
Collection
public
abstract
string
CollectionName
{
get
;
}
public
virtual
IMongoCollection
<
TEntity
>
Collection
{
get
{
return
_databaseProvider
.
Database
.
GetCollection
<
TEntity
>(
typeof
(
TEntity
).
Name
);
string
text
=
CollectionName
;
if
(
Extensions
.
IsNullOrEmpty
(
CollectionName
))
{
text
=
typeof
(
TEntity
).
Name
;
}
return
_databaseProvider
.
Database
.
GetCollection
<
TEntity
>(
text
,
null
);
}
}
private
readonly
IMongoDatabaseProvider
_databaseProvider
;
public
MongoDbRepositoryBase
(
IMongoDatabaseProvider
databaseProvider
)
{
_databaseProvider
=
databaseProvider
;
...
...
@@ -49,48 +56,84 @@ namespace Plus.MongoDb.Repositories
public
override
IQueryable
<
TEntity
>
GetAll
()
{
return
Collection
.
AsQueryable
();
return
Collection
.
AsQueryable
(
new
AggregateOptions
()
{
AllowDiskUse
=
true
}
);
}
public
override
TEntity
Get
(
TPrimaryKey
id
)
{
var
query
=
MongoDB
.
Driver
.
Builders
.
Query
<
TEntity
>.
EQ
(
e
=>
e
.
Id
,
id
);
var
entity
=
Collection
.
FindOne
(
query
);
if
(
entity
.
IsNull
())
var
val
=
Builders
<
TEntity
>.
Filter
.
Eq
((
TEntity
e
)
=>
e
.
Id
,
id
);
TEntity
entity
=
IFindFluentExtensions
.
First
(
IMongoCollectionExtensions
.
Find
(
Collection
,
val
,
null
),
default
);
if
(
entity
==
null
)
{
throw
new
EntityNotFoundException
(
"There is no such an entity with given primary key. Entity type: "
+
typeof
(
TEntity
).
FullName
+
", primary key: "
+
id
);
}
return
entity
;
}
public
override
async
Task
<
TEntity
>
GetAsync
(
TPrimaryKey
id
)
{
FilterDefinition
<
TEntity
>
query
=
Builders
<
TEntity
>.
Filter
.
Eq
((
TEntity
e
)
=>
e
.
Id
,
id
);
TEntity
entity
=
await
IFindFluentExtensions
.
FirstAsync
(
IMongoCollectionExtensions
.
Find
(
Collection
,
query
,
null
),
default
);
if
(
entity
==
null
)
{
throw
new
EntityNotFoundException
(
"There is no such an entity with given primary key. Entity type: "
+
typeof
(
TEntity
).
FullName
+
", primary key: "
+
id
);
}
return
entity
;
}
public
override
TEntity
FirstOrDefault
(
TPrimaryKey
id
)
{
var
query
=
MongoDB
.
Driver
.
Builders
.
Query
<
TEntity
>.
EQ
(
e
=>
e
.
Id
,
id
);
return
Collection
.
FindOne
(
query
);
FilterDefinition
<
TEntity
>
val
=
Builders
<
TEntity
>.
Filter
.
Eq
((
TEntity
e
)
=>
e
.
Id
,
id
);
return
IFindFluentExtensions
.
FirstOrDefault
(
IMongoCollectionExtensions
.
Find
(
Collection
,
val
,
null
),
default
);
}
public
override
TEntity
Insert
(
TEntity
entity
)
{
Collection
.
Insert
(
entity
);
SetCreationAuditProperties
(
entity
);
CheckAndSetDefaultValue
(
entity
);
Collection
.
InsertOne
(
entity
,
null
,
default
);
return
entity
;
}
public
override
TEntity
Update
(
TEntity
entity
)
{
Collection
.
Save
(
entity
);
return
entity
;
throw
new
NotImplementedException
(
"更新实体指定列及查询条件,暂未实现"
);
}
public
override
void
Delete
(
TEntity
entity
)
{
Delete
(
entity
.
Id
);
throw
new
NotImplementedException
(
"删除实体,暂未实现"
);
}
public
override
void
Delete
(
TPrimaryKey
id
)
{
var
query
=
MongoDB
.
Driver
.
Builders
.
Query
<
TEntity
>.
EQ
(
e
=>
e
.
Id
,
id
);
Collection
.
Remove
(
query
);
TEntity
val
=
this
.
Get
(
id
);
this
.
Delete
(
val
);
}
protected
virtual
void
SetCreationAuditProperties
(
object
entityAsObj
)
{
if
(
entityAsObj
is
IHasCreationTime
val
)
{
if
(
val
.
CreationTime
==
default
)
{
val
.
CreationTime
=
DateTime
.
Now
;
}
if
(
entityAsObj
is
ICreationAudited
)
{
}
}
}
protected
virtual
void
CheckAndSetDefaultValue
(
object
entityAsObj
)
{
PropertyInfo
[]
properties
=
entityAsObj
.
GetType
().
GetProperties
();
foreach
(
PropertyInfo
propertyInfo
in
properties
)
{
if
(
propertyInfo
.
PropertyType
==
typeof
(
string
)
&&
propertyInfo
.
GetValue
(
entityAsObj
)
==
null
)
{
propertyInfo
.
SetValue
(
entityAsObj
,
string
.
Empty
);
}
}
}
}
}
\ No newline at end of file
src/Plus.MongoDB/Uow/UnitOfWorkMongoDatabaseProvider.cs
View file @
3c7acca0
using
MongoDB.Driver
;
using
Plus.Dependency
;
using
Plus.Domain.Uow
;
using
Plus.MongoDb.Configuration
;
namespace
Plus.MongoDb.Uow
{
/// <summary>
/// Implements <see cref="IMongoDatabaseProvider"/> that gets database from active unit of work.
/// </summary>
public
class
UnitOfWorkMongoDatabaseProvider
:
IMongoDatabaseProvider
,
ITransientDependency
public
class
UnitOfWorkMongoDatabaseProvider
:
IMongoDatabaseProvider
{
public
MongoDatabase
Database
{
get
{
return
((
MongoDbUnitOfWork
)
_currentUnitOfWork
.
Current
).
Database
;
}
}
private
IMongoDbModuleConfiguration
_configuration
;
public
IMongoClient
Client
{
get
;
private
set
;
}
p
rivate
readonly
ICurrentUnitOfWorkProvider
_currentUnitOfWork
;
p
ublic
IMongoDatabase
Database
{
get
;
private
set
;
}
public
UnitOfWorkMongoDatabaseProvider
(
I
CurrentUnitOfWorkProvider
currentUnitOfWork
)
public
UnitOfWorkMongoDatabaseProvider
(
I
MongoDbModuleConfiguration
configuration
)
{
_currentUnitOfWork
=
currentUnitOfWork
;
_configuration
=
configuration
;
Client
=
new
MongoClient
(
_configuration
.
ConnectionString
);
Database
=
Client
.
GetDatabase
(
_configuration
.
DatabaseName
,
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