你怎么能使用Ninject注入一个asp.net(MVC2)自定义成员资格的供应商? [英] How can you inject an asp.net (mvc2) custom membership provider using Ninject?

查看:129
本文介绍了你怎么能使用Ninject注入一个asp.net(MVC2)自定义成员资格的供应商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我一直工作在这几个小时。我发现一对夫妇的帖子在这里,但没有实际解决该问题。因此,让我再试试吧...

OK, so I've been working on this for hours. I've found a couple of posts here, but nothing that actually resolves the problem. So, let me try it again...

我在使用Ninject和一个自定义的成员提供一个MVC2应用程序。

I have an MVC2 app using Ninject and a custom membership provider.

如果我尝试使用构造函数注入的供应商,我得到一个错误:'此对象定义无参数的构造函数

If I try and inject the provider using the ctor, I get an error: 'No parameterless constructor defined for this object.'

public class MyMembershipProvider : MembershipProvider
{
    IMyRepository _repository;

    public MyMembershipProvider(IMyRepository repository)
    {
        _repository = repository;
    }

我也一直在与工厂和初始化()玩耍,但一切都来了空白。

I've also been playing around with factories and Initialize(), but everything is coming up blanks.

有什么想法/例子吗?

推荐答案

这是我是如何能够做到这一点:

This is how I was able to do this:

1)我创建了一个静态辅助类Ninject

1) I created a static helper class for Ninject

public static class NinjectHelper
{
    public static readonly IKernel Kernel = new StandardKernel(new FooServices());

    private class FooServices : NinjectModule
    {
        public override void Load()
        {
            Bind<IFooRepository>()
                .To<EntityFooRepository>()
                .WithConstructorArgument("connectionString",
                    ConfigurationManager.ConnectionStrings["FooDb"].ConnectionString);
        }
    }
}

2)这是我的会员覆盖:

2) Here is my Membership override:

    public class FooMembershipProvider : MembershipProvider
    {
        private IFooRepository _FooRepository;

        public FooMembershipProvider()
        {
            NinjectHelper.Kernel.Inject(this);
        }

        [Inject]
        public IFooRepository Repository 
        { 
            set
            {
                _FooRepository = value;
            }
        }
        ...

通过这种方式它没有当会员提供实例化真正的问题。

With this approach it doesn't really matter when the Membership provider is instantiated.

这篇关于你怎么能使用Ninject注入一个asp.net(MVC2)自定义成员资格的供应商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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