我正在使用Orchard CMS创建一个网站,我有一个用Ninject编写的外部.NET项目用于依赖注入,我想与Orchard CMS中的模块一起使用.我知道Orchard使用Autofac进行依赖注入,这引起了我的问题,因为我之前从未使用过DI.
我创建了一个Autofac模块,UserModule它注册了一个源代码UserRegistrationSource,如下所示:
UserModule.cs
public class UserModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterSource(new UserRegistrationSource());
}
}
Run Code Online (Sandbox Code Playgroud)
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 = // …Run Code Online (Sandbox Code Playgroud)