标签: lamar

如何在运行时传递参数?

我们正在从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

c# lamar

6
推荐指数
1
解决办法
152
查看次数

如何将 Lamar 2 与 ASP.NET Core 3 预览版结合使用?

我使用 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)

c# asp.net-core lamar

4
推荐指数
1
解决办法
1513
查看次数

拉马尔替代 StructureMap Forward()

在 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 的替代品,但我在注册选项中没有看到任何与之匹配的内容。

这在拉马尔可能吗?

structuremap lamar

3
推荐指数
1
解决办法
559
查看次数

标签 统计

lamar ×3

c# ×2

asp.net-core ×1

structuremap ×1