Can I use my Ninject .NET project within Orchard CMS?

為{幸葍}努か 提交于 2020-01-01 03:22:05

问题


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.

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 constructor

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.

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?


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/12143341/can-i-use-my-ninject-net-project-within-orchard-cms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!