mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
EF Core
This commit is contained in:
@ -36,6 +36,9 @@ namespace Esyur.Stores.EntityCore
|
||||
{
|
||||
public class EntityResource : IResource
|
||||
{
|
||||
[NotMapped]
|
||||
internal int _PrimaryId;
|
||||
|
||||
public event DestroyedEvent OnDestroy;
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
|
@ -45,7 +45,9 @@ namespace Esyur.Stores.EntityCore
|
||||
|
||||
public event DestroyedEvent OnDestroy;
|
||||
|
||||
struct TypeInfo
|
||||
Dictionary<Type, Dictionary<int, WeakReference>> DB = new Dictionary<Type, Dictionary<int, WeakReference>>();
|
||||
|
||||
internal struct TypeInfo
|
||||
{
|
||||
public string Name;
|
||||
public IEntityType Type;
|
||||
@ -53,55 +55,57 @@ namespace Esyur.Stores.EntityCore
|
||||
}
|
||||
|
||||
Dictionary<string, TypeInfo> TypesByName = new Dictionary<string, TypeInfo>();
|
||||
Dictionary<Type, TypeInfo> TypesByType = new Dictionary<Type, TypeInfo>();
|
||||
|
||||
/*
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
var extension = optionsBuilder.Options.FindExtension<EsyurExtension>()
|
||||
?? new EsyurExtension();
|
||||
|
||||
|
||||
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
|
||||
//optionsBuilder.UseLazyLoadingProxies();
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
//modelBuilder.Entity<Series>().ToTable("Series");
|
||||
//modelBuilder.Entity<Episode>().ToTable("Episodes").;
|
||||
//modelBuilder.Ignore<Entit>
|
||||
// modelBuilder.Entity<Series>(x=>x.Property(p=>p.Instance).HasConversion(v=>v.Managers.)
|
||||
Console.WriteLine("OnModelCreating");
|
||||
//modelBuilder.Entity()
|
||||
internal Dictionary<Type, TypeInfo> TypesByType = new Dictionary<Type, TypeInfo>();
|
||||
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}*/
|
||||
|
||||
bool Loaded;
|
||||
|
||||
public async AsyncReply<IResource> Get(string path)
|
||||
{
|
||||
var p = path.Split('/');
|
||||
var ti = TypesByName[p[0]];
|
||||
var id = Convert.ToInt32(p[1]);
|
||||
return DbContext.Find(ti.Type.ClrType, id) as IResource;
|
||||
return DbContextProvider().Find(ti.Type.ClrType, id) as IResource;
|
||||
}
|
||||
|
||||
public async AsyncReply<bool> Put(IResource resource)
|
||||
{
|
||||
if (resource is EntityStore)
|
||||
return false;
|
||||
|
||||
var type = ResourceProxy.GetBaseType(resource);//.GetType().;
|
||||
|
||||
var eid = (resource as EntityResource)._PrimaryId;// (int)resource.Instance.Variables["eid"];
|
||||
|
||||
if (DB[type].ContainsKey(eid))
|
||||
DB[type].Remove(eid);
|
||||
|
||||
DB[type].Add(eid, new WeakReference(resource));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public IResource GetById(Type type, int id)
|
||||
{
|
||||
if (!DB[type].ContainsKey(id))
|
||||
return null;
|
||||
|
||||
if (!DB[type][id].IsAlive)
|
||||
return null;
|
||||
|
||||
return DB[type][id].Target as IResource;
|
||||
}
|
||||
|
||||
[Attribute]
|
||||
public EsyurExtensionOptions Options { get; set; }
|
||||
public Func<DbContext> DbContextProvider { get; set; }
|
||||
|
||||
[Attribute]
|
||||
public DbContextOptionsBuilder Options { get; set; }
|
||||
|
||||
//DbContext dbContext;
|
||||
[Attribute]
|
||||
public DbContext DbContext { get; set; }
|
||||
//[Attribute]
|
||||
//public DbContext DbContext { get; set; }
|
||||
|
||||
public string Link(IResource resource)
|
||||
{
|
||||
@ -178,9 +182,15 @@ namespace Esyur.Stores.EntityCore
|
||||
|
||||
public AsyncReply<bool> Trigger(ResourceTrigger trigger)
|
||||
{
|
||||
if (trigger == ResourceTrigger.SystemInitialized && DbContext != null)
|
||||
if (trigger == ResourceTrigger.Initialize)// SystemInitialized && DbContext != null)
|
||||
{
|
||||
var types = DbContext.Model.GetEntityTypes();
|
||||
|
||||
if (DbContextProvider == null)
|
||||
DbContextProvider = () => Activator.CreateInstance(Options.Options.ContextType, Options.Options) as DbContext;
|
||||
|
||||
var context = Activator.CreateInstance(Options.Options.ContextType, Options.Options) as DbContext;
|
||||
|
||||
var types = context.Model.GetEntityTypes();
|
||||
foreach (var t in types)
|
||||
{
|
||||
var ti = new TypeInfo()
|
||||
@ -192,6 +202,8 @@ namespace Esyur.Stores.EntityCore
|
||||
|
||||
TypesByName.Add(t.ClrType.Name, ti);
|
||||
TypesByType.Add(t.ClrType, ti);
|
||||
|
||||
DB.Add(t.ClrType, new Dictionary<int, WeakReference>());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,14 +11,14 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Marten" Version="3.10.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.1.2" />
|
||||
<PackageReference Include="Npgsql" Version="4.1.3.1" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />
|
||||
<PackageReference Include="System.Collections" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -57,6 +57,7 @@ namespace Esyur.Stores.EntityCore
|
||||
|
||||
public EntityStore Store => _store;
|
||||
|
||||
|
||||
public void ApplyServices(IServiceCollection services)
|
||||
{
|
||||
services.AddEntityFrameworkProxies();
|
||||
|
@ -60,11 +60,11 @@ namespace Esyur.Stores.EntityCore
|
||||
}
|
||||
|
||||
public static DbContextOptionsBuilder UseEsyur(this DbContextOptionsBuilder optionsBuilder,
|
||||
DbContext context,
|
||||
//DbContext context,
|
||||
string name = null,
|
||||
IResource parent = null,
|
||||
IPermissionsManager manager = null
|
||||
|
||||
IResource parent = null,
|
||||
IPermissionsManager manager = null,
|
||||
Func<DbContext> dbContextProvider = null
|
||||
)
|
||||
{
|
||||
var extension = optionsBuilder.Options.FindExtension<EsyurExtensionOptions>();
|
||||
@ -72,10 +72,10 @@ namespace Esyur.Stores.EntityCore
|
||||
if (extension == null)
|
||||
{
|
||||
|
||||
var store = Warehouse.New<EntityStore>(name, null, parent, manager);
|
||||
var store = Warehouse.New<EntityStore>(name, null, parent, manager, new { Options = optionsBuilder, DbContextProvider = dbContextProvider });
|
||||
extension = new EsyurExtensionOptions(store);
|
||||
store.Options = extension;
|
||||
store.DbContext = context;
|
||||
//store.Options = optionsBuilder;
|
||||
//store.DbContext = context;
|
||||
}
|
||||
|
||||
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
|
||||
@ -86,22 +86,24 @@ namespace Esyur.Stores.EntityCore
|
||||
|
||||
public static DbContextOptionsBuilder<TContext> UseEsyur<TContext>(
|
||||
this DbContextOptionsBuilder<TContext> optionsBuilder,
|
||||
DbContext context,
|
||||
//DbContext context,
|
||||
string name = null,
|
||||
IResource parent = null,
|
||||
IPermissionsManager manager = null)
|
||||
IPermissionsManager manager = null,
|
||||
Func<DbContext> dbContextProvider = null)
|
||||
where TContext : DbContext
|
||||
{
|
||||
|
||||
|
||||
var extension = optionsBuilder.Options.FindExtension<EsyurExtensionOptions>();
|
||||
|
||||
|
||||
if (extension == null)
|
||||
{
|
||||
var store = Warehouse.New<EntityStore>(name, null, parent, manager);
|
||||
var store = Warehouse.New<EntityStore>(name, null, parent, manager, new { Options = optionsBuilder, DbContextProvider = dbContextProvider });
|
||||
extension = new EsyurExtensionOptions(store);
|
||||
store.Options = extension;
|
||||
store.DbContext = context;
|
||||
//store.Options = optionsBuilder;
|
||||
//store.Options = extension;
|
||||
//store.DbContext = context;
|
||||
}
|
||||
|
||||
|
||||
|
@ -24,10 +24,12 @@ SOFTWARE.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Esyur.Proxy;
|
||||
using Esyur.Resource;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@ -48,40 +50,39 @@ namespace Esyur.Stores.EntityCore
|
||||
public static object CreateInstance(
|
||||
IDbContextOptions dbContextOptions,
|
||||
IEntityType entityType,
|
||||
ILazyLoader loader,
|
||||
object[] constructorArguments)
|
||||
// ILazyLoader loader,
|
||||
object[] constructorArguments,
|
||||
DbContext context,
|
||||
int id = 0
|
||||
)
|
||||
{
|
||||
var options = dbContextOptions.FindExtension<EsyurExtensionOptions>();
|
||||
var manager = options.Store.Instance.Managers.Count > 0 ? options.Store.Instance.Managers.First() : null;
|
||||
|
||||
return CreateInstance2(
|
||||
options,
|
||||
entityType,
|
||||
loader,
|
||||
constructorArguments);
|
||||
var cache = options.Store.GetById(entityType.ClrType, id);
|
||||
|
||||
if (cache != null)
|
||||
return cache;
|
||||
|
||||
// check if the object exists
|
||||
var obj = Warehouse.New(entityType.ClrType) as EntityResource;//, "", options.Store, null, manager);
|
||||
obj._PrimaryId = id;
|
||||
options.Store.TypesByType[entityType.ClrType].PrimaryKey.SetValue(obj, id);
|
||||
Warehouse.Put(obj, id.ToString(), options.Store, null, null, 0, manager);
|
||||
|
||||
// obj.Instance.IntVal = id;//.Variables.Add("eid", id);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
public static object CreateInstance2(
|
||||
EsyurExtensionOptions options,
|
||||
IEntityType entityType,
|
||||
ILazyLoader loader,
|
||||
object[] constructorArguments)
|
||||
{
|
||||
//var key = entityType.FindPrimaryKey();
|
||||
//options.AddType(entityType);
|
||||
|
||||
var manager = options.Store.Instance.Managers.Count > 0 ? options.Store.Instance.Managers.First() : null;
|
||||
return Warehouse.New(entityType.ClrType, "", options.Store, null, manager);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public EsyurProxyRewrite(EsyurExtensionOptions ext, ProviderConventionSetBuilderDependencies conventionSetBuilderDependencies)
|
||||
{
|
||||
_directBindingConvention = new ConstructorBindingConvention(conventionSetBuilderDependencies);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void ProcessModelFinalized(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
|
||||
{
|
||||
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
|
||||
@ -108,8 +109,10 @@ namespace Esyur.Stores.EntityCore
|
||||
{
|
||||
new DependencyInjectionParameterBinding(typeof(IDbContextOptions), typeof(IDbContextOptions)),
|
||||
new EntityTypeParameterBinding(),
|
||||
new DependencyInjectionParameterBinding(typeof(ILazyLoader), typeof(ILazyLoader)),
|
||||
new ObjectArrayParameterBinding(binding.ParameterBindings)
|
||||
//new DependencyInjectionParameterBinding(typeof(ILazyLoader), typeof(ILazyLoader)),
|
||||
new ObjectArrayParameterBinding(binding.ParameterBindings),
|
||||
new ContextParameterBinding(typeof(DbContext)),
|
||||
new PropertyParameterBinding(entityType.FindPrimaryKey().Properties.FirstOrDefault())
|
||||
},
|
||||
proxyType));
|
||||
}
|
||||
|
Reference in New Issue
Block a user