static WrapperSingleton()
 {
     _wrapperType = DataWrapperTypeManager.GetDataWrapperType(typeof(T));
 }
Example #2
0
        private void InitializeExistingStores()
        {
            _compositeGeneratedAssembly = _compositeGeneratedAssembly ?? AssemblyFacade.GetGeneratedAssemblyFromBin();
            _sqlDataTypeStoresContainer = new SqlDataTypeStoresContainer(_dataProviderContext.ProviderName, _connectionString, _sqlLoggingContext);

            Dictionary <DataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> > allSqlDataTypeStoreDataScopes = BuildAllExistingDataTypeStoreDataScopes();

            var initializedStores = new List <InterfaceGeneratedClassesInfo>();

            bool dataContextRecompilationNeeded = false;

            Type dataContextClass = TryLoadDataContext(ref dataContextRecompilationNeeded);


            var dataTypes = LoadDataTypes(_interfaceConfigurationElements);

            var compilationData = new List <HelperClassesGenerationInfo>();

            foreach (InterfaceConfigurationElement element in _interfaceConfigurationElements)
            {
                HelperClassesGenerationInfo toCompile = null;

                var generatedClassesInfo = InitializeStoreTypes(element, allSqlDataTypeStoreDataScopes,
                                                                dataContextClass, dataTypes, false, ref dataContextRecompilationNeeded, ref toCompile);

                if (generatedClassesInfo == null)
                {
                    continue;
                }
                if (toCompile != null)
                {
                    compilationData.Add(toCompile);
                }

                initializedStores.Add(generatedClassesInfo);
            }

            if (compilationData.Any())
            {
                var codeGenerationBuilder = new CodeGenerationBuilder(_dataProviderContext.ProviderName + " : compiling missing classes");

                foreach (var toCompile in compilationData)
                {
                    toCompile.GenerateCodeAction(codeGenerationBuilder);
                }

                // Precompiling DataWrapper classes as well to improve loading time
                foreach (var interfaceType in dataTypes.Values)
                {
                    if (DataWrapperTypeManager.TryGetWrapperType(interfaceType.FullName) == null)
                    {
                        DataWrapperCodeGenerator.AddDataWrapperClassCode(codeGenerationBuilder, interfaceType);
                    }
                }

                var types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder, false).ToArray();

                foreach (var toCompile in compilationData)
                {
                    toCompile.PopulateFieldsAction(types);
                }
            }

            if (dataContextRecompilationNeeded)
            {
                dataContextClass = DataContextAssembler.EmitDataContextClass(
                    initializedStores
                    .Where(s => s.Fields != null)
                    .SelectMany(s => s.Fields.Values).Evaluate());
            }

            _sqlDataTypeStoresContainer.DataContextClass = dataContextClass;

            foreach (var typeInfo in initializedStores)
            {
                var store = EmbedDataContextInfo(typeInfo, dataContextClass);

                AddDataTypeStore(store, true, true);
            }
        }