版本并发控制长期会话。 NHibernate的。 ASP.NET MVC [英] version concurrency control for a long session. NHibernate. ASP.NET MVC

查看:79
本文介绍了版本并发控制长期会话。 NHibernate的。 ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)什么是并发控制更合适的/常用的方法呢?悲观或乐观锁?

1) What is more appropriate/commonly used approach for concurrency control? Pessimistic or optimistic locking?

2)我如何通知用户,如果有一个项目锁定或回滚是否发生?

2) How do I notify a user if there's a lock on an item or whether rollback had occured?

3)假设我已经添加了所有需要的标记(如乐观锁定=假来,我不希望被卷入比较属性),以排除我的实体的映射文件的定义,并在实体类的处理并发控制版本属性。这样够了,所有的东西是由 NHibernate的内/处理。或附加modifiactions应该是present?例如,在

3) Suppose I've added all needed markup (like optimistic-locking="false" to exclude properties I don't want to be involved in comparing) to my entity mapping files and defined a Version property on entity classes to handle concurrency control. Would that be enough and all stuff is processed within/by NHibernate. Or additional modifiactions should be present? For example in Repository?

public class Repository<T> : IRepository<T> where T : AbstractEntity<T>, IAggregateRoot
{
    private ISession session;

    public Repository(ISession session)
    {
        this.session = session;
    }

    public T Get(Guid id)
    {            
        return this.session.Get<T>(id);            
    }
    public IQueryable<T> Get(Expression<Func<T, Boolean>> predicate)
    {
        return this.session.Query<T>().Where(predicate);            
    }
    public IQueryable<T> Get()
    {
        return this.session.Query<T>();            
    }
    public T Load(Guid id)
    {
        return this.session.Load<T>(id);
    }
    public void Add(T entity)
    {            
        using(var transaction = this.session.BeginTransaction())
        {
            this.session.Save(entity);
            transaction.Commit();
        }
    }
    public void Remove(T entity)
    {            
        using(var transaction = this.session.BeginTransaction())
        {
            this.session.Delete(entity);
            transaction.Commit();
        }
    }
    public void Remove(Guid id)
    {            
        using(var transaction = this.session.BeginTransaction())
        {
            this.session.Delete(this.session.Load<T>(id));
            transaction.Commit();
        }
    }
    public void Update(T entity)
    {            
        using(var transaction = this.session.BeginTransaction())
        {
            this.session.Update(entity);
            transaction.Commit();
        }
    }
    public void Update(Guid id)
    {            
        using(var transaction = this.session.BeginTransaction())
        {
            this.session.Update(this.session.Load<T>(id));
            transaction.Commit();
        }
    }
}

// DI
public class DataAccessModule : Ninject.Modules.NinjectModule
{
    public override void Load()
    {
        this.Bind<ISessionFactory>()
            .ToMethod(c => new Configuration().Configure().BuildSessionFactory())
            .InSingletonScope();

        this.Bind<ISession>()
            .ToMethod(ctx => ctx.Kernel.TryGet<ISessionFactory>().OpenSession())
            .InRequestScope();

        this.Bind(typeof(IRepository<>)).To(typeof(Repository<>));
    }
}

我使用的是多次交易很长的会话。

I'm using a long session with multiple transactions.

谢谢!

推荐答案

您库不应该处理事务范围。它是一种完全不同的需求和库不能知道界限应该事务中有。

Your repository should never handle transaction scopes. Its a totally different requirement and Repository cant know what boundaries should transaction be have.

交易应受基础设施code之外的某处进行处理。如果你正在使用ASP.NET MVC - 行动过滤器是合适的(见实施是夏普Architecture项目)

Transactions should be handled somewhere outside by infrastructure code. If you are using ASP.NET MVC - action filters are appropriate(see implementation is Sharp Architecture project).

如果它的ASP.NET然后模块或全球ASAX处理可能被应用。

If its ASP.NET then module or global asax handling could be applied.

但就是不处理它在存储库,它很抽象泄漏,则需要暴露边界调用者。

But just DON'T handle it in repository, its very leaky abstraction, you WILL need expose boundaries to caller.

这篇关于版本并发控制长期会话。 NHibernate的。 ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆