我已经使用Microsoft.Azure.Functions.Extensions.DependencyInjection
和Microsoft.Extensions.DependencyInjection
在Azure函数中设置了DI。这就是我的创业公司:
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient<IThingFactory, ThingFactory>();
}
}
这在项目中运行良好,但我添加了对SomeOtherProject.dll
(我的类库项目之一(的引用。
我的问题是:我需要为每个接口设置服务吗;我将要使用的SomeOtherProject
的实现?
这取决于您。
如果你想使用Azure的依赖项注入功能(建议使用(,你应该始终为SomeOtherProject.dll
中的每个interfaces & implementations
设置服务。
例如,在SomeOtherProject.dll
中,有名为IRepository
的an interface
和名为Repository
的a class
,它们实现了接口。然后在azure函数中,引用这个SomeOtherProject.dll
,你想像这个示例一样使用依赖注入,你必须像这个builder.Services.AddSingleton<IRepository, Repository>();
一样在Startup class
中注册它们
但是,如果您不选择使用Dependency Injection
,则没有必要这样做,只需直接使用它们即可。
我认为没有必要。如果您使用dll引用,在添加引用之后,应该可以自由使用SomerProject的接口和实现。