阻止 Nhibernate 更新特定列?

发布于 2024-10-22 00:44:39 字数 1153 浏览 1 评论 0原文

我正在使用 NHibernate 编写一个 3 层应用程序,其中帐户密码以哈希形式保存在数据库中。

表示层在我的服务中调用一个保存方法,该方法需要一个帐户实体,其中密码在提供给存储库之前会被散列。

因此,第一次保存帐户时,密码会被正确散列,一切都很好。

但是,一旦我必须更新帐户,而不更改密码,帐户实体中的密码属性的值已经被散列,当它通过我的服务的保存方法传递时,它将再次散列。

我的实体如下所示:

public class AccountEntity : EntityBase {
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }
    public virtual string Email { get; set; }
    public virtual bool Enabled { get; set; }
}

我的服务具有保存方法:

public class AccountService {
    private readonly IRepository<AccountEntity> _accountRepository;
    private readonly IHashingProvider _hashingProvider;

    public AccountService(IRepository<AccountEntity> accountRepository, IHashingProvider hashingProvider) {
        _accountRepository = accountRepository;
        _hashingProvider = hashingProvider;
    }

    public void Save(AccountEntity accountEntity) {
        accountEntity.Password = _hashingProvider.Hash(accountEntity.Password);
        _accountRepository.Save(accountEntity);
    }
}

我怎样才能告诉 NHibernate 不要更新密码?我还有哪些其他的可能性?

I'm writing a 3-tier application using NHibernate where account passwords are saved as hash in a database.

The presentation layer calls a save method, that wants an account entity, in my service, where the password gets hashed before giving it to the repository.

So, first time an account gets saved, the password gets correctly hashed and everything is fine.

But once I have to update an account, without changing the password, the value of the password property in the account entity, is already hashed, when it gets passed through the save method of my service, it will hashed again.

My entity looks like this:

public class AccountEntity : EntityBase {
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }
    public virtual string Email { get; set; }
    public virtual bool Enabled { get; set; }
}

And my service with the save method:

public class AccountService {
    private readonly IRepository<AccountEntity> _accountRepository;
    private readonly IHashingProvider _hashingProvider;

    public AccountService(IRepository<AccountEntity> accountRepository, IHashingProvider hashingProvider) {
        _accountRepository = accountRepository;
        _hashingProvider = hashingProvider;
    }

    public void Save(AccountEntity accountEntity) {
        accountEntity.Password = _hashingProvider.Hash(accountEntity.Password);
        _accountRepository.Save(accountEntity);
    }
}

How can I tell NHibernate to NOT update the password? What other possibilities do I have?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倦话 2024-10-29 00:44:39

我建议以不涉及 NHibernate 层的方式解决这个问题。只需将“首次创建”操作(需要哈希)与“任何更新的常规保存”操作(不需要哈希)分开。

例如,向您的 AccountService 添加一个 Create() 方法以用于首次创建帐户,该方法可以进行哈希处理并委托给 Save()。然后,通用的 Save() 就可以正常地保存对象。

您还可以添加一个方法来保存密码更改,该方法可以通过哈希运行新请求的密码并调用 Save()

I'd suggest tackling this in a way that doesn't deal with the NHibernate layer. Simply separate the "first-time creation" operation (which requires hashing) from the "general save of any updates" operation (which doesn't need hashing).

For example, add a Create() method to your AccountService for creating the account the first time, which can do the hashing and delegate to Save(). The general Save() can then just persist the object normally.

You could also add a method for persisting a change in password, which can run the newly-requested password through the hash and calling Save().

送君千里 2024-10-29 00:44:39

您可以使用会话拦截器并使用 OnSave 函数将属性值重新设置为原始值

You can use a session interceptor and work on the OnSave function to re-set the property value at the original one

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文