StructureMap Constructor arguments

Game99 picture Game99 · Apr 18, 2011 · Viewed 8.7k times · Source

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

  }

Answer

PHeiberg picture PHeiberg · Apr 18, 2011

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.