gpt4 book ai didi

.net - 温莎城堡 : Registering all implementations of an interface having the same constructor parameter

转载 作者:行者123 更新时间:2023-12-02 08:47:09 25 4
gpt4 key购买 nike

我有同一个接口(interface)的多个实现,它们都具有相同的构造函数参数 (strConn)。是否可以使用 1 个 container.Register 调用注册所有实现?

这是我的:

container.Register(
Classes.FromAssemblyContaining<IUserMenuDao>()
.BasedOn<IDataAccess>().WithService.FromInterface()
);

这些实现都具有相同的构造函数参数;海峡。注册一个将是:

container.Register(Component.For<IUserMenuDao>()
.DependsOn(new {strConn = ConfigurationManager.ConnectionStrings["ABC123"].ConnectionString})
.ImplementedBy<UserMenuDaoSqlServerImpl>());

有没有一种方法可以按照类似于代码示例 #1 的约定进行注册,但对所有实现都使用 DependsOn?

最佳答案

确实如此

container.Register( 
Classes.FromAssemblyContaining<IUserMenuDao>()
.BasedOn<IDataAccess>()
.WithService.FromInterface()
.Configure(c => c.DependsOn(new {strConn = ConfigurationManager.ConnectionStrings["ABC123"].ConnectionString}))
);

关于.net - 温莎城堡 : Registering all implementations of an interface having the same constructor parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11670586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com