Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StructureMap Constructor arguments

Tags:

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

  }
like image 292
Game99 Avatar asked Apr 18 '11 03:04

Game99


2 Answers

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.

like image 196
PHeiberg Avatar answered Oct 28 '22 15:10

PHeiberg


        For<BlobContainer>()
            .HybridHttpOrThreadLocalScoped()
            .Use<BlobContainer>()
            .Ctor<CloudStorageAccount >("account").Is(...)
            .Ctor<string >("containerName").Is(...)
            .Ctor<string >("contentType").Is(...)
            .Ctor<BlobContainerPermissions >("blobContainerPermissions").Is(...);
like image 24
ozczecho Avatar answered Oct 28 '22 16:10

ozczecho