<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace System.ComponentModel.Editable
{
    public abstract class JasilyEditableViewModel<T> : JasilyViewModel<T>
    {
        protected Type ThisType { get; private set; }

        protected Type SourceType { get; private set; }

        public JasilyEditableViewModel(T source)
            : base(source)
        {
            this.ThisType = this.GetType();
            this.SourceType = typeof(T);
        }

        public virtual void WriteToObject(T obj)
        {
            if (ReferenceEquals(obj, null)) return;

            foreach (var field in this.SourceType.GetRuntimeFields()
                .Where(f => f.GetCustomAttribute<EditableFieldAttribute>() != null))
            {
                var getter = this.ThisType.GetGetter(field.Name);
                if (getter != null)
                {
                    field.SetValue(obj, getter(this));
                }
            }

            foreach (var property in this.SourceType.GetRuntimeProperties()
                .Where(f => f.GetCustomAttribute<EditableFieldAttribute>() != null))
            {
                var getter = this.ThisType.GetGetter(property.Name);
                if (getter != null)
                {
                    property.SetValue(obj, getter(this));
                }
            }
        }

        public virtual void ReadFromObject(T obj)
        {
            if (ReferenceEquals(obj, null)) return;

            foreach (var field in this.SourceType.GetRuntimeFields()
                .Where(f => f.GetCustomAttribute<EditableFieldAttribute>() != null))
            {
                var setter = this.ThisType.GetSetter(field.Name);
                if (setter != null)
                {
                    setter(this, field.GetValue(obj));
                }
            }

            foreach (var property in this.SourceType.GetRuntimeProperties()
                .Where(f => f.GetCustomAttribute<EditableFieldAttribute>() != null))
            {
                var setter = this.ThisType.GetSetter(property.Name);
                if (setter != null)
                {
                    setter(this, property.GetValue(obj));
                }
            }
        }
    }
}<commit_msg>1. fixed a design error.<commit_after>﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

namespace System.ComponentModel.Editable
{
    public abstract class JasilyEditableViewModel<T> : JasilyViewModel<T>
    {
        private List<Action<object, object>> WriteToObjectMapped;
        private List<Action<object, object>> ReadFromObjectMapped;
        private Dictionary<string, Tuple<Func<object, object>, Action<object, object>>> ThisMapped;
        private Dictionary<string, Tuple<Func<object, object>, Action<object, object>>> SourceMapped;

        protected Type ThisType { get; private set; }

        protected Type SourceType { get; private set; }

        public JasilyEditableViewModel(T source)
            : base(source)
        {
            this.ThisType = this.GetType();
            this.SourceType = typeof(T);
        }

        private void MappingThis()
        {
            if (this.ThisMapped == null)
            {
                var mapped = new Dictionary<string, Tuple<Func<object, object>, Action<object, object>>>();
                foreach (var field in this.GetType().GetRuntimeFields()
                    .Where(f => f.GetCustomAttribute<EditableFieldAttribute>() != null))
                {
                    mapped.Add(field.Name, new Tuple<Func<object, object>, Action<object, object>>(field.GetValue, field.SetValue));
                }
                foreach (var property in this.GetType().GetRuntimeProperties()
                    .Where(p => p.GetCustomAttribute<EditableFieldAttribute>() != null))
                {
                    mapped.Add(property.Name, new Tuple<Func<object, object>, Action<object, object>>(property.GetValue, property.SetValue));
                }

                this.ThisMapped = mapped;
            }
        }

        private void MappingSource()
        {
            Debug.Assert(this.ThisMapped != null);

            if (this.SourceMapped == null)
            {
                Tuple<Func<object, object>, Action<object, object>> tmp;
                var mapped = new Dictionary<string, Tuple<Func<object, object>, Action<object, object>>>();
                foreach (var field in typeof(T).GetRuntimeFields())
                {
                    if (this.ThisMapped.TryGetValue(field.Name, out tmp))
                    {
                        mapped.Add(field.Name, new Tuple<Func<object, object>, Action<object, object>>(field.GetValue, field.SetValue));
                    }
                }
                foreach (var property in typeof(T).GetRuntimeProperties())
                {
                    if (this.ThisMapped.TryGetValue(property.Name, out tmp))
                    {
                        mapped.Add(property.Name, new Tuple<Func<object, object>, Action<object, object>>(property.GetValue, property.SetValue));
                    }
                }

                this.SourceMapped = mapped;
            }
        }

        private void MappingWrite()
        {
            if (this.WriteToObjectMapped == null)
            {
                this.MappingThis();
                this.MappingSource();
                var mapping = new List<Action<object, object>>();
                foreach (var kvp in this.ThisMapped)
                {
                    var getter = kvp.Value.Item1;
                    var setter = this.SourceMapped[kvp.Key].Item2;
                    mapping.Add((source, dest) => setter(dest, getter(source)));
                }
                this.WriteToObjectMapped = mapping;
            }
        }

        private void MappingRead()
        {
            if (this.ReadFromObjectMapped == null)
            {
                this.MappingThis();
                this.MappingSource();
                var mapping = new List<Action<object, object>>();
                foreach (var kvp in this.ThisMapped)
                {
                    var getter = this.SourceMapped[kvp.Key].Item1;
                    var setter = kvp.Value.Item2;
                    mapping.Add((source, dest) => setter(dest, getter(source)));
                }
                this.ReadFromObjectMapped = mapping;
            }
        }

        public virtual void WriteToObject(T obj)
        {
            if (ReferenceEquals(obj, null)) return;

            this.MappingWrite();

            foreach (var m in this.WriteToObjectMapped)
            {
                m(this, obj);
            }
        }

        public virtual void ReadFromObject(T obj)
        {
            if (ReferenceEquals(obj, null)) return;

            this.MappingRead();

            foreach (var m in this.ReadFromObjectMapped)
            {
                m(obj, this);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10245901639344263, 'fraction_numerical': 0.0007066139061616733, 'mean_word_length': 1.54568345323741, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1086666', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1749, 'n_words': 371}