I create a site using Orchard CMS, and I have an external .NET project written with Ninject for dependency injection, which I would like to use with the module in Orchard CMS. I know that Orchard uses Autofac for dependency injection, and this causes me problems since I have never worked with DI before.
I created an Autofac module UserModule, which registers the source UserRegistrationSource, for example:
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 =
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);
}
}
Constructor EFMembershipManager.cs
public EFMembershipManager(ServerRepository db,
ServerRepositoryMembershipProvider membershipProvider,
string testUsername,
string serverUsername)
{
...
}
EFMembershipManageris a class from an external project that uses Ninject for DI and uses ServerRepositoryand ServerRepositoryMembershipProvider, which are also introduced using Ninject.
And now I'm stuck ...
UserRegistrationSource () Ninject IUserServices, Ninject Enumerable, Autofac , IUserServices ?