Mapping constructor arguments to settings in the app.config - structuremap

With StructureMap, I know that you can wire constructor arguments to app settings when mapping in code, and you can specify constructor arguments when defining the DefaultInstance in the XML config, but is there a way to have the XML configuration look at its own app.config settings?

My specific case was using Entity, so maybe this isn't applicable for everything, but rather than duplicating the same connection string information, you can use "Name=MyConnectionString" as a connection string and the framework will automatically pull the named connection string from the app.config/web.config. This does what I need it to.

Related

When using DI to inject a custom service into a Mediator Handler I encounter a base64 encoding issue

I am building an azure table storage service. When I register it in the Startup.cs container everything works fine.
But when I start up and compile the application. Mediator returns an exception"
{"Error constructing handler for request of type MediatR.IRequestHandler`2[BWAffinity.Application.MembersEmailed.MembersEmailed+Command,MediatR.Unit]. Register your handlers with the container. See the samples in GitHub for examples."}
With an inner exception of:
InnerException = {"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."}
Here is the stacktrace just in case it is needed:
Here is where I try to inject the service into the handler:
My question is:
What is causing this issue when I try to inject the service into the handler?
Things I have tried:
I created two unit test that prove that it is working but the connection strings or "Keys" are passed directly into it.
The answer to this question is that in the container. I had the parameters in reverse. When doing dependency injection and passing in your values in the DI Container. Make sure your object parameters are input in the correct order.

F# Type Providers and Continuous Integration

The type definition of an F# type provider often requires a constant expression, e.g. for the SQL type provider:
type dbSchema = SqlDataConnection<"Data Source=MySqlServer;Initial Catalog=MyDatabase;">
However, when committing the code to SCM, and further having a build server doing its thing, you probably don’t want to use the same connection string, but rather the connection string of a SQL server database that is generated from the build process.
Is there a solution for this problem?
It would be really nice to be able to make this work, as it would provide a compile-time check of the database access code.
Update
The solution proposed by #tomaspetricek worked very well, but I had to add a provider name to the connection string:
<add name="DbConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=MySqlServer;Initial Catalog=MyDatabase;"/>
You can certainly specify the connection string using a key in a configuration file (see MSDN documentation):
SqlDataConnection<ConnectionStringName="...", ConfigFile="app.config">
In general, a type provider may require some constant expression, but I think most of the widely used ones provide a way for avoiding this. For example, SqlDataConnection can read the setting from configuration file, other standard F# type providers allow specifying LocalSchemaFile that allows you to specify the necessary structure locally (e.g. *.dbml file for SQL).
The F# Data type providers can take URL to a remote file, but they can also take local file. So I think that there should always be a way to specify the information without specifying a constant connection string (etc.) - but feel free to ask about specific providers.

Multiple connection strings, one repository with dependency injection

Right now I've got a basic IRepository that takes in IConnect (contains a string value). I'm running into an issue getting my DI (structuremap) to determine which connection string to use. In theory, if I use an attribute on the entity, I could write up a registry/scanner that determines this but I wonder if there's an easier way to do it?
right now I have something like this
ObjectFactory.Initialize(factory =>
{
factory.For<IConnect>().Singleton().Use<ConnectToMarket>()
.Ctor<string>("connectionString")
.Is(_marketConnectionString);
//and some other stuff
});
Ideas?
You could derive different repositories from different interfaces. Say you have a IMarketRepository which is constructed with the market connection string. So all repositories that use the marketconnectionstring are constructed with that connection string (provided you do the registration correctly).
I'm assuming you don't have to use the same repository for multiple connectionstrings?
Another solution could be to make multiple implementations of IConnectionString or something which you can specify in the constructor.
Both solutions are not really nice I must say.

StructureMap Constructor arguments

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)
{
}
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.
For<BlobContainer>()
.HybridHttpOrThreadLocalScoped()
.Use<BlobContainer>()
.Ctor<CloudStorageAccount >("account").Is(...)
.Ctor<string >("containerName").Is(...)
.Ctor<string >("contentType").Is(...)
.Ctor<BlobContainerPermissions >("blobContainerPermissions").Is(...);

Best way to use an IoC container for retrieving runtime settings

I have an C# dll project for which I have to store the runtime settings in an external XML file, and this dll will be used in an ASP.NET/ASP.NET MVC application for which I also have to store the runtime settings in a external file.
Which IoC container can be used to create an object with the runtime settings loaded from a specific external file (or app.config/web.config), and also works for web applications running in medium trust? Any howto/tutorial would be greatly appreciated.
So far I've found only this articles:
Use Castle Windsor to Read Your Config Files Automatically
Getting rid of strings (3): take your app settings to the next level
Update
I'm sending mails from my dll to a variable number of SMTP servers, based on the current record type. For type A I'm using a given SMTP server+port, for type B I'm using an alternate set of server+port values. Of course, I want to be able to modify those values after deployment, so I store them in a XML file.
If I'm storing the SMTP settings as a SMTPConfiguration class with 2 properties (SMTPServer as String and SMTPPort as Int32), is it possible to return from an IoC container the required object based on the given record type, and what is the best way to read the runtime settings in order to build the returning object?
Update2
Let's say I'm storing in the configuration file the following parameters: ASMTPServer, BSMTPServer, ASMTPPort, BSMTPPort.
I can use Castle DictionaryAdapter to read all those settings as properties of an AppConfiguration class.
What is the recommended method to specify that the required SMTPConfiguration class should use ASMTPServer and ASMTPPort values if I'm using a type A record as a parameter (and should use BSMTPServer and BSMTPPort values if I'm using a type B record as a parameter) ? Also, how can I specify that the AppConfiguration is required in this process?
Is there a pattern for initializing objects created wth a DI container
Windsor Config Parameters With Non-Primitive Types

Resources