private void EnsureInitialized()
{
lock (this)
{
if (!this.initialized)
{
this.repositoryTypeName = !string.IsNullOrEmpty(this.repositoryTypeName) ? this.repositoryTypeName : "BlueCollar.SQLiteRepository, BlueCollar";
bool sqlite = this.repositoryTypeName.StartsWith("BlueCollar.SQLiteRepository", StringComparison.Ordinal);
if (!string.IsNullOrEmpty(this.connectionStringName))
{
ConnectionStringSettings css = ConfigurationManager.ConnectionStrings[this.connectionStringName];
if (css != null && !string.IsNullOrEmpty(css.ConnectionString))
{
this.connectionString = css.ConnectionString;
}
else
{
this.connectionString = ConfigurationManager.AppSettings[this.connectionStringName];
}
if (string.IsNullOrEmpty(this.connectionString) && !sqlite)
{
throw new ConfigurationErrorsException(
string.Format(CultureInfo.InvariantCulture, "Failed to find a connection string named '{0}' in either <connectionStrings/> or <appSettings/>.", this.connectionStringName),
BlueCollarSection.Section.ElementInformation.Source,
BlueCollarSection.Section.ElementInformation.LineNumber);
}
}
if (sqlite && string.IsNullOrEmpty(this.connectionString))
{
string path = this.ResolvePath(Path.Combine(this.DefaultDataDirectory, "BlueCollar.sqlite"));
this.connectionString = string.Concat("data source=", path, ";journal mode=Off;synchronous=Off;version=3");
}
Exception typeException = null;
try
{
this.repositoryType = Type.GetType(this.repositoryTypeName, true, true);
}
catch (TargetInvocationException ex)
{
typeException = ex;
}
catch (TypeLoadException ex)
{
typeException = ex;
}
catch (ArgumentException ex)
{
typeException = ex;
}
catch (FileNotFoundException ex)
{
typeException = ex;
}
catch (FileLoadException ex)
{
typeException = ex;
}
catch (BadImageFormatException ex)
{
typeException = ex;
}
if (typeException != null)
{
throw new ConfigurationErrorsException(
string.Format(CultureInfo.InvariantCulture, "Failed to load type '{0}' configured as the IRepository type to use.", this.repositoryTypeName),
typeException,
BlueCollarSection.Section.ElementInformation.Source,
BlueCollarSection.Section.Repository.ElementInformation.LineNumber);
}
if (!typeof(IRepository).IsAssignableFrom(this.repositoryType))
{
throw new ConfigurationErrorsException(
string.Format(CultureInfo.InvariantCulture, "Type '{0}', configured as the IRepository type to use, does not implement IRepository.", this.repositoryTypeName),
BlueCollarSection.Section.ElementInformation.Source,
BlueCollarSection.Section.Repository.ElementInformation.LineNumber);
}
if (string.IsNullOrEmpty(this.connectionString))
{
this.repositoryConstructor = this.repositoryType.GetConstructor(BindingFlags.Public | BindingFlags.Instance, Type.DefaultBinder, Type.EmptyTypes, null);
if (this.repositoryConstructor == null)
{
throw new ConfigurationErrorsException(
string.Format(CultureInfo.InvariantCulture, "Failed to find an empty constructor for IRepository type '{0}'.", this.repositoryTypeName),
BlueCollarSection.Section.ElementInformation.Source,
BlueCollarSection.Section.Repository.ElementInformation.LineNumber);
}
}
else
{
this.repositoryConstructor = this.repositoryType.GetConstructor(BindingFlags.Public | BindingFlags.Instance, Type.DefaultBinder, new Type[] { typeof(string) }, null);
if (this.repositoryConstructor == null)
{
throw new ConfigurationErrorsException(
string.Format(CultureInfo.InvariantCulture, "Failed to find a constructor that takes a single string argument (the connection string) for IRepository type '{0}'.", this.repositoryTypeName),
BlueCollarSection.Section.ElementInformation.Source,
BlueCollarSection.Section.Repository.ElementInformation.LineNumber);
}
}
this.initialized = true;
}
}
}