2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00
This commit is contained in:
2020-10-05 00:46:51 +03:00
parent 8c2d616d62
commit ba084b79e6
29 changed files with 1135 additions and 843 deletions

View File

@ -36,8 +36,8 @@ namespace Esyur.Stores.EntityCore
{
public class EntityResource : IResource
{
[NotMapped]
internal object _PrimaryId;
//[NotMapped]
//internal object _PrimaryId;
public event DestroyedEvent OnDestroy;
public event PropertyChangedEventHandler PropertyChanged;
@ -49,7 +49,7 @@ namespace Esyur.Stores.EntityCore
{
}
protected virtual void Create()
{
@ -69,6 +69,6 @@ namespace Esyur.Stores.EntityCore
//throw new NotImplementedException();
}
}
}

View File

@ -29,9 +29,6 @@ using Esyur.Resource.Template;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore.Proxies;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Esyur.Proxy;
using System.Linq;
using Microsoft.EntityFrameworkCore.Metadata;
@ -65,7 +62,8 @@ namespace Esyur.Stores.EntityCore
{
var p = path.Split('/');
var ti = TypesByName[p[0]];
var id = Convert.ToInt32(p[1]);
var id = Convert.ChangeType(p[1], ti.PrimaryKey.PropertyType);// Convert.ToInt32();
//Type.cas ti.PrimaryKey.PropertyType.
return DbContextProvider().Find(ti.Type.ClrType, id) as IResource;
}
@ -76,7 +74,9 @@ namespace Esyur.Stores.EntityCore
var type = ResourceProxy.GetBaseType(resource);//.GetType().;
var eid = (resource as EntityResource)._PrimaryId;// (int)resource.Instance.Variables["eid"];
//var eid = (resource as EntityResource)._PrimaryId;// (int)resource.Instance.Variables["eid"];
var eid = TypesByType[type].PrimaryKey.GetValue(resource);
if (DB[type].ContainsKey(eid))
DB[type].Remove(eid);
@ -86,7 +86,7 @@ namespace Esyur.Stores.EntityCore
return true;
}
public IResource GetById(Type type, int id)
public IResource GetById(Type type, object id)
{
if (!DB[type].ContainsKey(id))
return null;
@ -114,7 +114,7 @@ namespace Esyur.Stores.EntityCore
var id = TypesByType[type].PrimaryKey.GetValue(resource);
//DbContext.Model.FindEntityType(type).DisplayName();
// DbContext.Model.FindEntityType(type).DisplayName
//var entityType = DbContext.Model.FindEntityType(type);
//var id = entityType.FindPrimaryKey().Properties
@ -188,29 +188,37 @@ namespace Esyur.Stores.EntityCore
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()
{
Name = t.ClrType.Name,
PrimaryKey = t.FindPrimaryKey().Properties.FirstOrDefault()?.PropertyInfo,
Type = t
};
TypesByName.Add(t.ClrType.Name, ti);
TypesByType.Add(t.ClrType, ti);
DB.Add(t.ClrType, new Dictionary<object, WeakReference>());
}
ReloadModel();
}
return new AsyncReply<bool>(true);
}
public void ReloadModel()
{
TypesByName.Clear();
TypesByType.Clear();
var context = DbContextProvider();// Activator.CreateInstance(Options.Options.ContextType, Options.Options) as DbContext;
var types = context.Model.GetEntityTypes();
foreach (var t in types)
{
var ti = new TypeInfo()
{
Name = t.ClrType.Name,
PrimaryKey = t.FindPrimaryKey().Properties.FirstOrDefault()?.PropertyInfo,
Type = t
};
TypesByName.Add(t.ClrType.Name, ti);
TypesByType.Add(t.ClrType, ti);
if (!DB.ContainsKey(t.ClrType))
DB.Add(t.ClrType, new Dictionary<object, WeakReference>());
}
}
public void Destroy()
{
//throw new NotImplementedException();

View File

@ -11,14 +11,7 @@
</PropertyGroup>
<ItemGroup>
<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" Version="4.1.3.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
<PackageReference Include="System.Collections" Version="4.3.0" />
</ItemGroup>

View File

@ -30,7 +30,6 @@ using System.Text;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.Proxies.Internal;
using System.Linq;
using Microsoft.EntityFrameworkCore.Metadata;
using System.Reflection;
@ -60,7 +59,7 @@ namespace Esyur.Stores.EntityCore
public void ApplyServices(IServiceCollection services)
{
services.AddEntityFrameworkProxies();
// services.AddEntityFrameworkProxies();
new EntityFrameworkServicesBuilder(services)
.TryAdd<IConventionSetPlugin, EsyurPlugin>();

View File

@ -29,6 +29,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Esyur.Stores.EntityCore
@ -40,19 +41,28 @@ namespace Esyur.Stores.EntityCore
// return dbContext.GetInfrastructure().CreateResource<T>(properties);
//}
public static T AddResource<T>(this DbSet<T> dbSet, object properties = null) where T:class,IResource
public static T AddResource<T>(this DbSet<T> dbSet, object properties = null) where T : class, IResource
{
var store = dbSet.GetInfrastructure().GetService<IDbContextOptions>().FindExtension<EsyurExtensionOptions>().Store;
var manager = store.Instance.Managers.FirstOrDefault();// > 0 ? store.Instance.Managers.First() : null;
//var db = dbSet.GetService<ICurrentDbContext>().Context;
//var resource = dbSet.GetInfrastructure().CreateResource<T>(properties);
//var resource = Warehouse.New<T>("", options.Store, null, null, null, properties);
var resource = Warehouse.New<T>("", null, null, null, null, properties);
dbSet.Add(resource);
var entity = dbSet.Add(resource);
entity.Context.SaveChanges();
var id = store.TypesByType[typeof(T)].PrimaryKey.GetValue(resource);
Warehouse.Put(resource, id.ToString(), store, null, null, 0, manager);
return resource;
}
public static T CreateResource<T>(this IServiceProvider serviceProvider, object properties = null) where T:class,IResource
public static T CreateResource<T>(this IServiceProvider serviceProvider, object properties = null) where T : class, IResource
{
var options = serviceProvider.GetService<IDbContextOptions>().FindExtension<EsyurExtensionOptions>();
@ -72,7 +82,7 @@ namespace Esyur.Stores.EntityCore
)
{
var extension = optionsBuilder.Options.FindExtension<EsyurExtensionOptions>();
if (extension == null)
{
@ -90,7 +100,7 @@ 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,
@ -100,7 +110,7 @@ namespace Esyur.Stores.EntityCore
var extension = optionsBuilder.Options.FindExtension<EsyurExtensionOptions>();
if (extension == null)
{
var store = Warehouse.New<EntityStore>(name, null, parent, manager, new { Options = optionsBuilder, DbContextProvider = dbContextProvider });

View File

@ -25,7 +25,7 @@ SOFTWARE.
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Proxies.Internal;
//using Microsoft.EntityFrameworkCore.Proxies.Internal;
using System;
using System.Collections.Generic;
using System.Text;

View File

@ -47,15 +47,26 @@ namespace Esyur.Stores.EntityCore
private readonly ConstructorBindingConvention _directBindingConvention;
//public static object CreateInstance(IDbContextOptions dbContextOptions, IEntityType entityType,
// object[] constructorArguments, DbContext context, long id)
//{
// return CreateInstance(dbContextOptions, entityType,
// constructorArguments, context, id);
//}
public static object CreateInstance(
IDbContextOptions dbContextOptions,
IEntityType entityType,
// ILazyLoader loader,
object[] constructorArguments,
DbContext context,
int id = 0
)
IDbContextOptions dbContextOptions,
IEntityType entityType,
object[] properties
// ILazyLoader loader,
// object[] constructorArguments,
//DbContext context,
)
{
///var id = constructorArguments.Last();
var id = properties.First();
var options = dbContextOptions.FindExtension<EsyurExtensionOptions>();
var manager = options.Store.Instance.Managers.Count > 0 ? options.Store.Instance.Managers.First() : null;
@ -66,11 +77,11 @@ namespace Esyur.Stores.EntityCore
// check if the object exists
var obj = Warehouse.New(entityType.ClrType) as EntityResource;//, "", options.Store, null, manager);
obj._PrimaryId = id;
//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);
// obj.Instance.IntVal = id;//.Variables.Add("eid", id);
return obj;
}
@ -82,21 +93,21 @@ namespace Esyur.Stores.EntityCore
}
public void ProcessModelFinalized(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
{
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
{
var proxyType = ResourceProxy.GetProxy(entityType.ClrType);
var ann = entityType.GetAnnotation(CoreAnnotationNames.ConstructorBinding);
// var ann = entityType.GetAnnotation(CoreAnnotationNames.ConstructorBinding);
var binding = (InstantiationBinding)entityType[CoreAnnotationNames.ConstructorBinding];
if (binding == null)
_directBindingConvention.ProcessModelFinalized(modelBuilder, context);
binding = (InstantiationBinding)entityType[CoreAnnotationNames.ConstructorBinding];
try
@ -109,12 +120,18 @@ namespace Esyur.Stores.EntityCore
{
new DependencyInjectionParameterBinding(typeof(IDbContextOptions), typeof(IDbContextOptions)),
new EntityTypeParameterBinding(),
//new DependencyInjectionParameterBinding(typeof(ILazyLoader), typeof(ILazyLoader)),
new ObjectArrayParameterBinding(binding.ParameterBindings),
new ContextParameterBinding(typeof(DbContext)),
new PropertyParameterBinding(entityType.FindPrimaryKey().Properties.FirstOrDefault())
// constructor arguments
//new ObjectArrayParameterBinding(binding.ParameterBindings),
//new ContextParameterBinding(typeof(DbContext)),
new ObjectArrayParameterBinding(new ParameterBinding[]{
new PropertyParameterBinding(entityType.FindPrimaryKey().Properties.FirstOrDefault())
})
// new Microsoft.EntityFrameworkCore.Metadata.ObjectArrayParameterBinding(),
//new ObjectArrayParameterBinding()
},
proxyType));
}
catch
{

View File

@ -0,0 +1,8 @@
{
"profiles": {
"Esyur.Stores.EntityCore": {
"commandName": "Project",
"commandLineArgs": "--migrate"
}
}
}