我实际上使用NHibernate开发了一个带ASP.Net MVC的网站。
您可以找到我在GitHub上使用的解决方案基础示例:Pixel.Sample
我有图片库,用于管理NHibernate Microsoft SQL实体。 My PictureManager将我的PictureRepository调用为Save()它。 我想在Azure CloudStorage blob中为我的二进制图片和thunbail进行分片,所以我没有在我的ClassMap上映射二进制字段,但是如何在我当前的体系结构中捕获我的保存更新删除以仅在不同的支持中保存二进制(云端储存)
其实我试图为此找到解决方案,任何想法?
更新
在Pelican的相关回答之后,我想我会使用LazyLoad与Proxy一起加载Binary和Interceptor来保存/更新/删除
查看我的answer
答案 0 :(得分:0)
您可以使用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() );
答案 1 :(得分:0)
谢谢Pelican帮我解决这个问题,我找到了我想要的东西:
<强> NHibernate.Proxy.DynamicProxy.ProxyFactory 强>
在IInterceptor.Instance上,您应该选择要放置代理的类
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/