nhibernate:如何设置使用数据库数据的实体验证?
我已经设置了 nhibernate,但在实际保存并提交到数据库之前验证实体时遇到问题。当我加载和更改持久实体时,随后的验证将访问数据库并通过这样做提交事务,从而在知道实际验证结果之前将实体保存在数据库中。
我在 .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");
}
}
更新:澄清 好的,这是代码:
// get persistent entity from nhibernate
var entityOriginal = repository.GetById(id);
entity.CopyDataToEntity(entityOriginal);
ValidationResult validationResult = entityOriginal.Validate();
- 我从数据库中获取持久实体
- ,我对其进行更改...现在,如果会话被刷新,它将保留在数据库中,
- 我执行entity.Validate(),它从数据库获取另一个实体并在进程中提交(正如您所言)即使从数据库获取也必须提交)!...所以我尝试验证的实体在验证本身期间保留在数据库中。
所以问题是 Entity.Validate() 检查数据库中是否有正确输入的数据,并且由于即使在 session.Get() 上也提交了事务,所以更改的实体会立即保存(有效或无效)。
我的存储库实现是经典的:
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;
}
如何实现可以检查数据库中内容的验证?
可能的解决方案是在对持久对象进行任何更改之前进行验证,但事实就是如此做起来很痛苦,一定有更好的方法。感谢您的帮助。
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.
I use Repository pattern in .NET MVC web app and it goes like this:
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();
- I get the persistent entity from the database
- I change it... now if session is flushed it will be persisted in database
- I do entity.Validate() which gets another entity from database and commits in process (as you have to commit even for fetching from database)!... so entity I try to validate is persisted in database during the validation itself.
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.
My repository implementation is classic:
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;
}
How can I achieve validation that can check stuff in database?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现的解决方案非常丑陋,但它有效:
仍在寻找不过更好的解决方案。
The solution I've found is very ugly but it works:
Still looking for a better solution though.