/// <summary> /// This is the preferred way to initialize the Schema Loader. This will retrieve a DB Schema Loader using the /// SqlConnection Provider specified. With an Sql Connection Provider, we can defer (e.g. lazy load) /// the loading of the Schema Definitions until it's actually needed. This may speed up startup time if /// this initialization is part of a static element at startup and/or if it is never needed based on execution logic. /// </summary> /// <param name="sqlConnectionProvider"></param> /// <returns></returns> public static ISqlBulkHelpersDBSchemaLoader GetSchemaLoader(ISqlBulkHelpersConnectionProvider sqlConnectionProvider) { //Validate arg is a Static Schema Loader... var sqlConnProvider = sqlConnectionProvider.AssertArgumentIsNotNull(nameof(sqlConnectionProvider)); //Init cached version if it exists; which may already be initialized! var resultLoader = SchemaLoaderLazyCache.GetOrAdd( sqlConnProvider.GetDbConnectionUniqueIdentifier(), new Lazy <SqlBulkHelpersDBSchemaStaticLoader>(() => new SqlBulkHelpersDBSchemaStaticLoader(sqlConnectionProvider)) ); //Unwrap the Lazy<> to get, or construct, a valid Schema Loader... return(resultLoader.Value); }
public SqlBulkHelpersDBSchemaStaticLoader(ISqlBulkHelpersConnectionProvider sqlConnectionProvider) { sqlConnectionProvider.AssertArgumentIsNotNull(nameof(sqlConnectionProvider)); //Safely initialize the Lazy<> loader for Table Definition Schemas. //NOTE: We use a Lazy<> here so that our manual locking does as little work as possible and simply initializes the Lazy<> reference, // leaving the optimized locking for execution of the long-running logic to the underlying Lazy<> object to manage with // maximum efficiency _tableDefinitionsLookupLazy = new Lazy <ILookup <string, SqlBulkHelpersTableDefinition> >(() => { //Get a local reference so that it's scoping will be preserved... var localScopeSqlConnectionProviderRef = sqlConnectionProvider; var dbSchemaResults = LoadSqlBulkHelpersDBSchemaHelper(localScopeSqlConnectionProviderRef); //Set the initialization flag (mainly for reference/debugging) IsInitialized = true; return(dbSchemaResults); }); }