Skip to content

Commit

Permalink
添加ef扩展
Browse files Browse the repository at this point in the history
  • Loading branch information
zan.dai committed May 14, 2021
1 parent 1c256b6 commit 0eee501
Show file tree
Hide file tree
Showing 21 changed files with 852 additions and 8 deletions.
2 changes: 1 addition & 1 deletion LBON.Consts/LBON.Consts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description>Lu Ban Of .Net - .Net 高可用、高效率的扩展组件</Description>
<ReleaseVersion>0.0.1</ReleaseVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
<Version>2.0.1.3</Version>
<Version>2.0.1.4</Version>
<Copyright>CacoCode</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/CacoCode/LBON</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace LBON.DependencyInjection.DependencyInjection
{
public interface IScopedDependency
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace LBON.DependencyInjection.DependencyInjection
{
public interface ISingletonDependency
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace LBON.DependencyInjection.DependencyInjection
{
public interface ITransientDependency
{
}
}
61 changes: 61 additions & 0 deletions LBON.DependencyInjection/DependencyInjectionExtension.cs
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);
}
}
}
}

}
}
}
38 changes: 38 additions & 0 deletions LBON.DependencyInjection/LBON.DependencyInjection.csproj
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>
7 changes: 7 additions & 0 deletions LBON.EntityFrameworkCore/Entities/EntityBase.cs
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; }
}
}
15 changes: 15 additions & 0 deletions LBON.EntityFrameworkCore/Entities/FullAuditedEntity.cs
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; }
}
}
18 changes: 18 additions & 0 deletions LBON.EntityFrameworkCore/Entities/ICreationAudited.cs
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; }
}
}
23 changes: 23 additions & 0 deletions LBON.EntityFrameworkCore/Entities/IDeletionAudited.cs
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; }
}
}
7 changes: 7 additions & 0 deletions LBON.EntityFrameworkCore/Entities/IEntity.cs
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; }
}
}
16 changes: 16 additions & 0 deletions LBON.EntityFrameworkCore/Entities/IExtendableObject.cs
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; }
}
}
18 changes: 18 additions & 0 deletions LBON.EntityFrameworkCore/Entities/IModificationAudited.cs
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 LBON.EntityFrameworkCore/Extensions/ExtendableObjectExtensions.cs
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;
}
}
}
Loading

0 comments on commit 0eee501

Please sign in to comment.