예제 #1
0
        public void HasValue <TValue>(Func <TEntity, TValue> converter)
        {
            var native = this.converter.Get <TValue>();

            TreeEncoderDescriptor <TState, TNative, TEntity> .BindValue(this.definition, e => native(converter(e)));

            this.definition.Callback = (reader, state, entity) => reader.WriteAsValue(state, native(converter(entity)));
        }
예제 #2
0
        public IEncoderDescriptor <TElement> HasElements <TElement>(Func <TEntity, IEnumerable <TElement> > getter)
        {
            var elementDefinition = this.definition.Create <TElement>();
            var elementDescriptor =
                new TreeEncoderDescriptor <TState, TNative, TElement>(this.converter, elementDefinition);

            return(TreeEncoderDescriptor <TState, TNative, TEntity> .BindArray(this.definition, getter, elementDescriptor));
        }
예제 #3
0
        public IEncoderDescriptor <TField> HasField <TField>(string name, Func <TEntity, TField> getter)
        {
            var fieldDefinition = this.definition.Create <TField>();
            var fieldDescriptor = new TreeEncoderDescriptor <TState, TNative, TField>(this.converter, fieldDefinition);

            return(TreeEncoderDescriptor <TState, TNative, TEntity> .BindField(this.definition, name, this.fields, getter,
                                                                               fieldDescriptor));
        }
예제 #4
0
        public IEncoderDescriptor <TElement> HasElements <TElement>(Func <TEntity, IEnumerable <TElement> > getter,
                                                                    IEncoderDescriptor <TElement> descriptor)
        {
            if (!(descriptor is TreeEncoderDescriptor <TState, TNative, TElement> ancestor))
            {
                throw new ArgumentOutOfRangeException(nameof(descriptor), "incompatible descriptor type");
            }

            return(TreeEncoderDescriptor <TState, TNative, TEntity> .BindArray(this.definition, getter, ancestor));
        }
예제 #5
0
        private static IEncoderDescriptor <TElement> BindArray <TElement>(
            IWriterDefinition <TState, TNative, TEntity> parent, Func <TEntity, IEnumerable <TElement> > getter,
            TreeEncoderDescriptor <TState, TNative, TElement> elementDescriptor)
        {
            var elementDefinition = elementDescriptor.definition;

            parent.Callback = (reader, state, entity) =>
                              reader.WriteAsArray(state, getter(entity), elementDefinition.Callback);

            return(elementDescriptor);
        }
예제 #6
0
        public IEncoderDescriptor <TField> HasField <TField>(string name, Func <TEntity, TField> getter,
                                                             IEncoderDescriptor <TField> descriptor)
        {
            if (!(descriptor is TreeEncoderDescriptor <TState, TNative, TField> ancestor))
            {
                throw new ArgumentOutOfRangeException(nameof(descriptor), "incompatible descriptor type");
            }

            return(TreeEncoderDescriptor <TState, TNative, TEntity> .BindField(this.definition, name, this.fields, getter,
                                                                               ancestor));
        }
예제 #7
0
        private static IEncoderDescriptor <TField> BindField <TField>(
            IWriterDefinition <TState, TNative, TEntity> parentDefinition,
            string name, Dictionary <string, WriterCallback <TState, TNative, TEntity> > parentFields,
            Func <TEntity, TField> getter, TreeEncoderDescriptor <TState, TNative, TField> fieldDescriptor)
        {
            if (parentFields.ContainsKey(name))
            {
                throw new InvalidOperationException($"field '{name}' was declared twice on same descriptor");
            }

            parentDefinition.Callback = (reader, state, entity) => reader.WriteAsObject(state, entity, parentFields);

            var fieldDefinition = fieldDescriptor.definition;

            parentFields[name] = (reader, state, entity) => fieldDefinition.Callback(reader, state, getter(entity));

            return(fieldDescriptor);
        }
예제 #8
0
        public void HasValue()
        {
            var converter = this.converter.Get <TEntity>();

            TreeEncoderDescriptor <TState, TNative, TEntity> .BindValue(this.definition, converter);
        }