2
我正在使用单独的核心类库编写Outlook加载项。我试图使用一个Ioc容器(Autofac),但是我对于如何将Outlook会话注册为我的服务的依赖关系有点遗憾。使用依赖注入(IoC)的Outlook加载项
我是一个DI新手。
在我ThisAddIn.cs文件我实例化一个引导程序在我的类库:
public override void BeginInit()
{
_core = new AddInBootstrapper();
base.BeginInit();
}
的AddInBootStrapper.cs类是我的核心类库。它的构造看起来像这样:
public AddInBootstrapper()
{
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<OutlookService>().As<IOutlookService>();
containerBuilder.RegisterType<EmailPrintService>().As<IPrintService<Microsoft.Office.Interop.Outlook.MailItem>>();
_container = containerBuilder.Build();
}
我OutlookService类对Outlook会话的依赖(ThisAddIn.Application.Session):
public OutlookService(Microsoft.Office.Interop.Outlook.NameSpace session)
{
_session = session;
}
我不知道如何在这样组装起来正确的方式。我是否简单地通过构造函数将Session传递到Bootstrapper类中,然后将其连接起来,还是有其他更好的方法?
_core = new AddInBootstrapper(this.Application.Session);
我发现了一些可行的方法,但不确定是否理想。见下文 – raydenl 2012-07-19 01:19:22