Commit 71223cfd authored by 阿星Plus's avatar 阿星Plus

todo : mongodb test

parent ff43234c
using MongoDB.Driver;
using Plus.Core.Tests.Configuration;
using System.Collections.Generic;
namespace Plus.MongoDb.Test
{
public class BlogMongoDbDatabaseProvider : IMongoDatabaseProvider
{
public IMongoClient Client { get; }
public IMongoDatabase Database { get; }
MongoDatabase IMongoDatabaseProvider.Database
{
get
{
return null;
}
}
//public MongoDatabase Database
//{
// get
// {
// var servers = new List<MongoServerAddress>();
// AppSettings.MongoDb.Servers.ForEach(x =>
// {
// servers.Add(new MongoServerAddress(x.Host, x.Port));
// });
// var settings = new MongoClientSettings();
// settings.Servers = servers;
// if (AppSettings.MongoDb.Username.IsNotNullOrEmpty())
// {
// settings.Credential = MongoCredential.CreateCredential("admin", AppSettings.MongoDb.Username, AppSettings.MongoDb.Password);
// }
// if (AppSettings.MongoDb.ConnectionMode.ToLower() == "replicaset")
// {
// settings.ConnectionMode = ConnectionMode.ReplicaSet;
// settings.ReadPreference = new ReadPreference(ReadPreferenceMode.SecondaryPreferred);
// }
// var client = new MongoClient(settings);
// var database = client.GetDatabase(AppSettings.MongoDb.DatabaseName);
// return Database;
// }
//}
public BlogMongoDbDatabaseProvider()
{
var servers = new List<MongoServerAddress>();
AppSettings.MongoDb.Servers.ForEach(x =>
{
servers.Add(new MongoServerAddress(x.Host, x.Port));
});
var settings = new MongoClientSettings();
settings.Servers = servers;
if (AppSettings.MongoDb.Username.IsNotNullOrEmpty())
{
settings.Credential = MongoCredential.CreateCredential("admin", AppSettings.MongoDb.Username, AppSettings.MongoDb.Password);
}
if (AppSettings.MongoDb.ConnectionMode.ToLower() == "replicaset")
{
settings.ConnectionMode = ConnectionMode.ReplicaSet;
settings.ReadPreference = new ReadPreference(ReadPreferenceMode.SecondaryPreferred);
}
Client = new MongoClient(settings);
Database = Client.GetDatabase(AppSettings.MongoDb.DatabaseName);
}
}
}
\ No newline at end of file
using Plus.Modules;
using System.Reflection;
namespace Plus.MongoDb.Test
{
[DependsOn(typeof(PlusMongoDbModule))]
public class BlogMongoDbModule : PlusModule
{
public override void PreInitialize()
{
IocManager.Register<BlogMongoDbDatabaseProvider>();
}
public override void Initialize()
{
IocManager.RegisterAssembly(Assembly.GetExecutingAssembly());
}
}
}
\ No newline at end of file
using MongoDB.Bson;
using MongoDB.Driver;
using Plus.Domain.Entities;
using Plus.MongoDb.Repositories;
using System.Linq;
namespace Plus.MongoDb.Test
{
public abstract class BlogMongoDbRepositoryBase
{
private readonly BlogMongoDbDatabaseProvider _blogMongoDbDatabaseProvider;
public BlogMongoDbRepositoryBase(BlogMongoDbDatabaseProvider blogMongoDbDatabaseProvider)
{
_blogMongoDbDatabaseProvider = blogMongoDbDatabaseProvider;
}
public IMongoDatabase Database => _blogMongoDbDatabaseProvider.Database;
}
public abstract class BlogMongoDbTakeAutoIncRepositoryBase<TEntity> : BlogMongoDbRepositoryBase<TEntity, ObjectId> where TEntity : class, IEntity<ObjectId>
{
public BlogMongoDbTakeAutoIncRepositoryBase(BlogMongoDbDatabaseProvider databaseProvider) : base(databaseProvider)
{
}
public override IQueryable<TEntity> GetAll()
{
return base.GetAll();
}
}
public abstract class BlogMongoDbRepositoryBase<TEntity> : BlogMongoDbRepositoryBase<TEntity, ObjectId>
where TEntity : class, IEntity<ObjectId>
{
public BlogMongoDbRepositoryBase(BlogMongoDbDatabaseProvider databaseProvider) : base(databaseProvider) { }
}
public abstract class BlogMongoDbRepositoryBase<TEntity, TPrimaryKey> : MongoDbRepositoryBase<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
public BlogMongoDbRepositoryBase(BlogMongoDbDatabaseProvider databaseProvider) : base(databaseProvider) { }
}
}
\ No newline at end of file
namespace Plus.MongoDb.Test
{
public class DbConsts
{
public class DbTableName
{
public const string Posts = "posts";
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Plus.MongoDb" Version="1.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Plus\Plus.csproj" />
<ProjectReference Include="..\test\Plus.Core.Tests\Plus.Core.Tests.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Mappers\" />
</ItemGroup>
</Project>
using Plus.Core.Tests.Domain;
using Plus.Core.Tests.Repositories;
namespace Plus.MongoDb.Test.Repositories
{
public class ArticleRepository : BlogMongoDbTakeAutoIncRepositoryBase<Article>, IArticleRepository
{
public ArticleRepository(BlogMongoDbDatabaseProvider databaseProvider) : base(databaseProvider)
{
}
}
}
\ No newline at end of file
......@@ -31,15 +31,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.RedisCache", "src\Plus
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.Extensions.Serialization", "src\Plus.Extensions.Serialization\Plus.Extensions.Serialization.csproj", "{209BF4EB-311F-45AD-AED6-F314A49D83C7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.Web.Tests", "test\Plus.Web.Tests\Plus.Web.Tests.csproj", "{879A21A2-38A9-45C9-89C1-6BB7709C4F0B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.Web.Tests", "test\Plus.Web.Tests\Plus.Web.Tests.csproj", "{879A21A2-38A9-45C9-89C1-6BB7709C4F0B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.Core.Tests", "test\Plus.Core.Tests\Plus.Core.Tests.csproj", "{1017D21C-DE53-4791-8624-7727FFD73FD0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.Core.Tests", "test\Plus.Core.Tests\Plus.Core.Tests.csproj", "{1017D21C-DE53-4791-8624-7727FFD73FD0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.EFCore.Test", "test\Plus.EFCore.Test\Plus.EFCore.Test.csproj", "{4CF853EC-99EC-4441-B086-9EA2C761DB03}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.EFCore.Test", "test\Plus.EFCore.Test\Plus.EFCore.Test.csproj", "{4CF853EC-99EC-4441-B086-9EA2C761DB03}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.Services.Test", "test\Plus.Services.Test\Plus.Services.Test.csproj", "{D27671BF-7E67-4559-9DA6-E4AD418C3E7C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.Services.Test", "test\Plus.Services.Test\Plus.Services.Test.csproj", "{D27671BF-7E67-4559-9DA6-E4AD418C3E7C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.Services.Dto.Test", "test\Plus.Services.Dto.Test\Plus.Services.Dto.Test.csproj", "{0F6DE8F7-14C0-4205-B14D-10DA2FEEB9D6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.Services.Dto.Test", "test\Plus.Services.Dto.Test\Plus.Services.Dto.Test.csproj", "{0F6DE8F7-14C0-4205-B14D-10DA2FEEB9D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.MongoDb.Test", "Plus.MongoDb.Test\Plus.MongoDb.Test.csproj", "{D6796121-03B4-419D-B75E-560B29EFD06C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -99,6 +101,10 @@ Global
{0F6DE8F7-14C0-4205-B14D-10DA2FEEB9D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F6DE8F7-14C0-4205-B14D-10DA2FEEB9D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F6DE8F7-14C0-4205-B14D-10DA2FEEB9D6}.Release|Any CPU.Build.0 = Release|Any CPU
{D6796121-03B4-419D-B75E-560B29EFD06C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6796121-03B4-419D-B75E-560B29EFD06C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6796121-03B4-419D-B75E-560B29EFD06C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6796121-03B4-419D-B75E-560B29EFD06C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -117,6 +123,7 @@ Global
{4CF853EC-99EC-4441-B086-9EA2C761DB03} = {AD27338E-5F7F-4C1A-9E6F-9BFED17EF75A}
{D27671BF-7E67-4559-9DA6-E4AD418C3E7C} = {AD27338E-5F7F-4C1A-9E6F-9BFED17EF75A}
{0F6DE8F7-14C0-4205-B14D-10DA2FEEB9D6} = {AD27338E-5F7F-4C1A-9E6F-9BFED17EF75A}
{D6796121-03B4-419D-B75E-560B29EFD06C} = {AD27338E-5F7F-4C1A-9E6F-9BFED17EF75A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {44529F52-F5EE-4330-BB68-E09EB8B967E3}
......
......@@ -23,9 +23,9 @@ namespace Plus.Core.Tests.Configuration
public static class MongoDb
{
private static IConfigurationSection MongoDbSection => _config.GetSection("MongoDb");
private static IConfigurationSection MongoDbSection => ConnectionStrings.GetSection("MongoDb");
public static string ConnectionMode => MongoDbSection["ConnectionMode"];
public static string ConnectionMode => MongoDbSection["ConnectionMode"] ?? "";
public static string DatabaseName => MongoDbSection["DatabaseName"];
......
using MongoDB.Bson;
using Plus.Domain.Entities;
using System;
namespace Plus.Core.Tests.Domain
{
public class Article : IEntity<ObjectId>
{
public ObjectId Id { get; set; }
public int NumId { get; set; }
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 作者
/// </summary>
public string Author { get; set; }
/// <summary>
/// 来源
/// </summary>
public string Source { get; set; }
/// <summary>
/// 链接
/// </summary>
public string Url { get; set; }
/// <summary>
/// 摘要
/// </summary>
public string Abstract { get; set; }
/// <summary>
/// 内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 点击量
/// </summary>
public int Hits { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime? CreationTime { get; set; }
public bool IsTransient()
{
return Id == ObjectId.Empty;
}
}
}
\ No newline at end of file
......@@ -4,6 +4,10 @@
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Bson" Version="2.8.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Plus\Plus.csproj" />
</ItemGroup>
......
using MongoDB.Bson;
using Plus.Core.Tests.Domain;
using Plus.Domain.Repositories;
namespace Plus.Core.Tests.Repositories
{
public interface IArticleRepository : IRepository<Article, ObjectId>
{
}
}
\ No newline at end of file
using System;
namespace Plus.Services.Dto.Test
{
public class ArticleDto
{
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 作者
/// </summary>
public string Author { get; set; }
/// <summary>
/// 来源
/// </summary>
public string Source { get; set; }
/// <summary>
/// 链接
/// </summary>
public string Url { get; set; }
/// <summary>
/// 摘要
/// </summary>
public string Abstract { get; set; }
/// <summary>
/// 内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 点击量
/// </summary>
public int Hits { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime? CreationTime { get; set; }
}
}
\ No newline at end of file
......@@ -6,5 +6,7 @@ namespace Plus.Services.Test.Blog
public partial interface IBlogService
{
Task<PostDto> Get(int id);
Task<ArticleDto> GetArticle(int id);
}
}
\ No newline at end of file
......@@ -9,9 +9,13 @@ namespace Plus.Services.Test.Blog.Impl
{
private readonly IPostRepository _postRepository;
public BlogService(IPostRepository postRepository)
private readonly IArticleRepository _articleRepository;
public BlogService(IPostRepository postRepository, IArticleRepository articleRepository)
{
_postRepository = postRepository;
_articleRepository = articleRepository;
}
public async Task<PostDto> Get(int id)
......@@ -19,5 +23,11 @@ namespace Plus.Services.Test.Blog.Impl
var entity = await _postRepository.GetAsync(id);
return entity.MapTo<PostDto>();
}
public async Task<ArticleDto> GetArticle(int id)
{
var entity = await _articleRepository.FirstOrDefaultAsync(x => x.NumId == id);
return entity.MapTo<ArticleDto>();
}
}
}
\ No newline at end of file
......@@ -32,5 +32,16 @@ namespace Plus.Web.Tests.Controllers
};
return response;
}
[HttpGet]
[Route("GetArticle")]
public async Task<Response<ArticleDto>> GetArticle(int id)
{
var response = new Response<ArticleDto>
{
Result = await _blogService.GetArticle(id)
};
return response;
}
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Plus.MongoDb.Test\Plus.MongoDb.Test.csproj" />
<ProjectReference Include="..\..\src\Plus.Extensions\Plus.Extensions.csproj" />
<ProjectReference Include="..\..\src\Plus.Log4Net\Plus.Log4Net.csproj" />
<ProjectReference Include="..\Plus.Core.Tests\Plus.Core.Tests.csproj" />
......@@ -19,4 +20,6 @@
<ProjectReference Include="..\Plus.Services.Test\Plus.Services.Test.csproj" />
</ItemGroup>
<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
</Project>
......@@ -9,6 +9,7 @@ using Plus.Dependency;
using Plus.EFCore.Test;
using Plus.Log4Net;
using Plus.Modules;
using Plus.MongoDb.Test;
using Plus.Services.Dto.Test;
using Plus.Services.Test;
using System.Reflection;
......@@ -60,7 +61,8 @@ namespace Plus.Web.Tests
typeof(BlogCoreModule),
typeof(BlogServicesModule),
typeof(BlogServicesDtoModule),
typeof(BlogEntityFrameworkCoreModule)
typeof(BlogEntityFrameworkCoreModule),
typeof(BlogMongoDbModule)
)]
internal class BlogWebModule : PlusModule
{
......
......@@ -9,9 +9,9 @@
"ConnectionStrings": {
"MySql": "Server=localhost;User Id=root;Password=123456;Database=meowv_blog",
"MongoDb": {
"Servers": [ "localhost:27019" ],
"Servers": [ "localhost:27017" ],
"ConnectionMode": "",
"DatabaseName": "",
"DatabaseName": "MeowvBlog",
"Username": "",
"Password": ""
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment