StructureMap3 How to configure constructor string injection for all types?


I have registered my types using

Scan( scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); scan.With(new ControllerConvention()); });

But how do I specify for constructor injection with out having to specify the concrete type like this?

string connStr = "..."; For<IRepository().Use<MyRepository>().Ctor<string>("connectionString").Is(connStr);


You can create dedicated convention for registration of repositories.

public class RepositoryConvention : IRegistrationConvention { private const string ConnectionString = "your connection string"; public void Process(Type type, Registry registry) { if (type.IsConcrete() && type.GetInterfaces().Contains(typeof(IRepository))) { registry.For(typeof(IRepository)) .Use(type) .CtorDependency<string>("connectionString") .Is(ConnectionString); } } }

or create dedicated type to provide with connection string. I bet you are getting it from web/app.config so adding abstraction for accessing it would be helpful anyway.

public interface IConfigurationSettingsReader { string ReadConnectionString(string name); T ReadSetting<T>(string settingName); }

Then you just add it as a dependency for your MyRepository and you don't need to add it explicitly in registration or use custom convention.

public class MyRepository : IRepository { private readonly string connectionString; public MyRepository(IConfigurationSettingsReader settingsReader) { this.connectionString = settingsReader.ReadConnectionString("ConnStrName"); } }

You can consider creating an abstract base repository class to be inherited by each repository to get rid of setup bolerplate.

Hope this helps!


  • Post-Redirect-Get (PRG) Summary Page on Submit
  • post-commit hook run as what user in svn
  • @EnableJpaRepositories looking for which package?
  • curl - How to escape < in parameter value
  • Domain name setup with AWS S3 bucket with static hosting
  • Android gradle : Error No resource found that matches the given name: attr when refer to an android
  • How to force Composer to download a local package?
  • MEF 2: import many
  • Accessing local repository in offline mode
  • CodeIgniter URI Parameter is partially bypassing an “if” statement
  • Specifying Castle WCF Integration Facility Endpoint Behavior per Endpoint
  • D3 nodes and links from JSON with nested arrays of children
  • How to create a file in java without a extension
  • Alert pop up with LWUIT
  • NHibernate Validation Localization with S#arp Architecture
  • ilmerge with a PFX file
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Adding a button at the bottom of a table view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Read text file and split every line in MSBuild
  • req.body is undefined - nodejs
  • Counter field in MS Access, how to generate?
  • Get object from AWS S3 as a stream
  • Java applet as stand-alone Windows application?
  • MySQL WHERE-condition in procedure ignored
  • sending/ receiving email in Java
  • Adding custom controls to a full screen movie
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • how does django model after text[] in postgresql [duplicate]
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?