-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zan.dai
committed
May 14, 2021
1 parent
1c256b6
commit 0eee501
Showing
21 changed files
with
852 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
LBON.DependencyInjection/DependencyInjection/IScopedDependency.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace LBON.DependencyInjection.DependencyInjection | ||
{ | ||
public interface IScopedDependency | ||
{ | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
LBON.DependencyInjection/DependencyInjection/ISingletonDependency.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace LBON.DependencyInjection.DependencyInjection | ||
{ | ||
public interface ISingletonDependency | ||
{ | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
LBON.DependencyInjection/DependencyInjection/ITransientDependency.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace LBON.DependencyInjection.DependencyInjection | ||
{ | ||
public interface ITransientDependency | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Linq; | ||
using System.Reflection; | ||
using LBON.DependencyInjection.DependencyInjection; | ||
using LBON.EntityFrameworkCore.Repositories; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class DependencyInjectionExtension | ||
{ | ||
public static void ServiceRegister(this IServiceCollection services, params Assembly[] assemblies) | ||
{ | ||
services.AddScoped(typeof(IEfRepository<,,>), typeof(EfRepository<,,>)); | ||
if (assemblies.Length <= 0) | ||
{ | ||
assemblies = new[] { Assembly.GetExecutingAssembly() }; | ||
} | ||
|
||
foreach (var assembly in assemblies) | ||
{ | ||
var allTypes = assembly.GetTypes(); | ||
foreach (var type in allTypes) | ||
{ | ||
if (typeof(ITransientDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) | ||
{ | ||
|
||
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ITransientDependency")); | ||
foreach (var interfaceType in interfaceTypes) | ||
{ | ||
services.AddTransient(interfaceType, type); | ||
} | ||
} | ||
} | ||
|
||
foreach (var type in allTypes) | ||
{ | ||
if (typeof(ISingletonDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) | ||
{ | ||
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ISingletonDependency")); | ||
foreach (var interfaceType in interfaceTypes) | ||
{ | ||
services.AddSingleton(interfaceType, type); | ||
} | ||
} | ||
} | ||
|
||
foreach (var type in allTypes) | ||
{ | ||
if (typeof(IScopedDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) | ||
{ | ||
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("IScopedDependency")); | ||
foreach (var interfaceType in interfaceTypes) | ||
{ | ||
services.AddScoped(interfaceType, type); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<Description>Lu Ban Of .Net - .Net 高可用、高效率的扩展组件</Description> | ||
<ReleaseVersion>2.0.1.4</ReleaseVersion> | ||
<SynchReleaseVersion>false</SynchReleaseVersion> | ||
<Version>2.0.1.4</Version> | ||
<Copyright>CacoCode</Copyright> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageProjectUrl>https://github.com/CacoCode/LBON</PackageProjectUrl> | ||
<PackageIcon>logo.png</PackageIcon> | ||
<RepositoryUrl>https://github.com/CacoCode/LBON</RepositoryUrl> | ||
<PackageTags>LBON.DependencyInjection</PackageTags> | ||
<Authors>CacoCode</Authors> | ||
<Company>CacoCode</Company> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\LICENSE" Link="LICENSE"> | ||
<PackagePath></PackagePath> | ||
<Pack>True</Pack> | ||
</None> | ||
<None Include="..\logo.png" Link="logo.png"> | ||
<PackagePath></PackagePath> | ||
<Pack>True</Pack> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\LBON.EntityFrameworkCore\LBON.EntityFrameworkCore.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace LBON.EntityFrameworkCore.Entities | ||
{ | ||
public class EntityBase<TKey> : IEntity<TKey> | ||
{ | ||
public TKey Id { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
|
||
namespace LBON.EntityFrameworkCore.Entities | ||
{ | ||
public class FullAuditedEntity<TKey,TUser>:EntityBase<TKey>, ICreationAudited<TUser>, IModificationAudited<TUser>, IDeletionAudited<TUser> | ||
{ | ||
public TUser CreatorId { get; set; } | ||
public DateTime CreationTime { get; set; } | ||
public TUser LastModifierId { get; set; } | ||
public DateTime? LastModificationTime { get; set; } | ||
public TUser DeleterId { get; set; } | ||
public bool IsDeleted { get; set; } | ||
public DateTime? DeletionTime { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace LBON.EntityFrameworkCore.Entities | ||
{ | ||
public interface ICreationAudited<TUser> : IHasCreator<TUser>, IHasCreationTime | ||
{ | ||
} | ||
|
||
public interface IHasCreator<TUser> | ||
{ | ||
TUser CreatorId { get; set; } | ||
} | ||
|
||
public interface IHasCreationTime | ||
{ | ||
DateTime CreationTime { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
|
||
namespace LBON.EntityFrameworkCore.Entities | ||
{ | ||
public interface IDeletionAudited<TUser> : IHasDeleter<TUser>,ISoftDelete, IHasDeletionTime | ||
{ | ||
} | ||
|
||
public interface ISoftDelete | ||
{ | ||
bool IsDeleted { get; set; } | ||
} | ||
|
||
public interface IHasDeleter<TUser> | ||
{ | ||
TUser DeleterId { get; set; } | ||
} | ||
|
||
public interface IHasDeletionTime | ||
{ | ||
DateTime? DeletionTime { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace LBON.EntityFrameworkCore.Entities | ||
{ | ||
public interface IEntity<TKey> | ||
{ | ||
TKey Id { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace LBON.EntityFrameworkCore.Entities | ||
{ | ||
public interface IExtendableObject | ||
{ | ||
/// <summary> | ||
/// Gets or sets the extension data. | ||
/// </summary> | ||
/// <value> | ||
/// The extension data. | ||
/// </value> | ||
[CanBeNull] | ||
string ExtensionData { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace LBON.EntityFrameworkCore.Entities | ||
{ | ||
public interface IModificationAudited<TUser> : IHasLastModifier<TUser>, IHasLastModificationTime | ||
{ | ||
} | ||
|
||
public interface IHasLastModifier<TUser> | ||
{ | ||
TUser LastModifierId { get; set; } | ||
} | ||
|
||
public interface IHasLastModificationTime | ||
{ | ||
DateTime? LastModificationTime { get; set; } | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
LBON.EntityFrameworkCore/Extensions/ExtendableObjectExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using JetBrains.Annotations; | ||
using LBON.EntityFrameworkCore.Entities; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using LBON.Helper; | ||
|
||
namespace LBON.EntityFrameworkCore.Extensions | ||
{ | ||
public static class ExtendableObjectExtensions | ||
{ | ||
public static T GetData<T>([NotNull] this IExtendableObject extendableObject, [NotNull] string name, bool handleType = false) | ||
{ | ||
return extendableObject.GetData<T>( | ||
name, | ||
handleType | ||
? new JsonSerializer { TypeNameHandling = TypeNameHandling.All } | ||
: JsonSerializer.CreateDefault() | ||
); | ||
} | ||
|
||
public static T GetData<T>([NotNull] this IExtendableObject extendableObject, [NotNull] string name, [CanBeNull] JsonSerializer jsonSerializer) | ||
{ | ||
if (extendableObject.ExtensionData == null) | ||
{ | ||
return default(T); | ||
} | ||
|
||
var json = JObject.Parse(extendableObject.ExtensionData); | ||
|
||
var prop = json[name]; | ||
if (prop == null) | ||
{ | ||
return default(T); | ||
} | ||
|
||
if (TypeHelper.IsPrimitiveExtendedIncludingNullable(typeof(T))) | ||
{ | ||
return prop.Value<T>(); | ||
} | ||
else | ||
{ | ||
return (T)prop.ToObject(typeof(T), jsonSerializer ?? JsonSerializer.CreateDefault()); | ||
} | ||
} | ||
|
||
|
||
|
||
public static void SetData<T>([NotNull] this IExtendableObject extendableObject, [NotNull] string name, [CanBeNull] T value, bool handleType = false) | ||
{ | ||
extendableObject.SetData( | ||
name, | ||
value, | ||
handleType | ||
? new JsonSerializer { TypeNameHandling = TypeNameHandling.All } | ||
: JsonSerializer.CreateDefault() | ||
); | ||
} | ||
|
||
public static void SetData<T>([NotNull] this IExtendableObject extendableObject, [NotNull] string name, [CanBeNull] T value, [CanBeNull] JsonSerializer jsonSerializer) | ||
{ | ||
if (extendableObject == null) | ||
{ | ||
throw new ArgumentNullException(nameof(extendableObject)); | ||
} | ||
|
||
if (name == null) | ||
{ | ||
throw new ArgumentNullException(nameof(name)); | ||
} | ||
|
||
if (jsonSerializer == null) | ||
{ | ||
jsonSerializer = JsonSerializer.CreateDefault(); | ||
} | ||
|
||
if (extendableObject.ExtensionData == null) | ||
{ | ||
if (EqualityComparer<T>.Default.Equals(value, default(T))) | ||
{ | ||
return; | ||
} | ||
|
||
extendableObject.ExtensionData = "{}"; | ||
} | ||
|
||
var json = JObject.Parse(extendableObject.ExtensionData); | ||
|
||
if (value == null || EqualityComparer<T>.Default.Equals(value, default(T))) | ||
{ | ||
if (json[name] != null) | ||
{ | ||
json.Remove(name); | ||
} | ||
} | ||
else if (TypeHelper.IsPrimitiveExtendedIncludingNullable(value.GetType())) | ||
{ | ||
json[name] = new JValue(value); | ||
} | ||
else | ||
{ | ||
json[name] = JToken.FromObject(value, jsonSerializer); | ||
} | ||
|
||
var data = json.ToString(Formatting.None); | ||
if (data == "{}") | ||
{ | ||
data = null; | ||
} | ||
|
||
extendableObject.ExtensionData = data; | ||
} | ||
|
||
public static bool RemoveData([NotNull] this IExtendableObject extendableObject, string name) | ||
{ | ||
if (extendableObject == null) | ||
{ | ||
throw new ArgumentNullException(nameof(extendableObject)); | ||
} | ||
|
||
if (extendableObject.ExtensionData == null) | ||
{ | ||
return false; | ||
} | ||
|
||
var json = JObject.Parse(extendableObject.ExtensionData); | ||
|
||
var token = json[name]; | ||
if (token == null) | ||
{ | ||
return false; | ||
} | ||
|
||
json.Remove(name); | ||
|
||
var data = json.ToString(Formatting.None); | ||
if (data == "{}") | ||
{ | ||
data = null; | ||
} | ||
|
||
extendableObject.ExtensionData = data; | ||
|
||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.