mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-26 21:13:13 +00:00
Rename
This commit is contained in:
74
Esiur.Stores.EntityCore/EntityResource.cs
Normal file
74
Esiur.Stores.EntityCore/EntityResource.cs
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Esiur.Core;
|
||||
using Esiur.Resource;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Esiur.Stores.EntityCore
|
||||
{
|
||||
public class EntityResource : IResource
|
||||
{
|
||||
//[NotMapped]
|
||||
//internal object _PrimaryId;
|
||||
|
||||
public event DestroyedEvent OnDestroy;
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
[NotMapped]
|
||||
public Instance Instance { get; set; }
|
||||
|
||||
public EntityResource()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected virtual void Create()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public AsyncReply<bool> Trigger(ResourceTrigger trigger)
|
||||
{
|
||||
if (trigger == ResourceTrigger.Initialize)
|
||||
Create();
|
||||
|
||||
return new AsyncReply<bool>(true);
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
242
Esiur.Stores.EntityCore/EntityStore.cs
Normal file
242
Esiur.Stores.EntityCore/EntityStore.cs
Normal file
@ -0,0 +1,242 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esiur.Core;
|
||||
using Esiur.Data;
|
||||
using Esiur.Resource;
|
||||
using Esiur.Resource.Template;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Esiur.Proxy;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Esiur.Stores.EntityCore
|
||||
{
|
||||
public class EntityStore : IStore
|
||||
{
|
||||
public Instance Instance { get; set; }
|
||||
|
||||
public event DestroyedEvent OnDestroy;
|
||||
|
||||
Dictionary<Type, Dictionary<object, WeakReference>> DB = new Dictionary<Type, Dictionary<object, WeakReference>>();
|
||||
object DBLock = new object();
|
||||
|
||||
internal struct TypeInfo
|
||||
{
|
||||
public string Name;
|
||||
public IEntityType Type;
|
||||
public PropertyInfo PrimaryKey;
|
||||
}
|
||||
|
||||
Dictionary<string, TypeInfo> TypesByName = new Dictionary<string, TypeInfo>();
|
||||
internal Dictionary<Type, TypeInfo> TypesByType = new Dictionary<Type, TypeInfo>();
|
||||
|
||||
|
||||
|
||||
bool Loaded;
|
||||
|
||||
public AsyncReply<IResource> Get(string path)
|
||||
{
|
||||
var p = path.Split('/');
|
||||
var ti = TypesByName[p[0]];
|
||||
var id = Convert.ChangeType(p[1], ti.PrimaryKey.PropertyType);// Convert.ToInt32();
|
||||
|
||||
|
||||
var db = DbContextProvider();
|
||||
var res = db.Find(ti.Type.ClrType, id);
|
||||
var ent = db.Entry(res);
|
||||
|
||||
foreach (var rf in ent.References)
|
||||
rf.Load();
|
||||
|
||||
return new AsyncReply<IResource>(res as IResource);
|
||||
}
|
||||
|
||||
public AsyncReply<bool> Put(IResource resource)
|
||||
{
|
||||
if (resource is EntityStore)
|
||||
return new AsyncReply<bool>(false);
|
||||
|
||||
var type = ResourceProxy.GetBaseType(resource);//.GetType().;
|
||||
|
||||
//var eid = (resource as EntityResource)._PrimaryId;// (int)resource.Instance.Variables["eid"];
|
||||
|
||||
var eid = TypesByType[type].PrimaryKey.GetValue(resource);
|
||||
|
||||
lock (DBLock)
|
||||
{
|
||||
if (DB[type].ContainsKey(eid))
|
||||
DB[type].Remove(eid);
|
||||
|
||||
DB[type].Add(eid, new WeakReference(resource));
|
||||
}
|
||||
|
||||
return new AsyncReply<bool>(true);
|
||||
}
|
||||
|
||||
public IResource GetById(Type type, object id)
|
||||
{
|
||||
lock (DBLock)
|
||||
{
|
||||
if (!DB[type].ContainsKey(id))
|
||||
return null;
|
||||
|
||||
if (!DB[type][id].IsAlive)
|
||||
return null;
|
||||
|
||||
return DB[type][id].Target as IResource;
|
||||
}
|
||||
}
|
||||
|
||||
[Attribute]
|
||||
public Func<DbContext> DbContextProvider { get; set; }
|
||||
|
||||
[Attribute]
|
||||
public DbContextOptionsBuilder Options { get; set; }
|
||||
|
||||
//DbContext dbContext;
|
||||
//[Attribute]
|
||||
//public DbContext DbContext { get; set; }
|
||||
|
||||
public string Link(IResource resource)
|
||||
{
|
||||
var type = ResourceProxy.GetBaseType(resource.GetType());
|
||||
|
||||
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();
|
||||
else
|
||||
return this.Instance.Name + "/" + type.Name;
|
||||
}
|
||||
|
||||
public bool Record(IResource resource, string propertyName, object value, ulong age, DateTime dateTime)
|
||||
{
|
||||
return true;
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Modify(IResource resource, string propertyName, object value, ulong age, DateTime dateTime)
|
||||
{
|
||||
return true;
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Remove(IResource resource)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> AddChild(IResource parent, IResource child)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> RemoveChild(IResource parent, IResource child)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> AddParent(IResource child, IResource parent)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> RemoveParent(IResource child, IResource parent)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncBag<T> Children<T>(IResource resource, string name) where T : IResource
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncBag<T> Parents<T>(IResource resource, string name) where T : IResource
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<KeyList<PropertyTemplate, PropertyValue[]>> GetRecord(IResource resource, DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AsyncReply<bool> Trigger(ResourceTrigger trigger)
|
||||
{
|
||||
if (trigger == ResourceTrigger.Initialize)// SystemInitialized && DbContext != null)
|
||||
{
|
||||
|
||||
if (DbContextProvider == null)
|
||||
DbContextProvider = () => Activator.CreateInstance(Options.Options.ContextType, Options.Options) as DbContext;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
23
Esiur.Stores.EntityCore/Esiur.Stores.EntityCore.csproj
Normal file
23
Esiur.Stores.EntityCore/Esiur.Stores.EntityCore.csproj
Normal file
@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<AssemblyName>Esiur.Stores.EntityCore</AssemblyName>
|
||||
<Authors>Ahmed Kh. Zamil</Authors>
|
||||
<Company>Esiur Foundation</Company>
|
||||
<Description>Esiur for Entity Framework Core</Description>
|
||||
<Product>Esiur Entity Framework Extension</Product>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageId>Esiur.Stores.EntityCore</PackageId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
|
||||
<PackageReference Include="System.Collections" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Esiur\Esiur.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
113
Esiur.Stores.EntityCore/EsiurExtensionOptions.cs
Normal file
113
Esiur.Stores.EntityCore/EsiurExtensionOptions.cs
Normal file
@ -0,0 +1,113 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
|
||||
using Microsoft.EntityFrameworkCore.Utilities;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using System.Reflection;
|
||||
using Esiur.Proxy;
|
||||
|
||||
namespace Esiur.Stores.EntityCore
|
||||
{
|
||||
public class EsiurExtensionOptions : 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);
|
||||
//}
|
||||
|
||||
|
||||
|
||||
private DbContextOptionsExtensionInfo _info;
|
||||
EntityStore _store;
|
||||
|
||||
public DbContextOptionsExtensionInfo Info => _info;
|
||||
|
||||
public EntityStore Store => _store;
|
||||
|
||||
|
||||
public void ApplyServices(IServiceCollection services)
|
||||
{
|
||||
// services.AddEntityFrameworkProxies();
|
||||
|
||||
new EntityFrameworkServicesBuilder(services)
|
||||
.TryAdd<IConventionSetPlugin, EsiurPlugin>();
|
||||
}
|
||||
|
||||
public void Validate(IDbContextOptions options)
|
||||
{
|
||||
var internalServiceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider;
|
||||
if (internalServiceProvider != null)
|
||||
{
|
||||
var scope = internalServiceProvider.CreateScope();
|
||||
var conventionPlugins = scope.ServiceProvider.GetService<IEnumerable<IConventionSetPlugin>>();
|
||||
if (conventionPlugins?.Any(s => s is EsiurPlugin) == false)
|
||||
{
|
||||
throw new InvalidOperationException("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public EsiurExtensionOptions(EntityStore store)
|
||||
{
|
||||
_info = new ExtensionInfo(this);
|
||||
_store = store;
|
||||
}
|
||||
|
||||
|
||||
private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
|
||||
{
|
||||
|
||||
public ExtensionInfo(IDbContextOptionsExtension extension)
|
||||
: base(extension)
|
||||
{
|
||||
}
|
||||
|
||||
private new EsiurExtensionOptions Extension
|
||||
=> (EsiurExtensionOptions)base.Extension;
|
||||
|
||||
public override bool IsDatabaseProvider => false;
|
||||
|
||||
public override string LogFragment => "Esiur";
|
||||
|
||||
public override long GetServiceProviderHashCode() => 2312;
|
||||
|
||||
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
131
Esiur.Stores.EntityCore/EsiurExtensions.cs
Normal file
131
Esiur.Stores.EntityCore/EsiurExtensions.cs
Normal file
@ -0,0 +1,131 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esiur.Resource;
|
||||
using Esiur.Security.Permissions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Stores.EntityCore
|
||||
{
|
||||
public static class EsiurExtensions
|
||||
{
|
||||
//public static T CreateResource<T>(this DbContext dbContext, object properties = null) where T:class,IResource
|
||||
//{
|
||||
// return dbContext.GetInfrastructure().CreateResource<T>(properties);
|
||||
|
||||
//}
|
||||
|
||||
public static T AddResource<T>(this DbSet<T> dbSet, object properties = null) where T : class, IResource
|
||||
{
|
||||
var store = dbSet.GetInfrastructure().GetService<IDbContextOptions>().FindExtension<EsiurExtensionOptions>().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);
|
||||
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
|
||||
{
|
||||
var options = serviceProvider.GetService<IDbContextOptions>().FindExtension<EsiurExtensionOptions>();
|
||||
|
||||
var resource = Warehouse.New<T>("", options.Store, null, null, null, properties);
|
||||
|
||||
resource.Instance.Managers.AddRange(options.Store.Instance.Managers.ToArray());
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
public static DbContextOptionsBuilder UseEsiur(this DbContextOptionsBuilder optionsBuilder,
|
||||
//DbContext context,
|
||||
string name = null,
|
||||
IResource parent = null,
|
||||
IPermissionsManager manager = null,
|
||||
Func<DbContext> dbContextProvider = null
|
||||
)
|
||||
{
|
||||
var extension = optionsBuilder.Options.FindExtension<EsiurExtensionOptions>();
|
||||
|
||||
if (extension == null)
|
||||
{
|
||||
|
||||
var store = Warehouse.New<EntityStore>(name, null, parent, manager, new { Options = optionsBuilder, DbContextProvider = dbContextProvider });
|
||||
extension = new EsiurExtensionOptions(store);
|
||||
//store.Options = optionsBuilder;
|
||||
//store.DbContext = context;
|
||||
}
|
||||
|
||||
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
|
||||
|
||||
return optionsBuilder;
|
||||
|
||||
}
|
||||
|
||||
public static DbContextOptionsBuilder<TContext> UseEsiur<TContext>(
|
||||
this DbContextOptionsBuilder<TContext> optionsBuilder,
|
||||
//DbContext context,
|
||||
string name = null,
|
||||
IResource parent = null,
|
||||
IPermissionsManager manager = null,
|
||||
Func<DbContext> dbContextProvider = null)
|
||||
where TContext : DbContext
|
||||
{
|
||||
|
||||
|
||||
var extension = optionsBuilder.Options.FindExtension<EsiurExtensionOptions>();
|
||||
|
||||
if (extension == null)
|
||||
{
|
||||
var store = Warehouse.New<EntityStore>(name, null, parent, manager, new { Options = optionsBuilder, DbContextProvider = dbContextProvider });
|
||||
extension = new EsiurExtensionOptions(store);
|
||||
//store.Options = optionsBuilder;
|
||||
//store.Options = extension;
|
||||
//store.DbContext = context;
|
||||
}
|
||||
|
||||
|
||||
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
|
||||
|
||||
return optionsBuilder;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
60
Esiur.Stores.EntityCore/EsiurPlugin.cs
Normal file
60
Esiur.Stores.EntityCore/EsiurPlugin.cs
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
|
||||
//using Microsoft.EntityFrameworkCore.Proxies.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Stores.EntityCore
|
||||
{
|
||||
public class EsiurPlugin : IConventionSetPlugin
|
||||
{
|
||||
private readonly IDbContextOptions _options;
|
||||
private readonly ProviderConventionSetBuilderDependencies _conventionSetBuilderDependencies;
|
||||
|
||||
public EsiurPlugin(
|
||||
IDbContextOptions options,
|
||||
ProviderConventionSetBuilderDependencies conventionSetBuilderDependencies)
|
||||
{
|
||||
_options = options;
|
||||
_conventionSetBuilderDependencies = conventionSetBuilderDependencies;
|
||||
}
|
||||
|
||||
|
||||
public ConventionSet ModifyConventions(ConventionSet conventionSet)
|
||||
{
|
||||
var extension = _options.FindExtension<EsiurExtensionOptions>();
|
||||
conventionSet.ModelFinalizedConventions.Add(new EsiurProxyRewrite(
|
||||
extension,
|
||||
_conventionSetBuilderDependencies));
|
||||
return conventionSet;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
149
Esiur.Stores.EntityCore/EsiurProxyRewrite.cs
Normal file
149
Esiur.Stores.EntityCore/EsiurProxyRewrite.cs
Normal file
@ -0,0 +1,149 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Esiur.Proxy;
|
||||
using Esiur.Resource;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
|
||||
namespace Esiur.Stores.EntityCore
|
||||
{
|
||||
public class EsiurProxyRewrite : IModelFinalizedConvention
|
||||
{
|
||||
private static readonly MethodInfo _createInstance
|
||||
= typeof(EsiurProxyRewrite).GetTypeInfo().GetDeclaredMethod(nameof(EsiurProxyRewrite.CreateInstance));
|
||||
|
||||
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,
|
||||
object[] properties
|
||||
|
||||
// ILazyLoader loader,
|
||||
// object[] constructorArguments,
|
||||
//DbContext context,
|
||||
)
|
||||
{
|
||||
///var id = constructorArguments.Last();
|
||||
var id = properties.First();
|
||||
|
||||
var options = dbContextOptions.FindExtension<EsiurExtensionOptions>();
|
||||
var manager = options.Store.Instance.Managers.Count > 0 ? options.Store.Instance.Managers.First() : null;
|
||||
|
||||
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 EsiurProxyRewrite(EsiurExtensionOptions ext, ProviderConventionSetBuilderDependencies conventionSetBuilderDependencies)
|
||||
{
|
||||
_directBindingConvention = new ConstructorBindingConvention(conventionSetBuilderDependencies);
|
||||
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
#pragma warning disable EF1001 // Internal EF Core API usage.
|
||||
var binding = (InstantiationBinding)entityType[CoreAnnotationNames.ConstructorBinding];
|
||||
#pragma warning restore EF1001 // Internal EF Core API usage.
|
||||
if (binding == null)
|
||||
_directBindingConvention.ProcessModelFinalized(modelBuilder, context);
|
||||
|
||||
#pragma warning disable EF1001 // Internal EF Core API usage.
|
||||
binding = (InstantiationBinding)entityType[CoreAnnotationNames.ConstructorBinding];
|
||||
#pragma warning restore EF1001 // Internal EF Core API usage.
|
||||
|
||||
|
||||
try
|
||||
|
||||
{
|
||||
entityType.SetAnnotation(
|
||||
#pragma warning disable EF1001 // Internal EF Core API usage.
|
||||
CoreAnnotationNames.ConstructorBinding,
|
||||
#pragma warning restore EF1001 // Internal EF Core API usage.
|
||||
new FactoryMethodBinding(
|
||||
_createInstance,
|
||||
new List<ParameterBinding>
|
||||
{
|
||||
new DependencyInjectionParameterBinding(typeof(IDbContextOptions), typeof(IDbContextOptions)),
|
||||
new EntityTypeParameterBinding(),
|
||||
// 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
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
8
Esiur.Stores.EntityCore/Properties/launchSettings.json
Normal file
8
Esiur.Stores.EntityCore/Properties/launchSettings.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Esiur.Stores.EntityCore": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "--migrate"
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user