注入依赖成域模型类与NHibernate(ASP.NET MVC + IOC) [英] Injecting Dependencies into Domain Model classes with Nhibernate (ASP.NET MVC + IOC)

查看:292
本文介绍了注入依赖成域模型类与NHibernate(ASP.NET MVC + IOC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个使用与NHibernate的处理数据库访问DDD(领域驱动设计)的方法ASP.NET MVC应用程序。我有我想通过注入依赖到域模型类(管理员)IoC容器,如温莎城堡,是这样的:

I'm building an ASP.NET MVC application that uses a DDD (Domain Driven Design) approach with database access handled by NHibernate. I have domain model class (Administrator) that I want to inject a dependency into via an IOC Container such as Castle Windsor, something like this:

public class Administrator
{
    public virtual int Id { get; set; }

    //.. snip ..//

    public virtual string HashedPassword { get; protected set; }

    public void SetPassword(string plainTextPassword)
    {
        IHashingService hasher = IocContainer.Resolve<IHashingService>();

        this.HashedPassword = hasher.Hash(plainTextPassword);
    }
}

我基本上要注入IHashingService为setPassword方法,而不直接调用IOC容器(因为这是假设是国际奥委会反模式)。但我不知道如何去这样做。我Administrator对象要么被通过新管理员()实例化; ,或者被通过NHibernate的加载,所以我会如何注入到IHashingService管理员类

I basically want to inject IHashingService for the SetPassword method without calling the IOC Container directly (because this is suppose to be an IOC Anti-pattern). But I'm not sure how to go about doing it. My Administrator object either gets instantiated via new Administrator(); or it gets loaded via NHibernate, so how would I inject the IHashingService into the Administrator class?

在第二个想法,我要对这个正确的方式?我希望能避免我的codeBase的散落着...

On second thoughts, am I going about this the right way? I was hoping to avoid having my codebase littered with...

currentAdmin.Password = HashUtils.Hash(password, Algorithm.Sha512);

......,而是获得域模型本身照顾散列,并整齐地封装它拿走。我可以预见另一名开发人员意外地选择了错误的算法,并有一些密码为SHA512,有的如MD5,有的用一种盐,有的用不同的盐等等,等等相反,如果开发商正在写...

...and instead get the domain model itself to take care of hashing and neatly encapsulate it away. I can envisage another developer accidently choosing the wrong algorithm and having some passwords as Sha512, and some as MD5, some with one salt, and some with a different salt etc. etc. Instead if developers are writing...

currentAdmin.SetPassword(password);

...那么这将隐藏这些细节远,走上述这些问题护理岂不?

...then that would hide those details away and take care of those problems listed above would it not?

推荐答案

有这里已经类似的问题对SO:

There are already similar questions here on SO:

<一个href=\"http://stackoverflow.com/questions/340461/dependency-injection-with-nhibernate-objects\">http://stackoverflow.com/questions/340461/dependency-injection-with-nhibernate-objects

<一个href=\"http://stackoverflow.com/questions/387783/di-ioc-nhibernate-and-help-in-getting-them-to-work-together\">http://stackoverflow.com/questions/387783/di-ioc-nhibernate-and-help-in-getting-them-to-work-together

您需要使用拦截器。看看法比奥Maulo的帖子执行:

You'll need to use Interceptors. Take a look at Fabio Maulo's post for implementation:

<一个href=\"http://nhforge.org/blogs/nhibernate/archive/2008/12/12/entities-behavior-injection.aspx\">http://nhforge.org/blogs/nhibernate/archive/2008/12/12/entities-behavior-injection.aspx

这篇关于注入依赖成域模型类与NHibernate(ASP.NET MVC + IOC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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