我可以在Orchard CMS中使用我的Ninject .NET项目吗?

时间:2021-12-15 22:44:41

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.

我正在使用Orchard CMS创建一个网站,我有一个用Ninject编写的外部.NET项目用于依赖注入,我想与Orchard CMS中的模块一起使用。我知道Orchard使用Autofac进行依赖注入,这引起了我的问题,因为我之前从未使用过DI。

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

我创建了一个Autofac模块UserModule,它注册了一个源UserRegistrationSource,如下所示:

UserModule.cs

UserModule.cs

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

UserRegistrationSource.cs

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

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 constructor

EFMembershipManager.cs构造函数

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

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.

EFMembershipManager是外部项目中的一个类,它使用Ninject作为DI,并使用ServerRepository和ServerRepositoryMembershipProvider,它们也使用Ninject注入。

And now I'm stuck...

而现在我被卡住了......

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?

UserRegistrationSource应该将Ninject容器(内核)作为构造函数参数,并尝试找到IUserServices服务,然后将解析调解到Ninject内核并返回一个空的Enumerable,以便Autofac不会尝试解析与IUserServices相关的任何内容,或者这是错误的做法?

1 个解决方案

#1


4  

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.

Autofac支持注册源(以及更多关于注册源的信息)。注册源是容器在尝试解析类型时将参考的服务。源可以使用构建类型的方法进行响应,也可以使用空列表来响应,该列表指示源无法提供所请求的类型。

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

在您的情况下,可以实现注册源,尝试从Ninject容器中解析所请求的类型。

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.

我对Orchard不太熟悉,但我猜它使用配置文件来配置Autofac。我的建议是你创建一个简单的Autofac模块来注册你的注册源实现,并且你配置Orchard来从config加载模块。

#1


4  

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.

Autofac支持注册源(以及更多关于注册源的信息)。注册源是容器在尝试解析类型时将参考的服务。源可以使用构建类型的方法进行响应,也可以使用空列表来响应,该列表指示源无法提供所请求的类型。

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

在您的情况下,可以实现注册源,尝试从Ninject容器中解析所请求的类型。

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.

我对Orchard不太熟悉,但我猜它使用配置文件来配置Autofac。我的建议是你创建一个简单的Autofac模块来注册你的注册源实现,并且你配置Orchard来从config加载模块。