Skip to content

Commit

Permalink
前后端项目分离, .net core作为Server端,Vuejs+Ui作为Client端
Browse files Browse the repository at this point in the history
  • Loading branch information
lukangkang committed Aug 31, 2017
1 parent baf6c75 commit 3838079
Show file tree
Hide file tree
Showing 108 changed files with 1,900 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceRoot}/Surpass.Web/bin/Debug/netcoreapp2.0/Surpass.Web.dll",
"program": "${workspaceRoot}/Surpass-Server/Surpass.Web/bin/Debug/netcoreapp2.0/Surpass.Web.dll",
"args": [],
"cwd": "${workspaceRoot}/Surpass.Web",
"cwd": "${workspaceRoot}/Surpass-Server/Surpass.Web",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
Expand Down
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"taskName": "build",
"args": [
"${workspaceRoot}/Surpass.Web/Surpass.Web.csproj"
"${workspaceRoot}/Surpass-Server/Surpass.Web/Surpass.Web.csproj"
],
"isBuildCommand": true,
"problemMatcher": "$msCompile"
Expand Down
File renamed without changes.
41 changes: 41 additions & 0 deletions Surpass-Server/Surpass.DI.Extension/ExportAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Microsoft.Extensions.DependencyInjection;

namespace Surpass.DI.Extension
{
/// <summary>
/// Attribute for register type to IoC container with specific service type<br/>
/// 用于根据类型实现的单个服务类型,注册类型到IoC容器的属性<br/>
/// </summary>
[AttributeUsage(
AttributeTargets.Class | AttributeTargets.Struct,
Inherited = false,
AllowMultiple = true)]
public class ExportAttribute : ExportAttributeBase
{
/// <summary>
/// Service key<br/>
/// 服务键<br/>
/// </summary>
public object ContractKey { get; set; }
/// <summary>
/// Service type<br/>
/// 服务类型<br/>
/// </summary>
public Type ServiceType { get; set; }
/// <summary>
/// Unregister service types before register<br/>
/// 注销已有的服务类型的实现<br/>
/// </summary>
public bool ClearExists { get; set; }

/// <summary>
/// Register implementation type to container<br/>
/// 注册实现类型到容器<br/>
/// </summary>
public override void RegisterToContainer(IServiceCollection container, Type type, ServiceLifetime lifeType)
{

}
}
}
13 changes: 13 additions & 0 deletions Surpass-Server/Surpass.DI.Extension/ExportAttributeBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Microsoft.Extensions.DependencyInjection;

namespace Surpass.DI.Extension
{
/// <summary>
/// 注册类型到依赖注入容器
/// </summary>
public class ExportAttributeBase:Attribute
{
public abstract void RegisterToContainer(IServiceCollection services,Type type, ServiceLifetime lifeType);
}
}
47 changes: 47 additions & 0 deletions Surpass-Server/Surpass.DI.Extension/ExportManyAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using Microsoft.Extensions.DependencyInjection;

namespace Surpass.DI.Extension
{
/// <summary>
/// 用于根据类型的基类和接口,注册类型到IoC容器的属性
/// </summary>
[AttributeUsage(
AttributeTargets.Class | AttributeTargets.Struct,
Inherited = false)]
public class ExportManyAttributes : ExportAttributeBase
{

/// <summary>
/// Service key<br/>
/// 服务键<br/>
/// </summary>
public object ContractKey { get; set; }
/// <summary>
/// Except types<br/>
/// 排除的类型列表<br/>
/// </summary>
public Type[] Except { get; set; }
/// <summary>
/// Also register with non public service types<br/>
/// 同时注册到非公开的服务类型<br/>
/// </summary>
public bool NonPublic { get; set; }
/// <summary>
/// Unregister service types before register<br/>
/// Please sure it won't unintentionally remove innocent implementations<br/>
/// 注销已有的服务类型的实现<br/>
/// 请确保它不会意外的移除无辜的实现<br/>
/// </summary>
public bool ClearExists { get; set; }

/// <summary>
/// Register implementation type to container<br/>
/// 注册实现类型到容器<br/>
/// </summary>
public override void RegisterToContainer(IServiceCollection container, Type type, ServiceLifetime lifeType)
{

}
}
}
11 changes: 11 additions & 0 deletions Surpass-Server/Surpass.DI.Extension/Surpass.DI.Extension.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="1.1.0" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
130 changes: 130 additions & 0 deletions Surpass-Server/Surpass.Infrastructure/Database/DatabaseManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ZKWeb.Database;
using ZKWeb.Logging;

namespace Surpass.Infrastructure.Database {
/// <summary>
/// Database manager<br/>
/// 数据库管理器<br/>
/// </summary>
/// <example>
/// <code>
/// var databaseManager = Application.Ioc.Resolve&lt;DatabaseManager&gt;();
/// using (var context = databaseManager.CreateContext()) {
/// var data = new ExampleTable() {
/// Name = "test",
/// CreateTime = DateTime.UtcNow,
/// Deleted = false
/// };
/// context.Save(ref data);
/// }
/// </code>
/// </example>
/// <seealso cref="IDatabaseContext"/>
/// <seealso cref="IDatabaseContextFactory"/>
public class DatabaseManager {
/// <summary>
/// Default database context factory<br/>
/// 默认的数据库上下文生成器<br/>
/// </summary>
protected virtual IDatabaseContextFactory DefaultContextFactory { get; set; }

/// <summary>
/// Create database context from the default factory<br/>
/// 使用默认的生成器创建数据库上下文<br/>
/// </summary>
/// <returns></returns>
public virtual IDatabaseContext CreateContext() {
return DefaultContextFactory.CreateContext();
}

/// <summary>
/// Create database context factory from the given parameters<br/>
/// 根据传入的参数创建数据库上下文生成器<br/>
/// </summary>
/// <param name="orm">Object relational mapper</param>
/// <param name="database">Database name</param>
/// <param name="connectionString">Database connection string</param>
/// <param name="handlers"></param>
/// <param name="providers"></param>
/// <returns></returns>
public static IDatabaseContextFactory CreateContextFactor(
string orm, string database, string connectionString, IEnumerable<IDatabaseInitializeHandler> handlers = null,
IEnumerable<IEntityMappingProvider> providers = null)
{
if (string.IsNullOrEmpty(orm))
{
throw new NotSupportedException("No ORM name is provided, please set it first");
}
var assemblyName = string.Format("ZKWeb.ORM.{0}", orm);
var assemblyLoader = Application.Ioc.Resolve<IAssemblyLoader>();
Assembly assembly;
try
{
assembly = assemblyLoader.Load(assemblyName);
}
catch (Exception e)
{
throw new NotSupportedException(string.Format(
"Load ORM assembly {0} failed, please install it first. error: {1}", orm, e.Message));
}
var factorType = assembly.GetTypes().FirstOrDefault(t =>
typeof(IDatabaseContextFactory).IsAssignableFrom(t));
if (factorType == null)
{
throw new NotSupportedException(string.Format(
"Find factory type from ORM {0} failed", orm));
}
if (providers != null && handlers == null)
{
handlers = new IDatabaseInitializeHandler[0];
}
if (handlers != null && providers != null)
return (IDatabaseContextFactory)Activator.CreateInstance(factorType, database, connectionString, handlers, providers);
return (IDatabaseContextFactory)Activator.CreateInstance(factorType, database, connectionString);
}

/// <summary>
/// Initialize database manager<br/>
/// 初始化数据库管理器<br/>
/// </summary>
internal protected virtual void Initialize() {
var configManager = Application.Ioc.Resolve<WebsiteConfigManager>();
var config = configManager.WebsiteConfig;
DefaultContextFactory = CreateContextFactor(
config.ORM, config.Database, config.ConnectionString);
}

/// <summary>
/// Use temporary database in the specified scope<br/>
/// 在指定范围内使用临时数据库<br/>
/// </summary>
/// <returns></returns>
public virtual IDisposable UseTemporaryDatabase(string key, IEnumerable<IDatabaseInitializeHandler> handlers = null,
IEnumerable<IEntityMappingProvider> providers = null)
{
var log = Application.Ioc.Resolve<LogManager>();
// Create database context factory, default use nhibernate orm
var configManager = Application.Ioc.Resolve<WebsiteConfigManager>();
var databases = configManager.WebsiteConfig.ExtensionDatabases;
var orm = databases.FirstOrDefault(x => x.Key.Equals(key)).ORM ?? "NHibernate";
//datatbases.GetOrDefault<string>(ExtraConfigKeys.TemporaryDatabaseORM) ?? "NHibernate";
var database = databases.FirstOrDefault(x => x.Key.Equals(key)).Database ?? "MSSQL";
//extra.GetOrDefault<string>(ExtraConfigKeys.TemporaryDatabaseType);
var connectionString = databases.FirstOrDefault(x => x.Key.Equals(key)).ConnectionString;
//extra.GetOrDefault<string>(ExtraConfigKeys.TemporaryDatabaseConnectionString);
var contextFactory = CreateContextFactor(orm, database, connectionString, handlers, providers);
// Override database manager with above factory
var overrideIoc = Application.OverrideIoc();
var databaseManagerMock = Substitute.For<DatabaseManager>();
databaseManagerMock.CreateContext().Returns(callInfo => contextFactory.CreateContext());
Application.Ioc.Unregister<DatabaseManager>();
Application.Ioc.RegisterInstance(databaseManagerMock);
// Finish override when disposed
return overrideIoc;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace ZKWeb.Database {
/// <summary>
/// The mapping options of the entity field<br/>
/// 实体字段的映射选项<br/>
/// </summary>
public class EntityMappingOptions {
/// <summary>
/// Custom column name<br/>
/// 自定义列名<br/>
/// </summary>
public string Column { get; set; }
/// <summary>
/// Custom field length<br/>
/// 自定义字段长度<br/>
/// </summary>
public long? Length { get; set; }
/// <summary>
/// Create unique index<br/>
/// 是否创建唯一键<br/>
/// </summary>
public bool? Unique { get; set; }
/// <summary>
/// Allow this field to be null<br/>
/// 是否允许这个字段等于null<br/>
/// </summary>
public bool? Nullable { get; set; }
/// <summary>
/// Create index with this name<br/>
/// 是否使用此名称创建索引<br/>
/// </summary>
public string Index { get; set; }
/// <summary>
/// Custom sql type<br/>
/// 自定义sql类型<br/>
/// </summary>
public string CustomSqlType { get; set; }
/// <summary>
/// Enable cascade delete<br/>
/// 是否开启级联删除<br/>
/// </summary>
public bool? CascadeDelete { get; set; }
/// <summary>
/// Member will serialize (eg: as json) before store into database,<br/>
/// and deserialize after retrieve from database if it is true<br/>
/// 如果开启了此选项是, 则会在保存成员时序列化(例如json), 并在获取成员时反序列化<br/>
/// </summary>
public bool? WithSerialization { get; set; }
/// <summary>
/// Custom navigation property on the other side for References, HasMany, HasManyToMany<br/>
/// If this option is empty, it will be detected automatically<br/>
/// 自定义的导航属性, 用于指定References, HasMany, HasManyToMany的另一边实体中的属性名称<br/>
/// 如果这个选项空白, 则会自动检测该属性<br/>
/// </summary>
public string Navigation { get; set; }
}
}
Loading

0 comments on commit 3838079

Please sign in to comment.