Nhibernate 和 Azure Blob for Binary 的分片模式

Sharding pattern with Nhibernate and Azure Blob for Binary

我实际上是用 ASP.Net MVC 和 NHibernate 开发一个网站。

您可以找到我在 GitHub 上使用的解决方案基础示例:Pixel.Sample

我有图片存储库来管理 NHibernate Microsoft SQL 实体。 我的 PictureManager 调用我的 PictureRepository 来 Save() 它。 我想在 Azure CloudStorage blob 中为图片和 thunbail 分片我的二进制文件,所以我没有在我的 ClassMap 上映射二进制字段,但是如何在我当前的体系结构中捕获我的保存更新删除以仅保存不同支持的二进制文件(CloudStorage)

实际上我正在尝试为此找到解决方案,有什么想法吗?

更新

经过Pelican的相关回答,我想我会使用LazyLoad和Proxy来加载Binary和Interceptor来保存/更新/删除

看看我的

您可以使用 NHibernate events,

public class TestInterceptor : EmptyInterceptor {

    private int updates;
    private int creates;
    private int loads;

    public override void OnDelete(object entity,
                                  object id,
                                  object[] state,
                                  string[] propertyNames,
                                  IType[] types)
    {
    }

    public override bool OnFlushDirty(object entity, 
                                      object id, 
                      object[] currentState,
                      object[] previousState, 
                      string[] propertyNames,
                      IType[] types) 
    {
    }

    public override bool OnLoad(object entity, 
                                object id, 
                object[] state, 
                string[] propertyNames, 
                IType[] types)
    {
    }

    public override bool OnSave(object entity, 
                                object id, 
                object[] state, 
                string[] propertyNames, 
                IType[] types)
    {
    }

    public override void AfterTransactionCompletion(ITransaction tx)
    {
        if ( tx.WasCommitted ) {
        }
    }
}

你可以注册了,

new Configuration().SetInterceptor( new TestInterceptor() );

谢谢 Pelican 帮我解答这个问题,我刚刚找到了我想要的:

NHibernate.Proxy.DynamicProxy.ProxyFactory

在 IInterceptor.Instance 上,您应该选择 class 您要放置代理

    public override Object Instantiate(String clazz, EntityMode entityMode, Object id)
    {
        if (entityMode == EntityMode.Poco)
        {
            Type type = Type.GetType(clazz, false);

            if (type != null)
            {
                Object instance = CreateProxy(type);

                this.session.SessionFactory.GetClassMetadata(clazz).SetIdentifier(instance, id, entityMode);

                return (instance);
            }
        }

        return (base.Instantiate(clazz, entityMode, id));
    }


    public static Object CreateProxy(Type type)
    {
        List<Type> interfaces = new List<Type>();
        //TODO: add interfaces to list
        interfaces.Add(typeof(IBar));

        Object instance = null;

        if ((interfaces.Count != 0) && (type.IsSealed == false))
        {
            //TODO: pass any custom parameters to the _CustomInterceptor class
            instance = proxyGenerator.CreateProxy(type, new CustomPictureInterceptor(), interfaces.ToArray());
        }
        else
        {
            instance = Activator.CreateInstance(type);
        }

        return (instance);
    }

在拦截方法上你可以做你想做的事:

  class CustomPictureInterceptor : NHibernate.Proxy.DynamicProxy.IInterceptor
    {
        public object Intercept(InvocationInfo info)
        {
            //Do what you want
            return info.InvokeMethodOnTarget();
        }
    }

参考文献:

http://weblogs.asp.net/ricardoperes/nhibernate-interceptor-for-dynamic-proxy-generation

http://kozmic.net/2011/03/20/working-with-nhibernate-without-default-constructors/