我是IoC的新手,在我当前的项目中使用Autofac.
我有以下两个班级:
public class UserService : IUserService { private readonly IUserRepository _repo; private readonly IMailService _mailService; public UserService(IUserRepository repo,IMailService mailService) { _repo = repo; _mailService = mailService; } } public class MailService : IMailService { private readonly IMailRepository _repo; private readonly IUserService _userService; public MailService(IMailRepository repo,IUserService userService) { _repo = repo; _userService = userService; } }
最初,我的UserService类不需要MailService类的实例,但是现在它确实如此,并且自从将这个引入UserService构造函数以来已经出现了这个循环依赖性错误,并且作为一个新手,我不知道如何解决这个问题
这是我的课程目前在Autofac中注册的方式:
var builder = new ContainerBuilder(); // controllers builder.RegisterControllers(Assembly.GetAssembly(typeof(UsersController))); // register other classes builder.RegisterType<UserRepository>().As<IUserRepository>(); builder.RegisterType<MailRepository>().As<IMailRepository>(); builder.RegisterType<UserService>().As<IUserService>(); builder.RegisterType<MailService>().As<IMailService>();