Can I use my Ninject .NET project within Orchard CMS?
- by Mattias Z
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 a Autofac module UserModule which registers the source UserRegistrationSource and I configured Orchard to load the module like this:
OrchardStarter.cs
....
builder.RegisterModule(new UserModule());
....
UserModule.cs
public class UserModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterSource(new UserRegistrationSource());
base.Load(builder);
}
}
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 = ...
yield return registrationBuilder.CreateRegistration();
}
}
But now I'm stuck...
Should I somehow try to resolve the dependencies in UserRegistrationSource by getting the requested types from the Ninject container (kernel) with Autofac or is this the wrong approach? Or should the Ninject kernel do the resolves for Autofac for the external project?