Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq to sql 需要使用Castle Dynamic Proxy(可能是AutoMapper)截获所有LINQ到SQL实体_Linq To Sql_Aop_Automapper_Castle Dynamicproxy - Fatal编程技术网

Linq to sql 需要使用Castle Dynamic Proxy(可能是AutoMapper)截获所有LINQ到SQL实体

Linq to sql 需要使用Castle Dynamic Proxy(可能是AutoMapper)截获所有LINQ到SQL实体,linq-to-sql,aop,automapper,castle-dynamicproxy,Linq To Sql,Aop,Automapper,Castle Dynamicproxy,当存储在数据库中时,我需要加密一组特定字段的值。 我正在使用LINQtoSQL 我的方法是:在将实体中匹配属性的值写入数据库之前,对其进行透明加密 我已经用Castle Dynamic Proxy编写了一个拦截器,它将加密setter上的相关属性,并在getter上解密它。以下是我如何使用它: var secretEntity = <Get a SecretEntity from the DataContext>; ProxyGenerator pg = new ProxyGener

当存储在数据库中时,我需要加密一组特定字段的值。 我正在使用LINQtoSQL

我的方法是:在将实体中匹配属性的值写入数据库之前,对其进行透明加密

我已经用Castle Dynamic Proxy编写了一个拦截器,它将加密setter上的相关属性,并在getter上解密它。以下是我如何使用它:

var secretEntity = <Get a SecretEntity from the DataContext>;
ProxyGenerator pg = new ProxyGenerator();
// (Typing from memory here, so excuse possible method errors, but you get the gist)
// Reassign to now reference the dynamic proxy.
secretEntity  = pg.CreateProxyWithTarget (secretEntity , new EncryptionInterceptor());
secretEntity.BigSecret = "Silentium"; // Does the encryption via the interceptor.
var decryptedSecret = secretEntity.BigSecret; // Does the decryption  via the interceptor.
但是没有运气。我假设这是因为一旦BeforeMap方法完成运行,将“se”引用重新分配给代理就没有任何效果

当创建一个新的秘密实体时,我可以通过使用Ninject自动化代理包装过程,但Ninject不会处理我从DataContext返回的现有实体

我和Castle Dynamic Proxy一起散步只有几个小时,AutoMapper对我了解不多。所以我希望有人能给我一个快速的指示,告诉我去哪里找

多谢各位

编辑

为了完整性起见,我想我应该为那些可能感兴趣的人添加拦截器的实现。由于不太了解Castle Dynamic Proxy,我相信可能有更好的方法来处理拦截并检测它是getter还是setter,等等。 以下是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
using TunedIn.Base.Model;
using TunedIn.Base.Ninject;
using TunedIn.Base.Extensions.StringExtensions;

namespace TunedIn.Base.Encryption
{
    public class PropertyEncryptionInterceptor : IInterceptor
    {
        public void Intercept (IInvocation invocation)
        {
            IPersonEncryptedFields p = invocation.InvocationTarget as IPersonEncryptedFields;
            if (p == null)
                throw new ApplicationException ("{0} expects the InvocationTarget of the dynamic proxy binding to implement {1}, but {2} does not.".FormatI (typeof (PropertyEncryptionInterceptor).FullName, typeof (IPersonEncryptedFields).FullName, invocation.InvocationTarget.GetType ().FullName));

            if (invocation.Method.Name.StartsWith ("set_"))
            {
                string val = (string)invocation.GetArgumentValue (0);
                val = Kernel.Get<IStringCrypto> ().Encrypt (val);
                invocation.SetArgumentValue (0, val);
                invocation.Proceed ();
            }
            else if (invocation.Method.Name.StartsWith ("get_"))
            {
                invocation.Proceed ();
                string ret = invocation.ReturnValue.ToString ();
                ret = Kernel.Get<IStringCrypto> ().Decrypt (ret);
                invocation.ReturnValue = ret;
            }
            else
                invocation.Proceed ();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Castle.DynamicProxy;
使用TunedIn.Base.Model;
使用TunedIn.Base.Ninject;
使用TunedIn.Base.Extensions.StringExtensions;
命名空间TunedIn.Base.Encryption
{
公共类属性加密接收器:I接收器
{
公共无效拦截(IInvocation调用)
{
IPersoneCryptedFields p=invocation.InvocationTarget作为IPersoneCryptedFields;
if(p==null)
抛出新的ApplicationException(“{0}期望动态代理绑定的调用目标实现{1},但{2}没有。”.FormatI(typeof(PropertyEncryptionInterceptor).FullName,typeof(ipersoneCryptedFields).FullName,invocation.InvocationTarget.GetType().FullName));
if(invocation.Method.Name.StartsWith(“set_”))
{
string val=(string)invocation.GetArgumentValue(0);
val=Kernel.Get().Encrypt(val);
invocation.SetArgumentValue(0,val);
procedure();
}
else if(invocation.Method.Name.StartsWith(“get_”))
{
procedure();
string ret=invocation.ReturnValue.ToString();
ret=Kernel.Get().Decrypt(ret);
invocation.ReturnValue=ret;
}
其他的
procedure();
}
}
}

也许我过于简单化了,但是仅仅向允许加密和解密的部分
机密性添加一个新属性就足够了吗?您可以将LINQ to SQL生成的原始属性设置为内部属性:

public partial class SecretEntity
{
    public string BigSecret
    {
        get { return Decrypt(this.BigSecretInternal); }
        set { this.BigSecretInternal = Encrypt(value); }
    }
}

这个答案太简单了!;)说真的,我没有想到内部LINQ-to_SQL实体属性。好吧,至少我现在知道如何使用Castle动态代理。塔克斯。出于兴趣,我仍然想知道一种将我的实体与动态代理连接起来的方法。@Jacques:值得一试;-)。恐怕我帮不了你。
public partial class SecretEntity
{
    public string BigSecret
    {
        get { return Decrypt(this.BigSecretInternal); }
        set { this.BigSecretInternal = Encrypt(value); }
    }
}