public override void Execute(DTObject dto) { foreach (var findExp in _findExps) { RemoveEntities(dto, findExp); } }
public void SetObject(string findExp, DTObject obj) { ValidateReadOnly(); if (string.IsNullOrEmpty(findExp)) { //dto.Set(newDTO) 这种表达式下说明此时需要替换整个dto //为了保证数据安全,需要克隆,{xxx:{a,b}},如果不克隆,那么b=xxx就会出现错误 var newRoot = obj.GetRoot().Clone() as DTEObject; newRoot.Parent = _root.Parent; _root = newRoot; } else { DTEObject entity = FindEntity <DTEObject>(findExp, false); if (entity == null) { var query = QueryExpression.Create(findExp); _root.SetEntity(query, (name) => { var e = obj.GetRoot().Clone(); e.Name = name; return(e); }); } } }
/// <summary> /// 向集合追加一个成员 /// </summary> /// <param name="findExp"></param> /// <param name="member"></param> public void Push(string findExp, DTObject member) { ValidateReadOnly(); DTEList entity = FindEntity <DTEList>(findExp, false); if (entity == null) { var query = QueryExpression.Create(findExp); _root.SetEntity(query, (name) => { var dte = DTOPool.CreateDTEList(this.IsPinned); dte.Name = name; return(dte); }); entity = FindEntity <DTEList>(findExp, true); } ; if (member == null) { return; } entity.Push(member); }
private void SetValue(DTEntity target, object value, string findExp) { var parent = target.Parent as DTEObject; if (parent == null) { throw new DTOException("表达式错误" + findExp); } var query = QueryExpression.Create(target.Name); parent.SetEntity(query, (name) => { var dtoValue = value as DTObject; if (dtoValue != null) { var t = dtoValue.Clone(); var newEntity = t.GetRoot(); newEntity.Name = name; return(newEntity); } else { DTObject t = DTObject.CreateReusable(); t.SetValue(value); var newEntity = t.GetRoot().GetFirstEntity(); if (newEntity == null) { throw new DTOException("预期之外的错误," + findExp); } newEntity.Name = name; return(newEntity); } }); }
public object Deserialize(DTObject dto) { var instance = this.ClassType.CreateInstance(); Deserialize(instance, dto); return(instance); }
public DTObject Serialize(object instance, bool isPinned) { var dto = isPinned ? DTObject.Create() : DTObject.CreateReusable(); Serialize(instance, dto); return(dto); }
public object Deserialize(DTObject dto) { using (var temp = _readerPool.Borrow()) { var reader = temp.Item; reader.Initialize(dto); return(_deserializeMethod(reader)); } }
public override void Serialize(object instance, DTObject dto) { using (var temp = _writerPool.Borrow()) { var writer = temp.Item; writer.Initialize(dto, _schemaCodes); SerializeMethod(instance, writer); } }
public SchemaCodes(Type classType, string schemaCode) { _codes = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); _typeCodes = new Dictionary <Type, string>(); this.ContainsAll = string.IsNullOrEmpty(schemaCode); _schema = DTObject.CreateReusable(schemaCode); Initialize(classType, schemaCode); }
public override void Deserialize(object instance, DTObject dto) { using (var temp = _readerPool.Borrow()) { var reader = temp.Item; reader.Initialize(dto, _schemaCodes); DeserializeMethod(instance, reader); } }
public override void Execute(DTObject dto) { var entities = dto.FindEntities(_findExp, false); foreach (var e in entities) { e.Name = _name; } //dto.OrderEntities(); }
public DTObject GetObject(string findExp, DTObject defaultValue) { var entity = this.FindEntity <DTEObject>(findExp, false); if (entity == null) { return(defaultValue); } return(DTOPool.CreateObject(entity, this.IsReadOnly, this.IsPinned)); }
/// <summary> /// 根据架构代码,将dto的数据创建到新实例<paramref name="instanceType"/>中 /// </summary> /// <param name="schemaCode"></param> /// <param name="instanceType"></param> /// <returns></returns> public object Save(Type instanceType, string schemaCode, DTObject dto) { if (instanceType == typeof(DTObject)) { return(dto.Clone()); } TypeSchemaCodeInfo typeInfo = TypeSchemaCodeInfo.GetTypeInfo(instanceType, schemaCode); return(typeInfo.Deserialize(dto)); }
public object Deserialize(Type objectType, DTObject dto) { if (objectType == typeof(DTObject)) { return(dto); } TypeMakupInfo typeInfo = TypeMakupInfo.GetTypeInfo(objectType); return(typeInfo.Deserialize(dto)); }
internal TypeMetadata(ObjectEntry root, string metadataCode, TypeMetadata parent) { this.Root = root; this.MetadataCode = metadataCode; this.Parent = parent; this.Index = parent.Index; var dto = DTObject.CreateReusable(metadataCode); this.Entries = Parse(dto.GetRoot()); }
public static DTEList CreateDTEList(string name, DTObject template, DTObjectList items, bool isPinned) { var dte = isPinned ? new DTEList() : Symbiosis.TryMark(_dteListPool, () => { return(new DTEList()); }); dte.InitByClone(name, template, items, isPinned); return(dte); }
public DTObject GetOrCreateObject(string findExp) { var obj = GetObject(findExp, false); if (obj == null) { obj = this.IsPinned ? DTObject.Create() : DTObject.CreateReusable(); this.SetObject(findExp, obj); } return(obj); }
private string GetItemMetadataCode(string listTypeName, DTObject item) { using (var temp = StringPool.Borrow()) { var code = temp.Item; code.Append("{item:"); code.Append(item.GetCode(false)); code.Append("}"); return(code.ToString()); } }
public void Push(string findExp, IEnumerable list, Func <object, DTObject> factory) { ValidateReadOnly(); var entity = GetOrCreateList(findExp); foreach (object item in list) { DTObject dto = factory(item); entity.Push(dto); } }
public void Push(string findExp, IEnumerable list, Action <DTObject, object> action) { ValidateReadOnly(); DTEList entity = GetOrCreateList(findExp); foreach (object item in list) { DTObject dto = entity.CreateAndPush(); action(dto, item); } }
public void Push(string findExp, int count, Action <DTObject, int> action) { ValidateReadOnly(); var entity = GetOrCreateList(findExp); for (int i = 0; i < count; i++) { DTObject dto = entity.CreateAndPush(); action(dto, i); } }
public T Deserialize <T>(DTObject obj) { var targetType = typeof(T); if (targetType == typeof(DTObject)) { return((T)((object)obj)); } TypeMakupInfo typeInfo = TypeMakupInfo.GetTypeInfo(targetType); return((T)typeInfo.Deserialize(obj)); }
/// <summary> /// 根据架构代码,将dto的数据写入到新实例<paramref name="instanceType"/>中 /// </summary> /// <param name="instance"></param> /// <param name="schemaCode"></param> /// <param name="dto"></param> public void Save(object instance, string schemaCode, DTObject dto) { //if (instance.IsNull()) return; var instanceType = instance.GetType(); if (instanceType == typeof(DTObject)) { instance = dto.Clone(); } TypeSchemaCodeInfo typeInfo = TypeSchemaCodeInfo.GetTypeInfo(instanceType, schemaCode); typeInfo.Deserialize(instance, dto); }
private void RemoveEntities(DTObject dto, string findExp) { var targets = dto.FindEntities(findExp, false); foreach (var target in targets) { var parent = target.Parent; if (parent == null) { throw new DTOException("预期之外的错误," + findExp); } parent.DeletEntity(target); } }
/// <summary> /// 该构造是一切的起点 /// </summary> /// <param name="metadataCode"></param> internal TypeMetadata(string metadataCode) { this.MetadataCode = metadataCode; this.Index = new TypeIndex(); this.Root = new ObjectEntry(this); var dto = DTObject.CreateReusable(metadataCode); var root = dto.GetRoot(); //设置了根类型的名称 this.Root.Name = this.Root.TypeName = root.Name; this.Index.Add(this.Root); //对根类型建立索引 this.Entries = Parse(root); }
/// <summary> /// 根据架构代码将对象的信息加载到dto中 /// </summary> /// <param name="dto"></param> /// <param name="schemaCode"></param> /// <param name="instance"></param> public void Load(DTObject dto, string schemaCode, object instance) { if (instance.IsNull()) { return; } var instanceType = instance.GetType(); if (instanceType == typeof(DTObject)) { return; } TypeSchemaCodeInfo typeInfo = TypeSchemaCodeInfo.GetTypeInfo(instanceType, schemaCode); typeInfo.Serialize(instance, dto); }
public DTObject Serialize(object instance, bool isPinned) { var dto = isPinned ? DTObject.Create() : DTObject.CreateReusable(); var serializable = instance as IDTOSerializable; if (serializable != null) { serializable.Serialize(dto, string.Empty); //string.Empty意味着 序列化的内容会完全替换dto } else { Serialize(instance, dto); } return(dto); }
private void RetainEntities(DTObject dto, string[] findExps) { if (findExps.Length == 0) { return; } //收集需要保留的实体 var targets = new List <ReservedInfo>(); foreach (var findExp in findExps) { var items = dto.FindEntities(findExp, false); foreach (var item in items) { targets.Add(new ReservedInfo(item, true)); //加入自身 //再加入父亲,由于自身需要保留,所以父亲也得保留 var parent = item.Parent; while (parent != null) { targets.Add(new ReservedInfo(parent, false)); parent = parent.Parent; } } } targets = targets.Distinct().ToList(); //过滤重复 var removes = new List <DTEntity>(); CollectNeedRemove(dto.GetRoot().GetEntities(), targets, removes); foreach (var t in removes) { var parent = t.Parent; if (parent == null) { throw new DTOException("预期之外的错误," + string.Join(";", findExps)); } parent.DeletEntity(t); } }
/// <summary> /// 该方法用于更改成员的值 /// </summary> /// <param name="dto"></param> /// <param name="findExp"></param> /// <param name="valueFindExp"></param> /// <param name="transformValue"></param> public void ChangeValue(DTObject dto, string findExp, string valueFindExp, Func <object, object> transformValue) { ArgumentAssert.IsNotNullOrEmpty(findExp, "findExp"); ArgumentAssert.IsNotNullOrEmpty(valueFindExp, "valueFindExp"); //1.找出需要赋值的目标成员 var targets = dto.FindEntities(findExp, false); if (targets.Length == 0) { dto.SetValue(findExp, string.Empty); //如果没有成员,就自动生成 } targets = dto.FindEntities(findExp, false); var valueExpression = _getValueExpression(valueFindExp); foreach (var target in targets) { var parent = target.Parent as DTEObject; if (parent == null) { throw new DTOException("预期之外的错误," + valueExpression.FindExp); } var parentDTO = valueExpression.StartRoot ? dto : DTOPool.CreateObject(parent, dto.IsReadOnly, dto.IsPinned); //2.找出值,值是在目标成员所在的对象下进行查找的 var entities = parentDTO.FindEntities(valueExpression.FindExp, false); if (entities.Length == 1) { //获取值 var ve = entities[0]; var newValue = GetValue(ve, transformValue, dto.IsReadOnly); if (newValue == null) { throw new DTOException("预期之外的数据转换," + valueExpression.FindExp); } //目标值是唯一的,这个时候要进一步判断 var valueObjParent = ve.Parent.Parent as DTEList; //这是值所在的对象的父亲 //if (valueObjFather != null && ve!=target) //如果值所在的对象处在集合中,并且不是自身对自身赋值,那么还是要以集合形式赋值 if (valueObjParent != null && ve.Parent != target.Parent) //如果值所在的对象处在集合中,并且不是自身对象对自身对象赋值,那么还是要以集合形式赋值 { //以集合赋值 SetValue(target, new object[] { newValue }, valueExpression.FindExp); } else { //赋单值 SetValue(target, newValue, valueExpression.FindExp); } } else if (entities.Length > 1) { //如果目标值是多个,那么是集合类型,这时候需要收集所有转换后的值,再赋值 List <object> values = new List <object>(entities.Length); foreach (var e in entities) { var newValue = GetValue(e, transformValue, dto.IsReadOnly); if (newValue == null) { throw new DTOException("预期之外的数据转换," + valueExpression.FindExp); } values.Add(newValue); } SetValue(target, values, valueExpression.FindExp); } else { //值为0,需要判断是否为数组 } } }
public void Execute(DTObject dto, Func <object, object> transformValue) { ChangeValue(dto, _findExp, _valueFindExp, transformValue); }