15

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

  }
JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
Game99
  • 227
  • 3
  • 6

2 Answers2

19

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.

PHeiberg
  • 29,411
  • 6
  • 59
  • 81
  • Sorry PHeiberg, I accidently clicked on down vote..and now cannot undo my action. – ozczecho Nov 16 '11 at 01:23
  • @PHeiberg: If I need instances of BlobContainer in different places but each place I need different argument for the constructor, how can I provide it if the arguments are not known in registration time? – Naor Jul 18 '12 at 19:43
  • @Naor: Then you have to register a BlobContainerFactory (a class that knows how to construct a BlobContainer with the correct arguments), or a Func (which has access to the context needed for creating the container). – PHeiberg Jul 21 '12 at 10:03
  • @PHeiberg: Do you have an example of that? – Naor Jul 22 '12 at 09:26
  • @Naor: Not a specific link or piece of code. It's better if you ask a new question, with your specific scenario. – PHeiberg Jul 23 '12 at 06:29
3
        For<BlobContainer>()
            .HybridHttpOrThreadLocalScoped()
            .Use<BlobContainer>()
            .Ctor<CloudStorageAccount >("account").Is(...)
            .Ctor<string >("containerName").Is(...)
            .Ctor<string >("contentType").Is(...)
            .Ctor<BlobContainerPermissions >("blobContainerPermissions").Is(...);
ozczecho
  • 8,649
  • 8
  • 36
  • 42