StructureMap Constructor arguments

谁说我不能喝 提交于 2019-12-10 00:59:00

问题


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)
  {

  }

回答1:


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.

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




回答2:


        For<BlobContainer>()
            .HybridHttpOrThreadLocalScoped()
            .Use<BlobContainer>()
            .Ctor<CloudStorageAccount >("account").Is(...)
            .Ctor<string >("containerName").Is(...)
            .Ctor<string >("contentType").Is(...)
            .Ctor<BlobContainerPermissions >("blobContainerPermissions").Is(...);


来源:https://stackoverflow.com/questions/5698274/structuremap-constructor-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!