Last active
September 26, 2016 21:24
-
-
Save skiningham/c12e5f0f45d88df20367 to your computer and use it in GitHub Desktop.
Rough example of nopCommerce DataSettingsManager using web.config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using System.Web.Configuration; | |
namespace Nop.Core.Data | |
{ | |
/// <summary> | |
/// Manager of data settings (connection string) | |
/// </summary> | |
public partial class DataSettingsManager | |
{ | |
/// <summary> | |
/// Load settings | |
/// </summary> | |
/// <returns></returns> | |
public virtual DataSettings LoadSettings() | |
{ | |
try { | |
System.Configuration.Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath); | |
return new DataSettings | |
{ | |
DataConnectionString = webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ConnectionString, | |
DataProvider = webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ProviderName | |
}; | |
} | |
catch (NullReferenceException) { | |
return new DataSettings(); | |
} | |
} | |
/// <summary> | |
/// Save settings to a file | |
/// </summary> | |
/// <param name="settings"></param> | |
public virtual void SaveSettings(DataSettings settings) | |
{ | |
if (null == settings) throw new ArgumentNullException("settings"); | |
System.Configuration.Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath); | |
webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ConnectionString = settings.DataConnectionString; | |
webConfig.ConnectionStrings.ConnectionStrings["DefaultConnection"].ProviderName = settings.DataProvider; | |
webConfig.Save(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment