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-03-25 08:52:15 +03:00
parent 8bd9b3282c
commit 836a1fdeae
23 changed files with 466 additions and 216 deletions

View File

@ -34,6 +34,8 @@ using Microsoft.EntityFrameworkCore.Proxies;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Esyur.Proxy;
using System.Linq;
using Microsoft.EntityFrameworkCore.Metadata;
using System.Reflection;
namespace Esyur.Stores.EntityCore
{
@ -43,6 +45,16 @@ namespace Esyur.Stores.EntityCore
public event DestroyedEvent OnDestroy;
struct TypeInfo
{
public string Name;
public IEntityType Type;
public PropertyInfo PrimaryKey;
}
Dictionary<string, TypeInfo> TypesByName = new Dictionary<string, TypeInfo>();
Dictionary<Type, TypeInfo> TypesByType = new Dictionary<Type, TypeInfo>();
/*
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
@ -56,27 +68,27 @@ namespace Esyur.Stores.EntityCore
}
*/
/*
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()
/*
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()
base.OnModelCreating(modelBuilder);
}*/
base.OnModelCreating(modelBuilder);
}*/
public async AsyncReply<IResource> Get(string path)
{
var p = path.Split('/');
var type = Options.Cache.Keys.Where(x => x.Name.ToLower() == p[0].ToLower()).FirstOrDefault();
var ti = TypesByName[p[0]];
var id = Convert.ToInt32(p[1]);
return DbContext.Find(type, id) as IResource;
return DbContext.Find(ti.Type.ClrType, id) as IResource;
}
public async AsyncReply<bool> Put(IResource resource)
@ -87,6 +99,7 @@ namespace Esyur.Stores.EntityCore
[Attribute]
public EsyurExtensionOptions Options { get; set; }
//DbContext dbContext;
[Attribute]
public DbContext DbContext { get; set; }
@ -94,7 +107,16 @@ namespace Esyur.Stores.EntityCore
{
var type = ResourceProxy.GetBaseType(resource.GetType());
var id = Options.Cache[type].GetValue(resource);
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
// .FirstOrDefault()?.PropertyInfo
// .GetValue(resource);
// var id = Types
if (id != null)
return this.Instance.Name + "/" + type.Name + "/" + id.ToString();
@ -156,6 +178,24 @@ namespace Esyur.Stores.EntityCore
public AsyncReply<bool> Trigger(ResourceTrigger trigger)
{
if (trigger == ResourceTrigger.SystemInitialized && DbContext != null)
{
var types = DbContext.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);
}
}
return new AsyncReply<bool>(true);
}

View File

@ -41,12 +41,12 @@ namespace Esyur.Stores.EntityCore
public class EsyurExtensionOptions : IDbContextOptionsExtension
{
public Dictionary<Type, PropertyInfo> Cache { get; } = new Dictionary<Type, PropertyInfo>();
public void AddType(IEntityType type)
{
if (!Cache.ContainsKey(type.ClrType))
Cache.Add(type.ClrType, type.FindPrimaryKey().Properties[0].PropertyInfo);
}
//public Dictionary<Type, PropertyInfo> Cache { get; } = new Dictionary<Type, PropertyInfo>();
//public void AddType(IEntityType type)
//{
// if (!Cache.ContainsKey(type.ClrType))
// Cache.Add(type.ClrType, type.FindPrimaryKey().Properties[0].PropertyInfo);
//}

View File

@ -68,7 +68,7 @@ namespace Esyur.Stores.EntityCore
object[] constructorArguments)
{
//var key = entityType.FindPrimaryKey();
options.AddType(entityType);
//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);