private object ReadMemberByLazy(PropertyRepositoryAttribute tip, DynamicData data) { var child = GetChildTableByRuntime(this, tip); var dataKey = _getIdName(tip.PropertyName); object id = null; if (data.TryGetValue(dataKey, out id)) { object rootId = null; var rootIdName = this.Type == DataTableType.AggregateRoot ? EntityObject.IdPropertyName : GeneratedField.RootIdName; if (data.TryGetValue(rootIdName, out rootId)) { var member = child.QuerySingle(rootId, id); if (tip.DomainPropertyType == DomainPropertyType.AggregateRoot) { //尝试加载快照 if (member.IsNull()) { return(ReadSnapshot(tip, id)); } } return(member); } } return(null); }
/// <summary> /// 读取基础数据的集合值 /// </summary> /// <param name="tip"></param> /// <param name="prmTip"></param> /// <param name="parent"></param> /// <param name="rootId"></param> /// <param name="masterId"></param> /// <param name="level"></param> /// <returns></returns> private object ReadValues(PropertyRepositoryAttribute tip, ParameterRepositoryAttribute prmTip, DomainObject parent, object rootId, object masterId, QueryLevel level) { using (var temp = SqlHelper.BorrowDatas()) { var datas = temp.Item; QueryPrimitiveValues(rootId, masterId, datas); Type implementType = null; if (parent == null) { //说明还在构造阶段,或者是内部调用 implementType = prmTip?.ImplementType ?? this.ObjectType; //middle表对应的是属性的基类类型 } else { implementType = this.ObjectType; //middle表对应的是属性的基类类型 } var list = CreateList(parent, implementType, tip.Property); var elementType = this.ElementType; var valueName = GeneratedField.PrimitiveValueName; foreach (var data in datas) { var value = data.Get(valueName); list.Add(value); } return(list); } }
private void ValidateType(PropertyRepositoryAttribute attribute) { if (!DataUtil.IsPrimitiveType(attribute.PropertyType) && !attribute.IsEmptyable) { throw new DomainDesignException(Strings.DomainObjectTypeWrong); } }
public DataField(PropertyRepositoryAttribute tip, DbType dbType, DbFieldType[] dbFieldTypes) { this.IsAdditional = false;//默认情况下不是附加字段 this.Tip = tip; this.DbType = dbType; _dbFieldTypes = dbFieldTypes.ToList(); this.Derivatives = new List <DataTable>(); }
public static GeneratedField Create(Type ownerType, Type propertyType, string name) { var attr = new PropertyRepositoryAttribute() { Property = new CustomProperty(ownerType, propertyType, name) }; return(new GeneratedField(attr, name, GeneratedFieldType.User)); }
public static GeneratedField CreateString(Type ownerType, string name, int maxLength, bool ascii) { var attr = new PropertyRepositoryAttribute() { Property = new StringProperty(ownerType, name, maxLength, ascii) }; return(new GeneratedField(attr, name, GeneratedFieldType.User)); }
/// <summary> /// 创建中间表多个数据的排序序号键 /// </summary> /// <param name="reflectedType"></param> /// <returns></returns> public static GeneratedField CreateOrderIndex(Type reflectedType, params DbFieldType[] types) { var attr = new PropertyRepositoryAttribute() { Property = new IntProperty(reflectedType, OrderIndexName) }; return(new GeneratedField(attr, OrderIndexName, GeneratedFieldType.Index, types)); }
public static GeneratedField CreateValueObjectPrimaryKey(Type reflectedType) { var attr = new PropertyRepositoryAttribute() { Property = new GuidProperty(reflectedType, EntityObject.IdPropertyName) }; return(new GeneratedField(attr, EntityObject.IdPropertyName, GeneratedFieldType.ValueObjectPrimaryKey, DbFieldType.PrimaryKey)); }
/// <summary> /// 创建引用次数的键 /// </summary> /// <param name="reflectedType"></param> /// <returns></returns> public static GeneratedField CreateAssociatedCount(Type reflectedType) { var attr = new PropertyRepositoryAttribute() { Property = new IntProperty(reflectedType, AssociatedCountName) }; return(new GeneratedField(attr, AssociatedCountName, GeneratedFieldType.AssociatedCount, DbFieldType.Common)); }
/// <summary> /// 领域类型的编号字段 /// </summary> /// <param name="reflectedType"></param> /// <returns></returns> public static GeneratedField CreateTypeKey(Type reflectedType) { var attr = new PropertyRepositoryAttribute() { Property = new StringProperty(reflectedType, TypeKeyName, 50, true) }; return(new GeneratedField(attr, TypeKeyName, GeneratedFieldType.TypeKey, DbFieldType.Common)); }
/// <summary> /// 更新时间的字段 /// </summary> /// <param name="reflectedType"></param> /// <returns></returns> public static GeneratedField CreateDataVersion(Type reflectedType) { var attr = new PropertyRepositoryAttribute() { Property = new IntProperty(reflectedType, DataVersionName) }; return(new GeneratedField(attr, DataVersionName, GeneratedFieldType.DataVersion, DbFieldType.Common)); }
private object ReadValueListFromData(PropertyRepositoryAttribute tip, DynamicData data) { object value = null; if (data.TryGetValue(tip.PropertyName, out value)) { return(ParseValueList(tip, value)); } return(null); }
/// <summary> /// /// </summary> /// <param name="tip"></param> /// <param name="id"></param> /// <returns></returns> private object ReadSnapshot(PropertyRepositoryAttribute tip, object id) { var objectTip = ObjectRepositoryAttribute.GetTip(tip.PropertyType, true); if (objectTip.Snapshot) //如果外部内聚根是有快照的,那么当数据不存在时,加载快照 { var idName = _getIdName(tip.PropertyName); var root = DataModel.Create(objectTip.ObjectType).Root; return(root.QuerySingle(id, QueryLevel.None)); } return(DomainObject.GetEmpty(tip.PropertyType)); }
/// <summary> /// 获取基元类型的属性值 /// </summary> /// <returns></returns> private static object GetPrimitivePropertyValue(DomainObject obj, PropertyRepositoryAttribute tip) { var value = obj.GetValue(tip.Property); if (!tip.IsEmptyable) { return(value); } var e = (IEmptyable)value; return(e.IsEmpty() ? null : e.GetValue()); //可以存null值在数据库 }
public static bool IsASCIIString(this PropertyRepositoryAttribute attr) { var stringProperty = attr.Property as GeneratedField.StringProperty; if (stringProperty != null) { return(stringProperty.IsASCII); } else { return(attr.Property.GetAttribute <ASCIIStringAttribute>() != null); } }
private object ReadAggregateRoot(PropertyRepositoryAttribute tip, DynamicData data) { var dataKey = _getIdName(tip.PropertyName); object id = null; if (data.TryGetValue(dataKey, out id)) { var model = DataModel.Create(tip.PropertyType); return(model.Root.QuerySingle(id, QueryLevel.None)); //通过属性读取外部根,我们都是无锁的查询方式 } return(null); }
public static int GetMaxLength(this PropertyRepositoryAttribute attr) { var stringProperty = attr.Property as GeneratedField.StringProperty; if (stringProperty != null) { return(stringProperty.MaxLength); } else { var sl = attr.Property.GetAttribute <StringLengthAttribute>(); return(sl == null ? 0 : sl.Max); } }
private object ReadAggregateRoot(PropertyRepositoryAttribute tip, DynamicData data, QueryLevel level) { var dataKey = _getIdName(tip.PropertyName); object id = null; if (data.TryGetValue(dataKey, out id)) { var model = DataModel.Create(tip.PropertyType); var queryLevel = GetQueryAggreateRootLevel(level); return(model.Root.QuerySingle(id, queryLevel)); } return(null); }
/// <summary> /// 获取集合类型的数据字段 /// </summary> /// <param name="propertyName"></param> /// <param name="propertyType"></param> /// <returns></returns> private bool TryGetListField(PropertyRepositoryAttribute attribute, bool isSnapshot, ref IDataField result) { var elementType = attribute.GetElementType(); if (DomainObject.IsValueObject(elementType)) { //值对象 var field = new ValueObjectListField(attribute); //当值对象A引用了值对象A时,会造成死循环 //var mapper = DataMapperFactory.Create(elementType); //var childs = mapper.GetObjectFields(elementType, isSnapshot); //field.AddChilds(childs); result = field; return(true); } else if (DomainObject.IsAggregateRoot(elementType)) { //引用了根对象 var field = new AggregateRootListField(attribute); result = field; return(true); } else if (DomainObject.IsEntityObject(elementType)) { //引用了内部实体对象 var field = new EntityObjectListField(attribute); //当成员对象A引用了成员对象A时,会造成死循环 //var mapper = DataMapperFactory.Create(elementType); //var childs = mapper.GetObjectFields(elementType, isSnapshot); //field.AddChilds(childs); result = field; return(true); } else if (elementType.IsList()) { throw new DomainDesignException(Strings.NestedCollection); } else { //值集合 var field = new ValueListField(attribute); result = field; return(true); } }
private object ReadMemberFromData(PropertyRepositoryAttribute tip, DynamicData data) { var name = _getNameWithSeparated(tip.PropertyName); using (var temp = SqlHelper.BorrowData()) { var subData = temp.Item; foreach (var p in data) { var dataName = p.Key; if (dataName.StartsWith(name)) { var subName = _getNextName(dataName); subData.Add(subName, p.Value); } } if (subData.IsEmpty()) { if (tip.DomainPropertyType == DomainPropertyType.AggregateRoot) { var idName = _getIdName(tip.PropertyName); var id = data.Get(idName); return(ReadSnapshot(tip, id)); } return(DomainObject.GetEmpty(tip.PropertyType)); } var typeKey = (string)subData.Get(GeneratedField.TypeKeyName); Type objectType = null; if (this.IsDynamic) { objectType = tip.PropertyType; } else { objectType = string.IsNullOrEmpty(typeKey) ? tip.PropertyType : DerivedClassAttribute.GetDerivedType(typeKey); } var child = GetRuntimeTable(this, tip.PropertyName, objectType); //先尝试中构造上下文中得到 return(child.GetObjectFromConstruct(subData) ?? child.CreateObject(objectType, subData, QueryLevel.None)); //成员始终是QueryLevel.None的方式加载 } }
private object ReadValueByLazy(PropertyRepositoryAttribute tip, DynamicData data) { object id = null; if (data.TryGetValue(EntityObject.IdPropertyName, out id)) { object rootId = null; var rootIdName = this.Type == DataTableType.AggregateRoot ? EntityObject.IdPropertyName : this.Root.TableIdName; if (data.TryGetValue(rootIdName, out rootId)) { return(QueryDataScalar(rootId, id, tip.PropertyName)); } } return(null); }
/// <summary> /// 读取对象集合的内部调用版本,此方法不是用于构造对象,而是为了查询用 /// </summary> /// <param name="tip"></param> /// <param name="data"></param> /// <param name="elementType"></param> /// <returns></returns> private void QueryMembers(PropertyRepositoryAttribute tip, DynamicData data, List <object> objs) { object rootId = null; var rootIdName = this.Type == DataTableType.AggregateRoot ? EntityObject.IdPropertyName : this.Root.TableIdName; if (data.TryGetValue(rootIdName, out rootId)) { //当前对象的编号,就是子对象的masterId object masterId = null; data.TryGetValue(EntityObject.IdPropertyName, out masterId); var child = GetChildTableByRuntime(this, tip); child.QueryOneToMore(rootId, masterId, objs); } }
public object ReadPropertyValue(object parent, PropertyRepositoryAttribute tip, ParameterRepositoryAttribute prmTip, DynamicData data) { //看对应的属性特性中是否定义了加载方法,优先执行自定义方法 object value = null; if (tip.TryLoadData(this.ObjectType, data, out value)) { return(value); } //自动加载 switch (tip.DomainPropertyType) { case DomainPropertyType.Primitive: { return(ReadPrimitive(tip, data)); } case DomainPropertyType.PrimitiveList: { return(ReadPrimitiveList(tip, data)); } case DomainPropertyType.AggregateRoot: { return(ReadAggregateRoot(tip, data)); } case DomainPropertyType.ValueObject: case DomainPropertyType.EntityObject: case DomainPropertyType.EntityObjectPro: { return(ReadMember(tip, data)); } case DomainPropertyType.EntityObjectList: case DomainPropertyType.EntityObjectProList: case DomainPropertyType.ValueObjectList: case DomainPropertyType.AggregateRootList: { return(ReadMembers(parent, tip, prmTip, data)); } } return(null); }
private object ReadPrimitiveList(DomainObject parent, PropertyRepositoryAttribute tip, ParameterRepositoryAttribute prmTip, DynamicData data, QueryLevel level) { object rootId = null; var rootIdName = this.Type == DataTableType.AggregateRoot ? EntityObject.IdPropertyName : GeneratedField.RootIdName; if (data.TryGetValue(rootIdName, out rootId)) { //当前对象的编号,就是子对象的masterId object masterId = null; data.TryGetValue(EntityObject.IdPropertyName, out masterId); var child = GetChildTableByRuntime(this, tip); return(child.ReadValues(tip, prmTip, parent, rootId, masterId, level)); } return(null); }
private object ReadMembers(object parent, PropertyRepositoryAttribute tip, ParameterRepositoryAttribute prmTip, DynamicData data) { object rootId = null; var rootIdName = this.Type == DataTableType.AggregateRoot ? EntityObject.IdPropertyName : this.Root.TableIdName; if (data.TryGetValue(rootIdName, out rootId)) { //当前对象的编号,就是子对象的masterId object masterId = null; data.TryGetValue(EntityObject.IdPropertyName, out masterId); var child = GetChildTableByRuntime(this, tip); return(child.ReadOneToMore(prmTip, parent, rootId, masterId)); } return(null); }
/// <summary> /// 获取集合类型的数据字段 /// </summary> /// <param name="propertyName"></param> /// <param name="propertyType"></param> /// <returns></returns> private static bool TryGetListField(PropertyRepositoryAttribute attribute, bool isSnapshot, ref IDataField result) { var elementType = attribute.PropertyType.ResolveElementType(); if (DomainObject.IsValueObject(elementType)) { //值对象 var field = new ValueObjectListField(attribute); var childs = GetObjectFields(elementType, isSnapshot); field.AddChilds(childs); result = field; return(true); } else if (DomainObject.IsAggregateRoot(elementType)) { //引用了根对象 var field = new AggregateRootListField(attribute); result = field; return(true); } else if (DomainObject.IsEntityObject(elementType)) { //引用了内部实体对象 var field = new EntityObjectListField(attribute); var childs = GetObjectFields(elementType, isSnapshot); field.AddChilds(childs); result = field; return(true); } else if (IsList(elementType)) { throw new DomainDesignException(Strings.NestedCollection); } else { //值集合 var field = new ValueListField(attribute); result = field; return(true); } }
private object ReadPrimitive(PropertyRepositoryAttribute tip, DynamicData data) { var value = tip.Lazy ? ReadValueByLazy(tip, data) : ReadValueFromData(tip, data); if (!tip.IsEmptyable) { return(value); } if (value == null) { //Emptyable类型的数据有可能存的是null值 return(tip.CreateDefaultEmptyable.Invoke(null, null)); } using (var temp = ArgsPool.Borrow1()) { var args = temp.Item; args[0] = value; return(tip.EmptyableConstructor.CreateInstance(args)); } }
private void InitObjectType(Type objectType, PropertyRepositoryAttribute tip) { this.ObjectType = objectType; if (this.Type == DataTableType.Middle) { this.ElementType = tip.GetElementType(); } else if (this.Type == DataTableType.AggregateRoot) { this.ObjectTip = ObjectRepositoryAttribute.GetTip(this.ObjectType, true); } else { //对于值对象和引用对象,如果没有定义ObjectRepositoryAttribute,那么使用根的ObjectRepositoryAttribute this.ObjectTip = ObjectRepositoryAttribute.GetTip(this.ObjectType, false); if (this.ObjectTip == null) { this.ObjectTip = this.Root.ObjectTip; } } }
/// <summary> /// 创建基础值集合的值字段 /// </summary> /// <param name="ownerType"></param> /// <param name="propertyType"></param> /// <param name="name"></param> /// <returns></returns> public static GeneratedField CreatePrimitiveValue(Type ownerType, ValueListField field) { var valueType = field.ValueType; DomainProperty property = null; if (valueType == typeof(string)) { property = new StringProperty(ownerType, PrimitiveValueName, field.Tip.GetMaxLength(), field.Tip.IsASCIIString()); } else { property = new CustomProperty(ownerType, valueType, PrimitiveValueName); } var attr = new PropertyRepositoryAttribute() { Property = property }; return(new GeneratedField(attr, PrimitiveValueName, GeneratedFieldType.PrimitiveValue, DbFieldType.NonclusteredIndex)); }
private object QueryMember(PropertyRepositoryAttribute tip, DynamicData data) { var child = GetChildTableByRuntime(this, tip); //通过基本表就可以实际查出数据,查数据会自动识别数据的真实类型的 object rootId = null; var rootIdName = this.Type == DataTableType.AggregateRoot ? EntityObject.IdPropertyName : this.Root.TableIdName; if (data.TryGetValue(rootIdName, out rootId)) { object id = null; var field = GetQuoteField(this, tip.PropertyName); if (data.TryGetValue(field.Name, out id)) { return(child.QuerySingle(rootId, id)); } } return(null); }