我是一名新手,对于依赖注入模式有些困惑。
场景:
我有一个称为“MailCore”的类库项目。该项目具有执行所有电子邮件发送工作的接口和类。 我有一个称为“站点”的MVC项目。它使用“MailCore”项目来发送电子邮件。我在这个项目中使用Unity,并注册了UnityContainer,事情运行得很好。
我还有另一个类库项目称为“SiteRepo”。有时,为了执行特定任务,我必须从该项目发送电子邮件。因此,“MailCore”项目也在该项目中被引用。
问题:
我已经在“SiteRepo”项目中从NuGet安装了Unity,但它似乎并没有在这个类库项目中创建任何UnityConfig。 如何在这里注册UnityContainer?
代码:
TheSite:
Public class JobController : Controller
{
private readonly IEmailBuilder mailBuilder;
public JobController(IEmailBuilder mailBuilder)
{
this.mailBuilder = mailBuilder;
}
public ActionResult Create(....)
{
JobRepo j = new JobRepo();
j.Create(....);
}
}
UnityConfig(这是Web应用程序“The Site”中的内容):
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<IEmailBuilder, EmailBuilder>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
SiteRepo:
Public class JobRepo()
{
Public bool Create(...)
{
//some other code to create a job....
//have to send email using MailCore !! The problem area in concern..
}
}