This is a more general answer but instead of injecting a string directly, it is generally a better idea to create an interface, a provider, and inject the provider itself. So in the future, if you need to change the way how you read and create the string, it would be easier. (consider you are reading it from app.config today but then you decide to store it in DB, or read it from the registry or pass it from a web request.)
A sample application:
public interface IMachineIdProvider
{
//Assume we are returning a machine Id.
String ResolveMachineId();
}
And implementation. Assume we are reading it from webconfig
public class MachineIdProvider : IMachineIdProvider
{
private string _machineId;
public string ResolveMachineId()
{
if (String.IsNullOrEmpty(_machineId))
{
this._machineId= ConfigurationManager.AppSettings["MachineId"];
}
return this._machineId;
}
}
And inject it that way:
kernel.Bind<IMachineIdProvider>().To<MachineIdProvider>().InSingletonScope();
Declare class with the interface
public class MyWonderfulController : BaseController
{
private IMachineIdProvider _machineIdProvider;
public MyWonderfulController(IMachineIdProvider machineIdProvider)
{
this._machineIdProvider = machineIdProvider;
}
}
So, wherever you need to use it, you can call
var id = _machineIdProvider.ResolveMachineId()
In this way, if you change the way that you obtain the string, you only change the method itself or create another one to inject.
@
prefix). I only use verbatim string literals if I want to use backslashes or multiple lines. – Lobate