StructureMap 构造函数参数 [英] StructureMap Constructor arguments

查看:38
本文介绍了StructureMap 构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 structureMap 的新手.如何使用流畅的配置为以下类定义构造函数参数?谢谢

I'm new to structureMap. How do I define constructor arguments for the following class with fluent configuration? Thanks

  public BlobContainer(CloudStorageAccount account
              , string containerName
              , string contentType
              , BlobContainerPermissions blobContainerPermissions)
  {

  }

推荐答案

对于原始类型,您将按照@ozczecho 的回答进行操作:

For primitive types you would go about as @ozczecho answered:

For<BlobContainer>()
  .Use<BlobContainer>()
  .Ctor<string>("containerName").Is("theContainerName")
  .Ctor<string>("contentType").Is("theContentType");

前提是这些值在注册时是已知的.对于非原始类型,您也可以通过这种方式执行此操作,但是您会失去容器以这种方式为您提供的灵活性.最好定义一个默认或命名实例并使用它(容器将自动为您解析默认实例).通过定义默认值,您只需更改一个注册即可轻松更改应用程序中某个类型的所有依赖项.

provided that the values are known at registration time. You can do it this way for non-primitive types as well, but you lose the flexibility that the container gives you this way. It's better to define a default or named instance and use that instead (the container will automatically resolve default instances for you). By defining defaults you can easily change all the dependencies on a type in your application by changing just one registation.

For<CloudStorageAccount>().Use<TheCloudStorageAccountType>();

如果依赖项是一个具体类型,其构造函数具有结构映射已知的依赖项,则您不必将其注册到容器中,它将被自动解析.

If a dependency is a concrete type with a constructor having dependencies that are known to structuremap you don't have to register it with the container, it will be automatically resolved.

所以如果 CloudStorageAccount 是一个具体的类,你只需要在 Structure Map 中注册它的依赖项.

So if CloudStorageAccount is a concrete class you only need to register it's dependencies in Structure Map.

这篇关于StructureMap 构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆