Where to store connection info

Topics: Administration, Writing modules
May 29, 2012 at 9: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?

Developer
May 29, 2012 at 12: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 8: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() {

            SchemaBuilder.CreateTable("SmtpSettingsPartRecord", 
                table => table
                    .ContentPartRecord()
                    .Column<string>("Address")
                    .Column<string>("Host")
                    .Column<int>("Port")
                    .Column<bool>("EnableSsl")
                    .Column<bool>("RequireCredentials")
                    .Column<string>("UserName")
                    .Column<string>("Password")
                );

            return 1;
        }
    }

 

namespace Orchard.Email.Handlers {
    [UsedImplicitly]
    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
            Filters.Add(StorageFilter.For(repository));

            OnLoaded<SmtpSettingsPart>(LazyLoadHandlers);
        }

Hope this helps.

Jun 1, 2012 at 4:52 PM

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