我可以使用CMS果园内我Ninject .NET项目? [英] Can I use my Ninject .NET project within Orchard CMS?

查看:175
本文介绍了我可以使用CMS果园内我Ninject .NET项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用CMS果园一个网站,我有Ninject编写了依赖注入,我愿与乌节CMS中的模块一起使用外部.NET项目。我知道,果园使用Autofac依赖注入,这引起了我的问题,因为我从来没有与DI工作过。

I am creating a website using Orchard CMS and I have an external .NET project written with Ninject for dependency injection which I would like to use together with a module within Orchard CMS. I know that Orchard uses Autofac for dependency injection and this is causing me problems since I never worked with DI before.

我已经创建了一个Autofac模块, UserModule ,其注册的来源, UserRegistrationSource ,就像这样:

I have created an Autofac module, UserModule, which registers the a source, UserRegistrationSource, like this:

UserModule.cs

public class UserModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterSource(new UserRegistrationSource());
    }
}

UserRegistrationSource.cs

public class UserRegistrationSource : IRegistrationSource
{
    public bool IsAdapterForIndividualComponents
    {
        get { return false; }
    }

    public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
    {
        var serviceWithType = service as IServiceWithType;
        if (serviceWithType == null)
            yield break;

        var serviceType = serviceWithType.ServiceType;
        if (!serviceType.IsInterface || !typeof(IUserServices).IsAssignableFrom(serviceType) || serviceType != typeof(IUserServices))
            yield break;

        var registrationBuilder = // something...

        yield return registrationBuilder.CreateRegistration();
    }
}

UserServices.cs

public interface IUserServices : IDependency
{
    void Add(string email, string password);
}

public class UserServices : IUserServices
{
    private readonly EFMembershipManager _manager;

    public UserServices(EFMembershipManager manager)
    {
        _manager = manager;
    }

    public void Add(string email, string password)
    {
        _manager.createUser(email, password);
    }
}

EFMembershipManager.cs 构造

public EFMembershipManager(ServerRepository db,
                           ServerRepositoryMembershipProvider membershipProvider,
                           string testUsername,
                           string serverUsername)
{
...
}

EFMembershipManager 是它使用Ninject对DI的和用途 ServerRepository 和<$ C $外部项目的类C> ServerRepositoryMembershipProvider 也为之所使用Ninject注入。

EFMembershipManager is a class from the external project which uses Ninject for DI's and uses ServerRepository and ServerRepositoryMembershipProvider whom also are injected using Ninject.

而现在我被困...

应该 UserRegistrationSource 乘坐Ninject容器(内核),构造函数的参数,并试图找到 IUserServices 服务然后调解解析为Ninject内核并返回一个空的可枚举,这样Autofac不会试图解决与 IUserServices 任何东西,或这是错误的做法?

Should UserRegistrationSource take the Ninject container (kernel) as a constructor argument and try to find the IUserServices service and then mediate the resolves to the Ninject kernel and return an empty Enumerable so that Autofac doesn't try to resolve anything related to IUserServices or is this the wrong approach?

推荐答案

Autofac支持登记源(多就注册来源这里)。的注册源是试图解决一个类型时,容器会咨询服务。源可以响应,无论是与一个装置建立表明该源是不能够提供所请求的类型的类型,或空列表。

Autofac supports registration sources (and more on registration sources here). A registration source is a service that the container will consult when trying to resolve a type. The source can respond, either with a means to build the type, or an empty list which indicates that the source is not able to provide the requested type.

在你的情况下,注册源可以实现,这将尝试解决您Ninject容器请求的类型。

In your case, a registration source could be implemented that will try to resolve the requested type from your Ninject container.

我不是太熟悉,果园,但我猜测它使用的配置文件来配置Autofac。我的建议是,你创建的注册您的注册简单Autofac模块代码实现,并且配置果园加载从配置模块

I'm not too familiar with Orchard but I'm guessing that it uses configuration files to configure Autofac. My suggestion is that you create a simple Autofac module that registers your registration source implementation, and that you configure Orchard to load the module from config.

这篇关于我可以使用CMS果园内我Ninject .NET项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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