nhibernate:如何设置使用数据库中数据的实体验证? [英] nhibernate: how to setup entity validation that uses data from database?

查看:81
本文介绍了nhibernate:如何设置使用数据库中数据的实体验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了nhibernate,但是在实际保存并提交到数据库之前,我在验证实体时遇到了问题.当我加载和更改持久实体时,随后进行的验证是访问数据库并通过提交事务来进行,从而在知道实际验证结果之前将实体保存在数据库中.

I've setup nhibernate but I have problems with validating entity before the actual save and commit to database. As I load and change persistent entity, validation that goes after is accessing the database and by doing so commiting the transaction, thus saving entity in database before the actual validation result is known.

我在.NET MVC Web应用程序中使用存储库模式,它是这样的:

public ActionResult Edit(int id, Entity entity)
{
    var repository = new EntityRepository();

    // get persistent entity from nhibernate
    var entityOriginal = repository.GetById(id);

    // copy data from input entity to persistent entity
    entity.CopyDataToEntity(entityOriginal);

    // if validation ok save to database
    // this validations is problematic becouse it needs info from database and uses transaction
    ValidationResult validationResult = entityOriginal.Validate();
    if (validationResult.IsValid())
    {
        repository.Save(entityOriginal);

        // everything ok, redirect to view entity page
        return RedirectToAction("view");
    }
    else
    {
        repository.Evict(prateciList);
        foreach (var message in validationResult.ErrorMessages)
        {
            ModelState.AddModelError(message.Key, message.Value);
        }
        return RedirectToAction("pogledaj");
    }
}

更新:澄清 好的,这是代码:

UPDATE: clarification ok here's the code:

// get persistent entity from nhibernate
var entityOriginal = repository.GetById(id);
entity.CopyDataToEntity(entityOriginal);
ValidationResult validationResult = entityOriginal.Validate();

  1. 我从数据库中获取了持久实体
  2. 我更改它...现在,如果刷新会话,它将保留在数据库中
  3. 我执行entity.Validate(),它从数据库中获取另一个实体并进行处理(因为即使从数据库中获取数据,您也必须进行提交)!...因此,我尝试验证的实体在验证过程中一直存在于数据库中

所以问题是Entity.Validate()检查数据库中是否正确输入了数据,并且由于即使在session.Get()上也提交了事务,因此更改的实体就保存在其中(有效或无效).

So the problem is that Entity.Validate() check the database for correctly inputed data, and since transaction is commited even on session.Get(), changed entity is saved (valid or invalid) right there.

我的存储库实施非常经典:

public virtual bool Save(T entity)
    {
        var session = SessionProvider.GetRequestSession(this.DatabaseName);
        using (var transaction = session.BeginTransaction())
        {
            try
            {
                session.SaveOrUpdate(entity);
                transaction.Commit();
            }
            catch (Exception ex)
            {
                _log.Error("Could not save object to database.", ex);
                if (transaction.IsActive)
                {
                    transaction.Rollback();

                }
                session.Close();
                return false;
            }
        }

        return true;
    }

如何实现可以检查数据库中内容的验证?

可能的解决方案是在对持久对象进行任何更改之前先进行验证,但这很难做到,必须有更好的方法.谢谢您的帮助.

Possible solution be to validate before I make any changes to persistent object, but this is such a pain to do there must be a better way. Thank you for your help.

推荐答案

我发现的解决方案非常难看,但是可以解决问题:

The solution I've found is very ugly but it works:

  1. 从数据库获取原始实体
  2. 将其克隆(复制所有数据)到新实例
  3. 修改和验证新实例
  4. 如果验证成功,则将所有内容从新实例复制到原始实例并保存

尽管如此,仍在寻找更好的解决方案.

Still looking for a better solution though.

这篇关于nhibernate:如何设置使用数据库中数据的实体验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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