Ejemplo n.º 1
0
		/// <summary>
		/// Initializes a new instance of the <see cref="DextopModelArraySerializer"/> class.
		/// </summary>
		/// <param name="meta">The meta.</param>
        public DextopModelArraySerializer(DextopModelTypeMeta meta)
        {
            Meta = meta;
            Fields = new List<FieldInfo>();
           
            foreach (var field in meta.Fields)
            {
                var property = meta.ModelType.GetProperty(field);
                var vp = BuildValueProvider(property);
                Fields.Add(new FieldInfo
                {
                    CanRead = property.CanRead,
                    CanWrite = property.CanWrite,
                    PropertyType = property.PropertyType,
                    ValueProvider = vp
                });
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DextopModelArraySerializer"/> class.
        /// </summary>
        /// <param name="meta">The meta.</param>
        public DextopModelArraySerializer(DextopModelTypeMeta meta)
        {
            Meta   = meta;
            Fields = new List <FieldInfo>();

            foreach (var field in meta.Fields)
            {
                var property = meta.ModelType.GetProperty(field);
                var vp       = BuildValueProvider(property);
                Fields.Add(new FieldInfo
                {
                    CanRead       = property.CanRead,
                    CanWrite      = property.CanWrite,
                    PropertyType  = property.PropertyType,
                    ValueProvider = vp
                });
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DextopModelArraySerializer"/> class.
        /// </summary>
        /// <param name="meta">The meta.</param>
        public DextopModelArraySerializer(DextopModelTypeMeta meta)
        {
            Meta = meta;
            Getters = new List<Func<object,object>>();
            Setters = new List<Action<object,object>>();
            foreach (var field in meta.Fields)
            {
                Func<object, object> getter = null;
                Action<object, object> setter = null;
                var property = meta.ModelType.GetProperty(field);
                if (property.CanRead)
                    getter = (target) => { return property.GetValue(target, null); };
                if (property.CanWrite)
                    setter = (target, value) => { property.SetValue(target, ChangeType(value, property.PropertyType), null); };

                Getters.Add(getter);
                Setters.Add(setter);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DextopLiveStore"/> class.
 /// </summary>
 /// <param name="meta">The meta.</param>
 /// <param name="source">The source.</param>
 /// <param name="serializer">The serializer.</param>
 public DextopLiveStore(DextopModelTypeMeta meta, IDextopObservableStore source = null, IDextopModelSerializer serializer = null)
 {
     this.meta = meta;
     this.serializer = serializer ?? meta.ArraySerializer;
     Source = source;
 }
Ejemplo n.º 5
0
        internal DextopModel BuildModel(Type type, DextopModelAttribute modelAttribute)
        {
            DextopModelTypeMeta meta;
            
            if (modelAttribute!=null)
                meta = new DextopModelTypeMeta
                {
                    IdField = modelAttribute.Id,
                    ModelName = modelAttribute.Name,
                    DefaultSerializerType = modelAttribute.DefaultSerializer
                };
            else
                meta = new DextopModelTypeMeta();

            if (meta.ModelType != null && meta.ModelType != type)
                throw new DextopException("Type mismatch.");

            var excludeFields = new HashSet<String>();
            if (meta.ExcludedFields != null)
                foreach (var f in meta.ExcludedFields)
                    excludeFields.Add(f);

            var properties = type.GetProperties();
            var fields = type.GetFields();
            var combined = ((MemberInfo[])fields).Union(properties);
            var model = new DextopModel
            {
                Meta = meta
            };

            List<String> idCandidates = new List<string>();
            var dfat = new DextopModelFieldAttribute();

            foreach (var p in combined)
            {
                Type fieldType;
                if (p is PropertyInfo)
                    fieldType = ((PropertyInfo)p).PropertyType;
                else
                    fieldType = ((FieldInfo)p).FieldType;

                var ea = AttributeHelper.GetCustomAttribute<DextopModelExcludeAttribute>(p, true);
                if (ea != null)
                    excludeFields.Add(p.Name);
                if (!excludeFields.Contains(p.Name))
                {
                    var fat = AttributeHelper.GetCustomAttribute<DextopModelFieldAttribute>(p, true) ?? dfat;
                    String ft, et;
                    if (!DextopModelFieldTypeMapper.TryGetFieldTypeName(fieldType, out ft, out et))
                    {
                        excludeFields.Add(p.Name);
                        continue;
                    }
                    var field = new DextopModel.Field
                    {
                        name = p.Name,
                        type = fat.type ?? ft,
                        defaultValue = fat.defaultValue,
                        useNull = fat.useNotNull ? false : true,//fieldType.IsClass || Codaxy.Common.Nullable.IsNullableType(type) || fieldType == typeof(String)
                        dateFormat = fat.dateFormat,
                        mapping = fat.mapping,
                        sortDir = fat.sortDir,
                        persist = fat.persist,
                        convert = fat.convert,
                        sortType = fat.sortType
                    };
                    model.Fields.Add(field);
                    if (meta.IdField == null)
                    {
                        var ida = AttributeHelper.GetCustomAttribute<DextopModelIdAttribute>(p, true);
                        if (ida != null)
                        {
                            meta.IdField = p.Name;
                            field.useNull = true;
                        }
                        else
                        {
                            if (p.Name.EndsWith("ID") || p.Name.EndsWith("id") || p.Name.EndsWith("Id"))
                                idCandidates.Add(p.Name);
                        }
                    }

                    var validations = AttributeHelper.GetCustomAttributes<DextopModelValidationAttribute>(p, true);
                    foreach (var v in validations)
                    {
                        var validation = v.ToValidation(p.Name, fieldType);
                        model.Validations.Add(validation);
                    }
                }
            }

            if (meta.IdField == null)
            {
                if (idCandidates.Count == 1)
                {
                    meta.IdField = idCandidates[0];
                }
                else
                    throw new DextopException("Model for type '{0}' could not be generated as id field could not be resolved.", type);
            }

            model.idProperty = meta.IdField;
			if (meta.ModelName == null)
				meta.ModelName = Application.MapTypeName(type, ".model");
            meta.ExcludedFields = excludeFields.Count == 0 ? null : excludeFields.ToArray();
            meta.Fields = model.Fields.Select(a => a.name).ToArray();
            meta.ModelType = type;
            meta.IsTreeModel = modelAttribute.IsTreeModel;
            
            if (!metas.TryAdd(type, meta))
                throw new DextopException("Model for type '{0}' already registered.", type);

            return model;                
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DextopProxy"/> class.
 /// </summary>
 /// <param name="dataProxy">The data proxy service.</param>
 /// <param name="meta">The meta.</param>
 /// <param name="serializer">The serializer.</param>
 public DextopProxy(IDextopDataProxy dataProxy, DextopModelTypeMeta meta, IDextopModelSerializer serializer = null)
 {
     this.proxy      = dataProxy;
     this.meta       = meta;
     this.serializer = serializer ?? meta.DefaultSerializer;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DextopProxy"/> class.
 /// </summary>
 /// <param name="dataProxy">The data proxy service.</param>
 /// <param name="meta">The meta.</param>
 /// <param name="serializer">The serializer.</param>
 public DextopProxy(IDextopDataProxy dataProxy, DextopModelTypeMeta meta, IDextopModelSerializer serializer = null)
 {
     this.proxy = dataProxy;
     this.meta = meta;
     this.serializer = serializer ?? meta.ArraySerializer;
 }
Ejemplo n.º 8
0
 public DextopModelJsonSerializer(DextopModelTypeMeta meta)
 {
     Meta = meta;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DextopLiveStore"/> class.
 /// </summary>
 /// <param name="meta">The meta.</param>
 /// <param name="source">The source.</param>
 /// <param name="serializer">The serializer.</param>
 public DextopLiveStore(DextopModelTypeMeta meta, IDextopObservableStore source = null, IDextopModelSerializer serializer = null)
 {
     this.meta       = meta;
     this.serializer = serializer ?? meta.ArraySerializer;
     Source          = source;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Builds the model for given type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="modelAttribute">The model attribute.</param>
        /// <returns></returns>
        public DextopModel BuildModel(Type type, DextopModelAttribute modelAttribute = null)
        {
            DextopModelTypeMeta meta;

            if (modelAttribute != null)
            {
                meta = new DextopModelTypeMeta
                {
                    IdField               = modelAttribute.Id,
                    ModelName             = modelAttribute.Name,
                    DefaultSerializerType = modelAttribute.DefaultSerializer
                }
            }
            ;
            else
            {
                meta = new DextopModelTypeMeta();
            }

            if (meta.ModelType != null && meta.ModelType != type)
            {
                throw new DextopException("Type mismatch.");
            }

            var excludeFields = new HashSet <String>();

            if (meta.ExcludedFields != null)
            {
                foreach (var f in meta.ExcludedFields)
                {
                    excludeFields.Add(f);
                }
            }

            var properties = type.GetProperties();
            var fields     = type.GetFields();
            var combined   = ((MemberInfo[])fields).Union(properties);
            var model      = new DextopModel
            {
                Meta = meta
            };

            List <String> idCandidates = new List <string>();
            var           dfat         = new DextopModelFieldAttribute();

            foreach (var p in combined)
            {
                Type fieldType;
                if (p is PropertyInfo)
                {
                    fieldType = ((PropertyInfo)p).PropertyType;
                }
                else
                {
                    fieldType = ((FieldInfo)p).FieldType;
                }

                var ea = AttributeHelper.GetCustomAttribute <DextopModelExcludeAttribute>(p, true);
                if (ea != null)
                {
                    excludeFields.Add(p.Name);
                }
                if (!excludeFields.Contains(p.Name))
                {
                    var    fat = AttributeHelper.GetCustomAttribute <DextopModelFieldAttribute>(p, true) ?? dfat;
                    String ft;
                    if (!DextopModelFieldTypeMapper.TryGetFieldTypeName(fieldType, out ft))
                    {
                        excludeFields.Add(p.Name);
                        continue;
                    }
                    var field = new DextopModel.Field
                    {
                        name         = p.Name,
                        type         = fat.type ?? ft,
                        defaultValue = fat.defaultValue,
                        useNull      = fat.useNotNull ? false : true,//fieldType.IsClass || Codaxy.Common.Nullable.IsNullableType(type) || fieldType == typeof(String)
                        dateFormat   = fat.dateFormat,
                        mapping      = fat.mapping,
                        sortDir      = fat.sortDir,
                        persist      = fat.persist,
                        convert      = fat.convert,
                        sortType     = fat.sortType
                    };
                    model.Fields.Add(field);
                    if (meta.IdField == null)
                    {
                        var ida = AttributeHelper.GetCustomAttribute <DextopModelIdAttribute>(p, true);
                        if (ida != null)
                        {
                            meta.IdField  = p.Name;
                            field.useNull = true;
                        }
                        else
                        {
                            if (p.Name.EndsWith("ID") || p.Name.EndsWith("id") || p.Name.EndsWith("Id"))
                            {
                                idCandidates.Add(p.Name);
                            }
                        }
                    }

                    var validations = AttributeHelper.GetCustomAttributes <DextopModelValidationAttribute>(p, true);
                    foreach (var v in validations)
                    {
                        var validation = v.ToValidation(p.Name, fieldType);
                        model.Validations.Add(validation);
                    }
                }
            }

            if (meta.IdField == null)
            {
                if (idCandidates.Count == 1)
                {
                    meta.IdField = idCandidates[0];
                }
                else
                {
                    throw new DextopException("Model for type '{0}' could not be generated as id field could not be resolved.", type);
                }
            }

            model.idProperty = meta.IdField;
            if (meta.ModelName == null)
            {
                meta.ModelName = Application.MapTypeName(type, ".model");
            }
            meta.ExcludedFields = excludeFields.Count == 0 ? null : excludeFields.ToArray();
            meta.Fields         = model.Fields.Select(a => a.name).ToArray();
            meta.ModelType      = type;

            if (!metas.TryAdd(type, meta))
            {
                throw new DextopException("Model for type '{0}' already registered.", type);
            }

            return(model);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DextopModelArraySerializer"/> class.
 /// </summary>
 /// <param name="meta">The meta.</param>
 public DextopModelDynamicArraySerializer(DextopModelTypeMeta meta) : base(meta)
 {
 }
 public DextopModelJsonSerializer(DextopModelTypeMeta meta)
 {
     Meta = meta;
 }
        /// <summary>
		/// Initializes a new instance of the <see cref="DextopModelArraySerializer"/> class.
		/// </summary>
		/// <param name="meta">The meta.</param>
        public DextopModelDynamicArraySerializer(DextopModelTypeMeta meta) : base(meta) {}