Пример #1
0
        internal static Type TryGetType(DataTypeDescriptor dataTypeDescriptor, bool forceReCompilation, out bool codeGenerationNeeded)
        {
            Verify.ArgumentNotNull(dataTypeDescriptor, "dataTypeDescriptor");
            codeGenerationNeeded = false;

            Type type;

            if (!forceReCompilation)
            {
                type = TypeManager.TryGetType(dataTypeDescriptor.GetFullInterfaceName());
                if (type != null) return type;

                if (!dataTypeDescriptor.IsCodeGenerated)
                {
                    type = TypeManager.TryGetType(dataTypeDescriptor.TypeManagerTypeName);
                    if (type != null) return type;
                }
            }

            if (!dataTypeDescriptor.IsCodeGenerated)
            {
                return null;
            }

            if (forceReCompilation)
            {
                type = TypeManager.TryGetType(dataTypeDescriptor.GetFullInterfaceName());
                if (type != null) return type;
            }

            codeGenerationNeeded = true;
            return null;
        }
Пример #2
0
        /// <summary>
        /// This method will return type given by the dataTypeDescriptor.
        /// If the data type does not exist, one will be dynamically
        /// runtime code generated.
        /// </summary>
        /// <param name="dataTypeDescriptor"></param>
        /// <param name="forceReCompilation">If this is true a new type will be compiled regardless if one already exists.</param>
        /// <returns></returns>
        public static Type GetType(DataTypeDescriptor dataTypeDescriptor, bool forceReCompilation = false)
        {
            bool codeGenerationNeeded;

            Type type = TryGetType(dataTypeDescriptor, forceReCompilation, out codeGenerationNeeded);
            if (type != null)
            {
                return type;
            }

            if (codeGenerationNeeded)
            {
                lock (_lock)
                {
                    type = TypeManager.TryGetType(dataTypeDescriptor.GetFullInterfaceName());
                    if (type != null) return type;

                    var codeGenerationBuilder = new CodeGenerationBuilder("DataInterface: " + dataTypeDescriptor.Name);
                    InterfaceCodeGenerator.AddAssemblyReferences(codeGenerationBuilder, dataTypeDescriptor);
                    InterfaceCodeGenerator.AddInterfaceTypeCode(codeGenerationBuilder, dataTypeDescriptor);

                    IEnumerable<Type> types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder);

                    return types.Single();
                }
            }

            return null;
        }
Пример #3
0
 /// <summary>
 /// Adds the assembly references required by the supplied <see cref="DataTypeDescriptor"/> to the supplied  <see cref="CodeGenerationBuilder"/>
 /// </summary>
 /// <param name="codeGenerationBuilder">Assembly refences is added to this builder</param>
 /// <param name="dataTypeDescriptor">Data type descriptor which may contain references to assemblies</param>
 public static void AddAssemblyReferences(CodeGenerationBuilder codeGenerationBuilder, DataTypeDescriptor dataTypeDescriptor)
 {
     foreach (Assembly assembly in GetReferencedAssemblies(dataTypeDescriptor))
     {
         codeGenerationBuilder.AddReference(assembly);
     }
 }
Пример #4
0
        public XmlDataTypeStore(DataTypeDescriptor dataTypeDescriptor, Type dataProviderHelperType, Type dataIdClassType, IEnumerable<XmlDataTypeStoreDataScope> xmlDateTypeStoreDataScopes, bool isGeneratedDataType)
        {
            if (dataProviderHelperType == null) throw new ArgumentNullException("dataProviderHelperType");
            if (dataIdClassType == null) throw new ArgumentNullException("dataIdClassType");

            DataTypeDescriptor =  dataTypeDescriptor;
            DataProviderHelperType = dataProviderHelperType;
            DataIdClassType = dataIdClassType;
            IsGeneratedDataType = isGeneratedDataType;

            _xmlDateTypeStoreDataScopes = xmlDateTypeStoreDataScopes.Evaluate();

            var ordering = new List<Func<XElement, IComparable>>();
            foreach (string key in dataTypeDescriptor.KeyPropertyNames)
            {
                XName localKey = key;
                ordering.Add(f => (string)f.Attribute(localKey) ?? "");
            }
            Func<IEnumerable<XElement>, IOrderedEnumerable<XElement>> orderer = f => ordering.Skip(1).Aggregate(f.OrderBy(ordering.First()), Enumerable.ThenBy);

            foreach (XmlDataTypeStoreDataScope xmlDataTypeStoreDataScope in _xmlDateTypeStoreDataScopes)
            {
                DataScopeIdentifier dataScopeIdentifier = DataScopeIdentifier.Deserialize(xmlDataTypeStoreDataScope.DataScopeName);
                CultureInfo culture = CultureInfo.CreateSpecificCulture(xmlDataTypeStoreDataScope.CultureName);
                Type dataType = dataTypeDescriptor.GetInterfaceType();

                Action cacheFlush = () => DataEventSystemFacade.FireExternalStoreChangedEvent(dataType, dataScopeIdentifier.ToPublicationScope(), culture);
                XmlDataProviderDocumentCache.RegisterExternalFileChangeAction(xmlDataTypeStoreDataScope.Filename, cacheFlush);

                XmlDataProviderDocumentWriter.RegisterFileOrderer(xmlDataTypeStoreDataScope.Filename, orderer);
            }
        }
        /// <summary>
        /// Create an invariant store
        /// </summary>
        /// <param name="providerName"></param>
        /// <param name="dataTypeDescriptor"></param>
        public static void AddNew(string providerName, DataTypeDescriptor dataTypeDescriptor)
        {
            var xmlDataProviderConfiguration = new XmlDataProviderConfiguration(providerName);

            string interfaceType = dataTypeDescriptor.TypeManagerTypeName;

            if (interfaceType != null)
            {
                object key = xmlDataProviderConfiguration.Section.Interfaces.GetKey(dataTypeDescriptor);

                if (key != null)
                {
                    Log.LogWarning(LogTitle, 
                        "Configuration file '{0}' already contains an interface type '{1} 'with id '{2}'. "
                        + "Possibly there are multiple AppDomain-s running.",
                        xmlDataProviderConfiguration.ConfigurationFilePath, dataTypeDescriptor, dataTypeDescriptor.DataTypeId);
                    return;
                }
            }

            XmlProviderInterfaceConfigurationElement configurationElement = BuildXmlProviderInterfaceConfigurationElement(dataTypeDescriptor);

            XmlDataProviderStoreManipulator.CreateStore(providerName, configurationElement);

            xmlDataProviderConfiguration.Section.Interfaces.Add(configurationElement);

            xmlDataProviderConfiguration.Save();
        }
Пример #6
0
        /// <summary>
        /// This method validates if the existing .NET runtime type match the recorded meta data (DataTypeDescriptor).
        /// In case there is a mismatch, changes might have been done to the runtime type and an update on 
        /// the existing store(s)could not be performed.
        /// </summary>
        /// <param name="interfaceType"></param>
        /// <param name="existingDataTypeDescriptor"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static bool Validate(Type interfaceType, DataTypeDescriptor existingDataTypeDescriptor, out string errorMessage)
        {
            DataTypeDescriptor newDataTypeDescriptor;
            
            try
            {
                newDataTypeDescriptor  = DynamicTypeManager.BuildNewDataTypeDescriptor(interfaceType);
            }
            catch (Exception)
            {
                errorMessage = null;
                return true;
            }

            try
            {
                new DataTypeChangeDescriptor(existingDataTypeDescriptor, newDataTypeDescriptor);
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return false;
            }

            errorMessage = null;
            return true;
        }
Пример #7
0
        /// <summary>
        /// Given a <see cref="DataTypeDescriptor"/> creates a interface declaration inheriting from IData, a valid C1 Datatype.
        /// </summary>
        /// <param name="dataTypeDescriptor">A description of the data type to generate interface for</param>
        /// <returns>The generated interface</returns>
        public static CodeTypeDeclaration CreateCodeTypeDeclaration(DataTypeDescriptor dataTypeDescriptor)
        {
            try
            {
                var codeTypeDeclaration = new CodeTypeDeclaration(dataTypeDescriptor.Name)
                {
                    IsInterface = true
                };

                codeTypeDeclaration.BaseTypes.Add(new CodeTypeReference(typeof(IData)));

                var propertyNamesToSkip = new List<string>();

                foreach (Type superInterface in dataTypeDescriptor.SuperInterfaces)
                {
                    codeTypeDeclaration.BaseTypes.Add(new CodeTypeReference(superInterface));

                    propertyNamesToSkip.AddRange(superInterface.GetAllProperties().Select(p => p.Name));
                }

                AddInterfaceAttributes(codeTypeDeclaration, dataTypeDescriptor);
                AddInterfaceProperties(codeTypeDeclaration, dataTypeDescriptor, propertyNamesToSkip);

                return codeTypeDeclaration;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(string.Format("Failed to generate interface for type '{0}'", dataTypeDescriptor.TypeManagerTypeName), ex);
            }
        }
Пример #8
0
 public EntityBaseClassGenerator(DataTypeDescriptor dataTypeDescriptor, string entityBaseClassName, string dataIdClassName, string providerName)
 {
     _entityBaseClassName = entityBaseClassName;
     _dataIdClassName = dataIdClassName;
     _dataTypeDescriptor = dataTypeDescriptor;
     _providerName = providerName;
 }
Пример #9
0
        internal static string MakeFileName(DataTypeDescriptor dataTypeDescriptor, DataScopeIdentifier dataScopeIdentifier, string cultureName)
        {
            string typeFullName = StringExtensionMethods.CreateNamespace(dataTypeDescriptor.Namespace, dataTypeDescriptor.Name, '.');

            string publicationScopePart = "";

            switch (dataScopeIdentifier.Name)
            {
                case DataScopeIdentifier.PublicName:
                    break;
                case DataScopeIdentifier.AdministratedName:
                    publicationScopePart = "_" + PublicationScope.Unpublished;
                    break;
                default:
                    throw new InvalidOperationException("Unsupported data scope identifier: '{0}'".FormatWith(dataScopeIdentifier.Name));
            }

            string cultureNamePart = "";

            if (cultureName != "")
            {
                cultureNamePart = "_" + cultureName;
            }

            return typeFullName + publicationScopePart + cultureNamePart + ".xml";
        }
 internal void AddLocale(DataTypeDescriptor typeDescriptor, CultureInfo cultureInfo)
 {
     foreach (DataScopeIdentifier dataScope in typeDescriptor.DataScopes)
     {
         CreateStore(typeDescriptor, dataScope, cultureInfo);
     }
 }
Пример #11
0
        internal void AddDataType(DataTypeDescriptor dataTypeDescriptor, IEnumerable<SqlDataTypeStoreDataScope> sqlDataTypeStoreDataScopes)
        {
            Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor);
            if (interfaceType == null) return;

            IEnumerable<Tuple<string, string>> names;

            SqlProviderCodeGenerator codeGenerator = new SqlProviderCodeGenerator(_providerName);
            IEnumerable<CodeTypeDeclaration> codeTypeDeclarations = codeGenerator.CreateCodeDOMs(dataTypeDescriptor, sqlDataTypeStoreDataScopes, out names);
            codeTypeDeclarations.ForEach(f => _codeGenerationBuilder.AddType(_namespaceName, f));

            _entityClassNamesAndDataContextFieldNames.AddRange(names);
            
            _codeGenerationBuilder.AddReference(interfaceType.Assembly);

            // Property serializer for entity tokens and more
            string dataIdClassFullName = NamesCreator.MakeDataIdClassFullName(dataTypeDescriptor, _providerName);

            var keyPropertiesDictionary = new Dictionary<string, Type>();
            var keyPropertiesList = new List<Tuple<string, Type>>();
            foreach (var keyField in dataTypeDescriptor.KeyFields)
            {
                Verify.That(!keyPropertiesDictionary.ContainsKey(keyField.Name), "Key field with name '{0}' already present. Data type: {1}. Check for multiple [KeyPropertyName(...)] attributes", keyField.Name, dataTypeDescriptor.Namespace + "." + dataTypeDescriptor.Name);

                keyPropertiesDictionary.Add(keyField.Name, keyField.InstanceType);
                keyPropertiesList.Add(new Tuple<string, Type>(keyField.Name, keyField.InstanceType));
            }

            PropertySerializerTypeCodeGenerator.AddPropertySerializerTypeCode(_codeGenerationBuilder, dataIdClassFullName, keyPropertiesList);
        }
 internal void RemoveLocale(string providerName, DataTypeDescriptor typeDescriptor, CultureInfo cultureInfo)
 {
     foreach (DataScopeIdentifier dataScope in typeDescriptor.DataScopes)
     {
         DropStore(typeDescriptor, dataScope, cultureInfo);
     }
 }
 private void CreateScopeData(DataTypeDescriptor typeDescriptor, DataScopeIdentifier dataScope)
 {
     foreach (var cultureInfo in GetCultures(typeDescriptor))
     {
         CreateStore(typeDescriptor, dataScope, cultureInfo);
     }
 }
Пример #14
0
        /// <summary>
        /// Use this method to register data type descriptors that have been validated and will be 
        /// intstalled.
        /// </summary>
        /// <param name="interfaceName"></param>
        /// <param name="dataTypeDescriptor"></param>
        public void AddPendingDataTypeDescritpor(string interfaceName, DataTypeDescriptor dataTypeDescriptor)
        {
            Verify.ArgumentNotNullOrEmpty(interfaceName, "interfaceName");
            Verify.ArgumentNotNull(dataTypeDescriptor, "dataTypeDescriptor");

            _pendingDataTypeDescriptors.Add(interfaceName, dataTypeDescriptor);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="interfaceType"></param>
        /// <param name="existingDataTypeDescriptor">Use null to get existing data type descriptor</param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static bool Validate(Type interfaceType, DataTypeDescriptor existingDataTypeDescriptor, out string errorMessage)
        {
            if (existingDataTypeDescriptor == null)
            {
                existingDataTypeDescriptor = DataMetaDataFacade.GetDataTypeDescriptor(interfaceType.GetImmutableTypeId());

                if (existingDataTypeDescriptor == null)
                {
                    errorMessage = null;
                    return true;
                }
            }

            errorMessage = _typeSpecificValidations.GetOrAdd(
                interfaceType,
                f =>
                {
                    string message;
                    bool isValid = DataTypeValidator.Validate(interfaceType, existingDataTypeDescriptor, out message);

                    if (isValid) return null;

                    var sb = new StringBuilder();
                    sb.AppendLine(string.Format("The data type interface '{0}' did not validate and can't be used at the moment.", interfaceType));
                    sb.AppendLine(message);

                    return sb.ToString();
                }
            );

            return errorMessage == null;
        }
        internal static InterfaceConfigurationElement AddNew(string providerName, DataTypeDescriptor dataTypeDescriptor)
        {
            lock (_syncRoot)
            {
                var configuration = new SqlDataProviderConfiguration(providerName);

                if (configuration.Section.Interfaces.ContainsInterfaceType(dataTypeDescriptor.DataTypeId))
                {
                    Log.LogWarning(LogTitle,
                        "Configuration file '{0}' already contains an interface with data type ID '{1}', type name '{2}'. "
                         + "Possibly there are multiple AppDomain-s running.",
                            configuration.ConfigurationFilePath,
                            dataTypeDescriptor.DataTypeId,
                            dataTypeDescriptor);

                    return configuration.Section.Interfaces.Get(dataTypeDescriptor);
                }

                InterfaceConfigurationElement interfaceConfig = BuildInterfaceConfigurationElement(dataTypeDescriptor);

                configuration.Section.Interfaces.Add(interfaceConfig);

                configuration.Save();

                return interfaceConfig;
            }
        }
Пример #17
0
        public XmlProviderCodeGenerator(DataTypeDescriptor dataTypeDescriptor, string namespaceName)
        {
            _dataTypeDescriptor = dataTypeDescriptor;

            DataProviderHelperClassFullName = namespaceName + "." + NamesCreator.MakeDataProviderHelperClassName(dataTypeDescriptor);
            WrapperClassFullName = namespaceName + "." + NamesCreator.MakeWrapperClassName(dataTypeDescriptor);
            DataIdClassFullName = namespaceName + "." + NamesCreator.MakeDataIdClassName(dataTypeDescriptor);
        }
 public SqlDataProviderHelperGenerator(DataTypeDescriptor dataTypeDescriptor, string sqlDataProviderHelperClassName, string dataIdClassName, string entityClassName, string dataContextFieldName)
 {
     _dataTypeDescriptor = dataTypeDescriptor;
     _sqlDataProviderHelperClassName = sqlDataProviderHelperClassName;
     _dataIdClassName = dataIdClassName;
     _entityClassName = entityClassName;
     _dataContextFieldName = dataContextFieldName;
 }
Пример #19
0
        /// <summary>
        /// Adds the source code defined by <see cref="DataTypeDescriptor"/> to the supplied  <see cref="CodeGenerationBuilder"/>
        /// </summary>
        /// <param name="codeGenerationBuilder">Source code is added to this builder</param>
        /// <param name="dataTypeDescriptor">Data type descriptor to convert into source code</param>
        public static void AddInterfaceTypeCode(CodeGenerationBuilder codeGenerationBuilder, DataTypeDescriptor dataTypeDescriptor)
        {
            var codeTypeDeclaration = CreateCodeTypeDeclaration(dataTypeDescriptor);

            var codeNamespace = new CodeNamespace(dataTypeDescriptor.Namespace);
            codeNamespace.Types.Add(codeTypeDeclaration);
            codeGenerationBuilder.AddNamespace(codeNamespace);
        }
Пример #20
0
        /// <exclude />
        public GeneratedTypesHelper(Type oldType)
        {
            Verify.ArgumentNotNull(oldType, "oldType");

            _oldType = oldType;
            _oldDataTypeDescriptor = DynamicTypeManager.GetDataTypeDescriptor(oldType);

            Initialize();
        }
Пример #21
0
        /// <exclude />
        public GeneratedTypesHelper(DataTypeDescriptor oldDataTypeDescriptor)
        {
            Verify.ArgumentNotNull(oldDataTypeDescriptor, "oldDataTypeDescriptor");

            _oldType = oldDataTypeDescriptor.GetInterfaceType();
            _oldDataTypeDescriptor = oldDataTypeDescriptor;

            Initialize();
        }
 internal static IEnumerable<CultureInfo> GetCultures(DataTypeDescriptor typeDescriptor)
 {
     if (typeDescriptor.Localizeable)
     {
         return DataLocalizationFacade.ActiveLocalizationCultures;
     }
     
     return new [] { CultureInfo.InvariantCulture };
 }
Пример #23
0
 public EntityClassGenerator(DataTypeDescriptor dataTypeDescriptor, string entityClassName, string entityBaseClassName, string tableName, string dataScopeIdentifierName, string localeCultureName)
 {
     _dataTypeDescriptor = dataTypeDescriptor;
     _entityClassName = entityClassName;
     _entityBaseClassName = entityBaseClassName;
     _tableName = tableName;
     _dataScopeIdentifierName = dataScopeIdentifierName;
     _localeCultureName = localeCultureName;
 }
Пример #24
0
        internal UpdateDataTypeDescriptor(DataTypeDescriptor oldDataTypeDescriptor,
            DataTypeDescriptor newDataTypeDescriptor, 
            string providerName)
        {
            OldDataTypeDescriptor = oldDataTypeDescriptor;
            NewDataTypeDescriptor = newDataTypeDescriptor;

            ProviderName = providerName;
        }
Пример #25
0
        //public static GeneratedTypesHelper.KeyFieldType ParseKeyFieldType(string keyFieldTypeStr)
        //{
        //    return (GeneratedTypesHelper.KeyFieldType) Enum.Parse(typeof (GeneratedTypesHelper.KeyFieldType), keyFieldTypeStr);
        //}

        public static GeneratedTypesHelper.KeyFieldType GetKeyFieldType(DataTypeDescriptor dataTypeDescriptor)
        {
            var idField = dataTypeDescriptor.Fields.Single(f => f.Name == "Id");
            if (idField != null)
            {
                return GetKeyFieldType(idField);
            }

            return GeneratedTypesHelper.KeyFieldType.Undefined;
        }
        /// <exclude />
        public DataTypeDescriptorFormsHelper(DataTypeDescriptor dataTypeDescriptor, string bindingNamesPrefix, bool showPublicationStatusSelector, EntityToken entityToken)
        {
            if (dataTypeDescriptor == null) throw new ArgumentNullException("dataTypeDescriptor");

            _dataTypeDescriptor = dataTypeDescriptor;
            _bindingNamesPrefix = bindingNamesPrefix;
            _showPublicationStatusSelector = showPublicationStatusSelector;
            EntityToken = entityToken;
            LayoutIconHandle = null;
        }
Пример #27
0
        internal DataTypeChangeDescriptor(DataTypeDescriptor originalTypeDescriptor, DataTypeDescriptor alteredTypeDescriptor, bool originalTypeDataExists)
        {
            if (originalTypeDescriptor.DataTypeId != alteredTypeDescriptor.DataTypeId) throw new ArgumentException("The original and current data type descriptors must have the same data type id");

            _original = originalTypeDescriptor;
            _altered = alteredTypeDescriptor;
            _originalTypeDataExists = originalTypeDataExists;

            ValidateTypeChanges();
        }
 public DataProviderHelperClassGenerator(
     string helperClassName,
     string wrapperClassName,
     string dataIdClassName,
     DataTypeDescriptor dataTypeDescriptor)
 {
     _helperClassName = helperClassName;
     _wrapperClassName = wrapperClassName;
     _dataIdClassName = dataIdClassName;
     _dataTypeDescriptor = dataTypeDescriptor;
 }
        internal static void AddEmptyDataClassTypeCode(CodeGenerationBuilder codeGenerationBuilder, DataTypeDescriptor dataTypeDescriptor, Type baseClassType = null, CodeAttributeDeclaration codeAttributeDeclaration = null)
        {
            Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor);
            if (interfaceType == null) return;

            if (baseClassType == null) baseClassType = typeof(EmptyDataClassBase);

            CodeTypeDeclaration codeTypeDeclaration = CreateCodeTypeDeclaration(dataTypeDescriptor, baseClassType, codeAttributeDeclaration);

            codeGenerationBuilder.AddType(NamespaceName, codeTypeDeclaration);
        }
        internal static bool ConfigurationExists( string providerName, DataTypeDescriptor dataTypeDescriptor)
        {
            lock (_syncRoot)
            {
                var configuration = new SqlDataProviderConfiguration(providerName);

                InterfaceConfigurationElement interfaceConfig = BuildInterfaceConfigurationElement(dataTypeDescriptor);

                return configuration.Section.Interfaces.ContainsInterfaceType(interfaceConfig);
            }
        }