コード例 #1
0
 public PropertyRegistration(ContentPropertyAttribute propertyAttribute, string name, string alias, DataTypeRegistration dataType, PropertyInfo metadata, string cssClasses)
 {
     PropertyAttribute = propertyAttribute;
     Name     = name;
     Alias    = alias;
     DataType = dataType;
     Metadata = metadata;
 }
コード例 #2
0
 public PropertyRegistration(ContentPropertyAttribute propertyAttribute, string name, string alias, DataTypeRegistration dataType, PropertyInfo metadata, string cssClasses)
 {
     PropertyAttribute = propertyAttribute;
     Name = name;
     Alias = alias;
     DataType = dataType;
     Metadata = metadata;
 }
コード例 #3
0
        public void RegisterDateTimeType <T, Tconverter>(string dataTypeName) where Tconverter : IDataTypeConverter <DateTime, T>
        {
            DataTypeRegistration reg = new DataTypeRegistration();

            reg.ClrType              = typeof(T);
            reg.CodeFirstControlled  = false; //no real definition so don't sync
            reg.DataTypeInstanceName = dataTypeName;
            reg.ConverterType        = typeof(Tconverter);
            reg.DbType = DatabaseType.Date;
            reg.UmbracoDatabaseType = DataTypeDatabaseType.Date;
            reg.Definition          = _service.GetDataTypeDefinitionByName(dataTypeName);
            reg.PropertyEditorAlias = reg.Definition.PropertyEditorAlias;
            _registerController.Register(typeof(T), reg);
        }
コード例 #4
0
        private DataTypeRegistration CreateUmbracoConverterDataType(PropertyInfo instance, string dataType)
        {
            DataTypeRegistration existingTypeRegistration = new DataTypeRegistration()
            {
                ClrType              = instance.PropertyType,
                ConverterType        = null,
                DataTypeInstanceName = dataType,
                PropertyEditorAlias  = null,
                DbType = DatabaseType.None,
                CodeFirstControlled = false //data type req'd to exist in Umbraco, shouldn't be modified
            };

            _registerController.Register(instance, existingTypeRegistration);
            return(existingTypeRegistration);
        }
コード例 #5
0
 /// <summary>
 /// Gets the prevalues for a specified data type
 /// </summary>
 public IReadOnlyList<PreValue> Get(DataTypeRegistration registration)
 {
     List<PreValue> result;
     if (!_cache.TryGetValue(registration, out result))
     {
         result = GetPreValues(registration.Definition.Id);
         if (!_cache.TryAdd(registration, result))
         {
             if (!_cache.ContainsKey(registration))
             {
                 throw new CodeFirstException("Unable to cache prevalues");
             }
         }
     }
     return result;
 }
コード例 #6
0
        /// <summary>
        /// Gets the prevalues for a specified data type
        /// </summary>
        public IReadOnlyList <PreValue> Get(DataTypeRegistration registration)
        {
            List <PreValue> result;

            if (!_cache.TryGetValue(registration, out result))
            {
                result = GetPreValues(registration.Definition.Id);
                if (!_cache.TryAdd(registration, result))
                {
                    if (!_cache.ContainsKey(registration))
                    {
                        throw new CodeFirstException("Unable to cache prevalues");
                    }
                }
            }
            return(result);
        }
コード例 #7
0
        private IDataTypeDefinition CreateDataTypeDefinition(DataTypeRegistration dataTypeRegistration)
        {
            IDataTypeDefinition dataTypeDefinition;

            if (dataTypeRegistration.DbType == DatabaseType.None)
            {
                throw new CodeFirstException("Database type not specified for " + dataTypeRegistration.DataTypeInstanceName);
            }
            else if (string.IsNullOrWhiteSpace(dataTypeRegistration.PropertyEditorAlias))
            {
                throw new CodeFirstException("Property Editor Alias not specified for " + dataTypeRegistration.DataTypeInstanceName);
            }
            else
            {
                dataTypeDefinition              = new Umbraco.Core.Models.DataTypeDefinition(-1, dataTypeRegistration.PropertyEditorAlias);
                dataTypeDefinition.Name         = dataTypeRegistration.DataTypeInstanceName;
                dataTypeDefinition.DatabaseType = dataTypeRegistration.UmbracoDatabaseType;
            }
            return(dataTypeDefinition);
        }
コード例 #8
0
        private void PersistDataTypeAndPreValues(DataTypeRegistration dataTypeRegistration, IDataTypeDefinition dataTypeDefinition, IDictionary <string, PreValue> preValues)
        {
            CodeFirstManager.Current.Log("Saving data type " + dataTypeRegistration.DataTypeInstanceName, this);
            if (CodeFirstManager.Current.Features.InitialisationMode == InitialisationMode.Ensure)
            {
                throw new CodeFirstPassiveInitialisationException("The data types or prevalues defined in the database do not match the types passed in to initialise. In InitialisationMode.Ensure the types must match or the site will be prevented from starting.");
            }
            else if (CodeFirstManager.Current.Features.InitialisationMode == InitialisationMode.Sync)
            {
                UmbracoContext.EnsureContext(new HttpContextWrapper(new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()))), ApplicationContext.Current, true);

                _service.SaveDataTypeAndPreValues(dataTypeDefinition, preValues);
                //reset the collection if we've modified a type
                _allDataTypeDefinitions = new Lazy <IEnumerable <IDataTypeDefinition> >(() =>
                {
                    return(_service.GetAllDataTypeDefinitions());
                });
            }
            else if (CodeFirstManager.Current.Features.InitialisationMode != InitialisationMode.Passive)
            {
                throw new CodeFirstException("Unknown initialisation mode");
            }
        }
コード例 #9
0
        private DataTypeRegistration BuildDataTypeRegistration(Type type)
        {
            DataTypeRegistration dataTypeRegistration;
            DataTypeAttribute    dataTypeAttribute = type.GetCodeFirstAttribute <DataTypeAttribute>();
            bool controlled = type.GetCustomAttribute <DoNotSyncDataTypeAttribute>(false) == null && type.GetCustomAttribute <BuiltInDataTypeAttribute>(false) == null;

            if (dataTypeAttribute == null)
            {
                throw new CodeFirstException(type.Name + " is not a valid data type");
            }

            dataTypeRegistration = new DataTypeRegistration()
            {
                ClrType              = type,
                ConverterType        = dataTypeAttribute.ConverterType,
                DataTypeInstanceName = dataTypeAttribute.Name,
                PropertyEditorAlias  = dataTypeAttribute.PropertyEditorAlias,
                UmbracoDatabaseType  = dataTypeAttribute.DbType,
                CodeFirstControlled  = controlled
            };
            _registerController.Register(type, dataTypeRegistration);

            return(dataTypeRegistration);
        }
コード例 #10
0
 /// <summary>
 /// Gets the registration for the specified property instance
 /// </summary>
 /// <exception cref="KeyNotFoundException">Thrown if the property instance is not registered</exception>
 public bool TryGetRegistration(Type dataType, out DataTypeRegistration registration)
 {
     return _register.TryGetValue(dataType, out registration);
 }
コード例 #11
0
        private IDataTypeDefinition UpdateOrCreateDataTypeDefinition(IDictionary <string, PreValue> codeFirstPreValues, bool updateDataTypeDefinition, DataTypeRegistration dataTypeRegistration)
        {
            IDataTypeDefinition dataTypeDefinition = _allDataTypeDefinitions.Value.SingleOrDefault(x => string.Equals(x.Name, dataTypeRegistration.DataTypeInstanceName, StringComparison.InvariantCultureIgnoreCase));

            if (updateDataTypeDefinition)
            {
                bool modified = false;
                if (dataTypeDefinition == null)
                {
                    CodeFirstManager.Current.Log("Creating data type " + dataTypeRegistration.DataTypeInstanceName, this);
                    dataTypeDefinition = CreateDataTypeDefinition(dataTypeRegistration);
                    modified           = true;
                }
                else
                {
                    CodeFirstManager.Current.Log("Syncing data type " + dataTypeRegistration.DataTypeInstanceName, this);
                    modified = UpdateDataTypeDefinition(dataTypeRegistration, dataTypeDefinition);
                }

                dataTypeRegistration.Definition = dataTypeDefinition;
                if (dataTypeRegistration.CodeFirstControlled || modified)
                {
                    IDictionary <string, PreValue> preValues = MergePreValuesWithExisting(dataTypeRegistration, codeFirstPreValues, ref modified);
                    if (modified)
                    {
                        PersistDataTypeAndPreValues(dataTypeRegistration, dataTypeDefinition, preValues);
                    }
                }
            }
            else if (dataTypeRegistration.Definition == null)
            {
                dataTypeRegistration.Definition = dataTypeDefinition;
            }

            return(dataTypeDefinition);
        }
コード例 #12
0
        private void MergeDataTypeInstanceWithAncestor(DataTypeInstanceAttribute customDataTypeAttribute, PropertyInfo property, DataTypeRegistration underlyingType)
        {
            if (customDataTypeAttribute.Name == null)
            {
                customDataTypeAttribute.Name = underlyingType.DataTypeInstanceName + " (" + property.DeclaringType.Name + "." + property.Name + ")";
            }

            if (customDataTypeAttribute.PropertyEditorAlias == null)
            {
                customDataTypeAttribute.PropertyEditorAlias = underlyingType.PropertyEditorAlias;
            }

            if (customDataTypeAttribute.ConverterType == null)
            {
                customDataTypeAttribute.ConverterType = underlyingType.ConverterType;
            }

            if (customDataTypeAttribute.DbType == DatabaseType.None)
            {
                customDataTypeAttribute.DbType = underlyingType.DbType;
            }
        }
コード例 #13
0
 /// <summary>
 /// Registers the given data type definition for the specified type
 /// </summary>
 /// <exception cref="CodeFirstException">Thrown if the specified type is already registered. 
 /// This operation is not thread safe. If Register may be called from multiple threads in your application
 /// then you are responsible for synchronising those calls.</exception>
 public void Register(Type dataType, DataTypeRegistration definition)
 {
     if (!_instance._register.TryAdd(dataType, definition))
     {
         throw new CodeFirstException("Data type already registered");
     }
 }
コード例 #14
0
        private DataTypeRegistration BuildDataTypeRegistration(PropertyInfo instance, out bool typeExisted)
        {
            DataTypeInstanceAttribute dataTypeInstanceAttribute = instance.GetCodeFirstAttribute <DataTypeInstanceAttribute>();

            if (dataTypeInstanceAttribute == null && instance.GetCustomAttributes(false).Any(x => x.GetType().Implements <IDataTypeInstance>()))
            {
                dataTypeInstanceAttribute = new DataTypeInstanceAttribute();
            }

            var dataType = instance.GetCodeFirstAttribute <ContentPropertyAttribute>().DataType;

            typeExisted = true;

            var  redirect = instance.GetCustomAttributes().FirstOrDefault(x => x.GetType().Implements <IDataTypeRedirect>()) as IDataTypeRedirect;
            Type targetType;

            if (redirect != null)
            {
                if (redirect is IInitialisablePropertyAttribute)
                {
                    (redirect as IInitialisablePropertyAttribute).Initialise(instance);
                }
                targetType = redirect.Redirect(instance);
            }
            else
            {
                targetType = instance.PropertyType;
            }

            if (targetType.IsConstructedGenericType)
            {
                EnsureGenericTypeRegistration(targetType, ref typeExisted);
            }

            if (dataTypeInstanceAttribute == null && string.IsNullOrWhiteSpace(dataType)) //no data type override specified
            {
                return(CheckTypeRegistrationForProperty(instance, targetType));
            }
            else if (!string.IsNullOrWhiteSpace(dataType)) //data type override specified in property attribute
            {
                typeExisted = true;                        //never modify any data type when using PEVCs - we only support back-office managed data types with PEVCs
                return(CreateUmbracoConverterDataType(instance, dataType));
            }

            if (dataTypeInstanceAttribute.HasNullProperties) //data type override specified in instance attribute
            {
                InferInstanceRegistrationProperties(instance, targetType, dataTypeInstanceAttribute);
            }

            typeExisted = false;
            DataTypeRegistration dataTypeRegistration = new DataTypeRegistration()
            {
                ClrType              = targetType,
                ConverterType        = dataTypeInstanceAttribute.ConverterType,
                DataTypeInstanceName = dataTypeInstanceAttribute.Name,
                PropertyEditorAlias  = dataTypeInstanceAttribute.PropertyEditorAlias,
                DbType = dataTypeInstanceAttribute.DbType,
                CodeFirstControlled = true
            };

            _registerController.Register(instance, dataTypeRegistration);
            return(dataTypeRegistration);
        }
コード例 #15
0
 /// <summary>
 /// Registers the given data type definition for the specified property instance
 /// </summary>
 /// <exception cref="CodeFirstException">Thrown if the specified instance is already registered. 
 /// This operation is not thread safe. If Register may be called from multiple threads in your application
 /// then you are responsible for synchronising those calls.</exception>
 public void Register(PropertyInfo instance, DataTypeRegistration definition)
 {
     if (!_instance._instanceRegister.TryAdd(instance.DeclaringType.FullName + "." + instance.Name, definition))
     {
         throw new CodeFirstException("Data type instance already registered");
     }
 }
コード例 #16
0
 /// <summary>
 /// Gets the registration for the specified type
 /// </summary>
 /// <exception cref="KeyNotFoundException">Thrown if the type is not registered</exception>
 public bool TryGetRegistration(PropertyInfo instance, out DataTypeRegistration registration)
 {
     var key = string.Format("{0}.{1}", instance.DeclaringType.FullName, instance.Name);
     return _instanceRegister.TryGetValue(key, out registration);
 }
コード例 #17
0
 public override bool Create(string input, DataTypeRegistration registration)
 {
     return(bool.Parse(input.ToString()));
 }
コード例 #18
0
 /// <summary>
 /// Gets the registration for the specified property instance
 /// </summary>
 /// <exception cref="KeyNotFoundException">Thrown if the property instance is not registered</exception>
 public bool TryGetRegistration(Type dataType, out DataTypeRegistration registration)
 {
     return(_register.TryGetValue(dataType, out registration));
 }
コード例 #19
0
        /// <summary>
        /// Gets the registration for the specified type
        /// </summary>
        /// <exception cref="KeyNotFoundException">Thrown if the type is not registered</exception>
        public bool TryGetRegistration(PropertyInfo instance, out DataTypeRegistration registration)
        {
            var key = string.Format("{0}.{1}", instance.DeclaringType.FullName, instance.Name);

            return(_instanceRegister.TryGetValue(key, out registration));
        }