This project is read-only.

Where to store connection info

Topics: Administration, Writing modules
May 29, 2012 at 10:13 AM

Hi, I am working on a module which deals with Azure Storage. Connecting to azure storage requires a connection string. I Can put this connection string in configuration file. But I am looking for a way to store this information somewhere  else so that admin user can change the connection string without dealing with the configuration file. I was thinking to store the connection string in DB. But problem is , I have to query the DB every time I need to connect to the azure storage. What is the best solution?

May 29, 2012 at 1:40 PM

Storing the string in the DB inside a custom part you attach to the Site content type (thus site settings) is perfectly fine. Then it can also made editable from under site settings.

I guess this "connecting to Azure storage" should only happen once in a request life time, and one additional DB query per request is not a performance issue, but you could also cache the string with Orchard's ICacheManager. That way DB would be only queried by the first request after app (re)start or after the value is modified.

May 30, 2012 at 9:25 AM

There's lots of examples of this, but if you look at Orchard.Email you'll find a simple example


namespace Orchard.Email.Models {
    public class SmtpSettingsPart : ContentPart<SmtpSettingsPartRecord> {
        private readonly ComputedField<string> _password = new ComputedField<string>();

        public ComputedField<string> PasswordField {
            get { return _password; }

namespace Orchard.Email {
    public class Migrations : DataMigrationImpl {

        public int Create() {

                table => table

            return 1;


namespace Orchard.Email.Handlers {
    public class SmtpSettingsPartHandler : ContentHandler {
        private readonly IEncryptionService _encryptionService;

        public SmtpSettingsPartHandler(IRepository<SmtpSettingsPartRecord> repository, IEncryptionService encryptionService) {
            T = NullLocalizer.Instance;
            Logger = NullLogger.Instance;

            _encryptionService = encryptionService;
            Filters.Add(new ActivatingFilter<SmtpSettingsPart>("Site")); //***NOTE This is what attaches it to the Site Settings


Hope this helps.

Jun 1, 2012 at 5:52 PM

Thanx everyone. I will test it and let u know if it works. But I am pretty sure it will work