我们正在从StructureMap迁移到Lamar,但找不到在运行时传递参数的“ Lamar版本”。
我们有一个需要字符串参数(伪代码)的类:
public class MyRepository {
public MyRepository(string accountId) {}
}
Run Code Online (Sandbox Code Playgroud)
…还有一家工厂
public class MyRepoFactory(Container container) {
public MyRepository GetRepositoryForAccount(string accountId) =>
container
// With() is not available in Lamar?
.With("accountId").EqualTo(accountId)
.GetInstance<IMyRepository>();
}
Run Code Online (Sandbox Code Playgroud)
实际上,还有其他依赖项。
怎么说Lamar GetInstance()可以IMyRepository使用值xy作为名为的构造函数参数accountId?
我使用 ASP.NET Core 3 配置了 Lamar,但出现错误
System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' to type 'Lamar.ServiceRegistry'.'
Run Code Online (Sandbox Code Playgroud)
我在Program课堂上的配置:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseLamar();
webBuilder.UseStartup<Startup>();
});
}
Run Code Online (Sandbox Code Playgroud)
和Startup班级:
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
//public void ConfigureServices(IServiceCollection services)
//{
// services.Configure<CookiePolicyOptions>(options => …Run Code Online (Sandbox Code Playgroud) 在 StructureMap 中,您可以声明一个Forward<,>语句,该语句允许注册单个具体实例,以便由StructureMap 文档中的多个接口解析:
var container = new Container(_ =>
{
// Let's make StatefulCache a SingletonThing in the container
_.ForConcreteType<StatefulCache>().Configure.Singleton();
_.Forward<StatefulCache, IReader>();
_.Forward<StatefulCache, IWriter>();
});
container.GetInstance<IReader>().ShouldBeOfType<StatefulCache>();
container.GetInstance<IWriter>().ShouldBeOfType<StatefulCache>();
Run Code Online (Sandbox Code Playgroud)
我正在考虑可能迁移到 Lamar,它是 StructureMap 的替代品,但我在注册选项中没有看到任何与之匹配的内容。
这在拉马尔可能吗?