private static readonly string PARAM_CURRENT_VALUE = "CurrentValue"; //当前值 //private static readonly object IDENTITY_CREATE_SYN_LOCK = new object(); /// <summary> /// 设置实体的键值。 /// </summary> /// <param name="entity"></param> public void FillEntityIdentity(object entity) { Type entityType = entity.GetType(); ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType); if (string.IsNullOrEmpty(mappingInfo.MapTable)) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定映射到的表。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } if (mappingInfo.PrimaryKeys == null || mappingInfo.PrimaryKeys.Count != 1) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定主键或者不是单一主键配置。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } FieldPropertyInfo keyInfo = mappingInfo.PrimaryKeys.Values.First <FieldPropertyInfo>(); //判断是否存在主键 bool exists = MB.Util.MyReflection.Instance.CheckObjectExistsProperty(entity, keyInfo.FieldName); if (!exists) { return; } object oldKey = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, keyInfo.FieldName); //判断是否已经存在键值。 if (oldKey != null && (int)oldKey > 0) { return; } int id = GetEntityIdentity(mappingInfo.MapTable); MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, keyInfo.FieldName, id); }
/// <summary> /// Generates an interface for each template that the current template derives from, recursively. /// </summary> private TemplateInfo GenerateInheritedInterfaces(ITemplateInfo template, TemplateGenerationMetadata templateData) { var existingInterface = templateData.GetInterface(template.TemplateId); if (existingInterface != null) { return(existingInterface); } var interfaceInfo = templateData.AddInterface(template, _parameters.InterfaceSuffix); var fieldKeys = GetBaseFieldSet(); fieldKeys.Add(interfaceInfo.TypeName); // member names cannot be the same as their enclosing type fieldKeys.Add(template.Name.AsIdentifier()); // prevent any fields from being generated that would conflict with a concrete item name as well as the interface name // create interface properties foreach (var field in template.OwnFields) { if (_templateInputProvider.IsFieldIncluded(field.Id)) { string propertyName = field.Name.AsNovelIdentifier(fieldKeys); var fieldInfo = new FieldPropertyInfo(field); fieldInfo.FieldPropertyName = propertyName; fieldInfo.SearchFieldName = _indexFieldNameTranslator.GetIndexFieldName(field.Name); fieldInfo.FieldType = _fieldMappingProvider.GetFieldType(field); if (fieldInfo.FieldType == null) { Log.Warn("Synthesis: Field type resolution for " + field.Template.Name + "::" + field.Name + " failed; no mapping found for field type " + field.Type, this); continue; // skip adding the field for generation } // record usage of the property name fieldKeys.Add(propertyName); // add the field to the metadata interfaceInfo.FieldsToGenerate.Add(fieldInfo); } } // add base interface inheritance foreach (var baseTemplate in template.BaseTemplates) { if (baseTemplate.Name.ToUpperInvariant() != StandardTemplate) { // recursively generate base templates' interfaces as needed var baseInterface = GenerateInheritedInterfaces(baseTemplate, templateData); if (baseInterface != null) { interfaceInfo.InterfacesImplemented.Add(baseInterface); // assign interface implementation } } } return(interfaceInfo); }
/// <summary> /// 获取执行更新实体最后修改时间的DbCommand。 /// </summary> /// <param name="db"></param> /// <param name="entity"></param> /// <returns></returns> public DbCommand GetSaveLastModifiedDateCommand(Database db, object entity) { bool exists = MB.Util.MyReflection.Instance.CheckObjectExistsProperty(entity, LAST_MODIFIED_DATE_PROPERTY); if (!exists) { return(null); } Type entityType = entity.GetType(); ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType); if (string.IsNullOrEmpty(mappingInfo.MapTable)) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定映射到的表。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } if (mappingInfo.PrimaryKeys == null || mappingInfo.PrimaryKeys.Count != 1) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定主键或者不是单一主键配置。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } FieldPropertyInfo keyInfo = mappingInfo.PrimaryKeys.Values.First <FieldPropertyInfo>(); var dbType = MB.Orm.Persistence.DatabaseHelper.GetDatabaseType(db); string sysDate = string.Empty; switch (dbType) { case MB.Orm.Enums.DatabaseType.Oracle: sysDate = "SysDate"; break; case MB.Orm.Enums.DatabaseType.Sqlite: sysDate = "datetime(CURRENT_TIMESTAMP,'localtime')"; break; case MB.Orm.Enums.DatabaseType.MSSQLServer: sysDate = "GetDate()"; break; case Enums.DatabaseType.MySql: sysDate = "Now()"; break; default: throw new MB.Util.APPException(string.Format("在获取数据库系统时间时,数据库类型{0} 还没有进行相应的处理", dbType.ToString()), Util.APPMessageType.SysErrInfo); } object key = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, keyInfo.PropertyName); string sqlStr = string.Format("UPDATE {0} SET {1}= {2} WHERE {3}='{4}'", mappingInfo.MapTable, LAST_MODIFIED_DATE_PROPERTY, sysDate, keyInfo.FieldName, key.ToString()); return(db.GetSqlStringCommand(sqlStr)); }
private void CreateInterfaceProperty(FieldPropertyInfo propertyInfo, CodeTypeMemberCollection members) { Assert.ArgumentNotNull(propertyInfo, "propertyInfo"); var property = new CodeMemberProperty { Type = new CodeTypeReference(propertyInfo.FieldType.PublicFieldType), Name = propertyInfo.FieldPropertyName, HasGet = true }; // add [IndexField] attribute property.CustomAttributes.Add(GetIndexFieldAttribute(propertyInfo.SearchFieldName)); AddCommentsToFieldProperty(property, propertyInfo.Field); members.Add(property); }
private void CreateItemProperty(FieldPropertyInfo propertyInfo, CodeTypeMemberCollection members) { Assert.ArgumentNotNull(propertyInfo, "propertyInfo"); var backingFieldName = "_" + propertyInfo.FieldPropertyName[0].ToString(CultureInfo.InvariantCulture).ToLower() + propertyInfo.FieldPropertyName.Substring(1); var backingField = new CodeMemberField(new CodeTypeReference(propertyInfo.FieldType.InternalFieldType), backingFieldName); backingField.Attributes = MemberAttributes.Private; var property = new CodeMemberProperty { // ReSharper disable BitwiseOperatorOnEnumWithoutFlags Attributes = MemberAttributes.Public | MemberAttributes.Final, // ReSharper restore BitwiseOperatorOnEnumWithoutFlags Type = new CodeTypeReference(propertyInfo.FieldType.PublicFieldType), Name = propertyInfo.FieldPropertyName, HasGet = true }; // add [IndexField] attribute property.CustomAttributes.Add(GetIndexFieldAttribute(propertyInfo.SearchFieldName)); // if(backingField == null) // backingField = new SynthesisFieldType(new Lazy<Field>(() => InnerItem.Fields["xxx"], GetSearchFieldValue("index-field-name")); var initializerLambda = new CodeSnippetExpression(string.Format("new global::Synthesis.FieldTypes.LazyField(() => InnerItem.Fields[\"{0}\"], \"{1}\", \"{2}\")", propertyInfo.Field.Id, propertyInfo.Field.Template.FullPath, propertyInfo.Field.Name)); var initializerSearchReference = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "GetSearchFieldValue", new CodePrimitiveExpression(propertyInfo.SearchFieldName)); var backingFieldNullCheck = new CodeConditionStatement(); backingFieldNullCheck.Condition = new CodeSnippetExpression(string.Format("{0} == null", backingFieldName)); backingFieldNullCheck.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(backingFieldName), new CodeObjectCreateExpression(propertyInfo.FieldType.InternalFieldType, initializerLambda, initializerSearchReference))); property.GetStatements.Add(backingFieldNullCheck); // return backingField; property.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(backingFieldName))); AddCommentsToFieldProperty(property, propertyInfo.Field); members.Add(backingField); members.Add(property); }
/// <summary> /// 获取数据实体的键值。 /// </summary> /// <param name="entity"></param> /// <returns></returns> public object GetEntityKeyValue(object entity, out string keyPropertyName) { Type entityType = entity.GetType(); ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType); if (string.IsNullOrEmpty(mappingInfo.MapTable)) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定映射到的表。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } if (mappingInfo.PrimaryKeys == null || mappingInfo.PrimaryKeys.Count != 1) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定主键或者不是单一主键配置。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } FieldPropertyInfo keyInfo = mappingInfo.PrimaryKeys.Values.First <FieldPropertyInfo>(); object key = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, keyInfo.PropertyName); keyPropertyName = keyInfo.PropertyName; return(key); }
//获取数据库中当前实体的最后修改时间。 private DateTime getEntityLastModifiedDate(object entity) { Type entityType = entity.GetType(); ModelMappingInfo mappingInfo = AttMappingManager.Instance.GetModelMappingInfo(entityType); if (string.IsNullOrEmpty(mappingInfo.MapTable)) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定映射到的表。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } if (mappingInfo.PrimaryKeys == null || mappingInfo.PrimaryKeys.Count != 1) { throw new MB.Util.APPException(string.Format("数据实体{0} 配置有误,没有指定主键或者不是单一主键配置。", entityType.FullName), MB.Util.APPMessageType.SysErrInfo); } FieldPropertyInfo keyInfo = mappingInfo.PrimaryKeys.Values.First <FieldPropertyInfo>(); object key = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, keyInfo.PropertyName); if (key == null) { return(DateTime.MinValue); } string sqlStr = string.Format("SELECT {0} FROM {1} WHERE {2}='{3}'", LAST_MODIFIED_DATE_PROPERTY, mappingInfo.MapTable, keyInfo.FieldName, key.ToString()); Database db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase(); int lastID = 1; DbCommand cmdSelect = db.GetSqlStringCommand(sqlStr); object val = db.ExecuteScalar(cmdSelect); if (val == null || val == System.DBNull.Value) { return(DateTime.MinValue); } cmdSelect.Dispose(); return(System.Convert.ToDateTime(val)); }
public static IPropertyInfo[] BuildPropertyPath(Type entityType, String memberName, IPropertyInfoProvider propertyInfoProvider) { String[] memberPath = EmbeddedMember.Split(memberName); Type currType = entityType; IPropertyInfo[] propertyPath = new IPropertyInfo[memberPath.Length]; for (int a = 0, size = propertyPath.Length; a < size; a++) { IPropertyInfo property = propertyInfoProvider.GetProperty(currType, memberPath[a]); if (property == null) { FieldInfo[] fields = ReflectUtil.GetDeclaredFieldInHierarchy(currType, memberPath[a]); if (fields.Length == 0) { throw new Exception("Path illegal: " + memberName); } property = new FieldPropertyInfo(currType, memberPath[a], fields[a]); } propertyPath[a] = property; currType = property.PropertyType; } return(propertyPath); }
public void ExportTo <T>(ref T obj) where T : class { bool flag = obj == null; if (!flag) { if (!obj.IsObject()) { bool flag3 = this.ObjectType == JsonItemType.OBJ_ARRAY || this.ObjectType == JsonItemType.OBJ_OBJECT; if (flag3) { object tempobj = null; if (this.ObjectType == JsonItemType.OBJ_ARRAY) { bool flag5 = obj.IsArray(); if (flag5) { bool flag6 = obj is Array; if (flag6) { Array array = obj as Array; bool flag7 = array.Length < this.SubItems.Count; if (flag7) { JsonItem.Resize(ref array, this.SubItems.Count); } obj = (array as T); } } IDictionary idict; bool flag8 = (idict = (obj as IDictionary)) != null; if (flag8) { object[] nobj = new object[this.SubItems.Count]; idict.Add(this.Name, nobj); tempobj = nobj; } else { IDictionary <string, object> ndict; if ((ndict = (obj as IDictionary <string, object>)) != null) { Dictionary <string, object> nobj2 = new Dictionary <string, object>(); ndict.Add(this.Name, nobj2); tempobj = nobj2; } } } else { if (obj is Array) { IList arr = obj as IList; Dictionary <string, object> nobj3 = new Dictionary <string, object>(); tempobj = nobj3; arr[this.Index] = ReflectUtil.MatchType(nobj3, obj.GetType().GetElementType()); } bool flag11 = this.Parent != null; if (flag11) { IDictionary idict2; if ((idict2 = (obj as IDictionary)) != null) { Type otypee = obj.GetType(); Type gentype = otypee.GetGenericArguments().ElementAt(1); bool flag13 = gentype != typeof(object); if (flag13) { return; } Type listType = typeof(Dictionary <,>); Type constructedListType = listType.MakeGenericType(otypee.GetGenericArguments()); object nobj4 = Activator.CreateInstance(constructedListType); idict2.Add(this.Name, nobj4); tempobj = nobj4; } else { IDictionary <string, object> ndict2; bool flag14 = (ndict2 = (obj as IDictionary <string, object>)) != null; if (flag14) { Dictionary <string, object> nobj5 = new Dictionary <string, object>(); ndict2.Add(this.Name, nobj5); tempobj = nobj5; } } } } for (int i = 0; i < this.SubItems.Count; i++) { JsonItem item = this.SubItems[i]; bool flag15 = tempobj != null; if (flag15) { item.ExportTo <object>(ref tempobj); } else { item.ExportTo <T>(ref obj); } } } else { bool flag16 = this.ObjectType == JsonItemType.OBJ_ARRAYITEM; if (flag16) { IList list; bool flag17 = (list = (obj as IList)) != null; if (flag17) { Type ntype = list.GetType(); bool isGenericType = ntype.IsGenericType; Type ltype; if (isGenericType) { ltype = ntype.GetGenericArguments().FirstOrDefault <Type>(); } else { ltype = list.GetType().GetElementType(); } bool isFixedSize = list.IsFixedSize; if (isFixedSize) { list[this.Index] = ReflectUtil.MatchType(this.Value, ltype); } else { list.Add(ReflectUtil.MatchType(this.Value, ltype)); } } } else { if (this.ObjectType == JsonItemType.OBJ_VARIANT) { T t = obj; Type otype = t?.GetType(); IDictionary idict3; bool flag19 = (idict3 = (obj as IDictionary)) != null; if (flag19) { Type gentype2 = otype.GetGenericArguments().ElementAt(1); idict3.Add(this.Name, ReflectUtil.MatchType(this.Value, gentype2)); } else { IDictionary <string, object> sdict; if ((sdict = (obj as IDictionary <string, object>)) != null) { sdict.Add(this.Name, this.Value); } } } } } } else { bool flag21 = this.ObjectType != JsonItemType.OBJ_OBJECT; if (!flag21) { Type otype2 = obj.GetType(); MemberInfo[] members = otype2.GetMembers(BindingFlags.Instance | BindingFlags.Public); foreach (MemberInfo member in members) { bool flag22 = member.MemberType != MemberTypes.Field && member.MemberType != MemberTypes.Property; if (!flag22) { string name = member.Name; JsonAttribute jattribut; if ((jattribut = (member.GetCustomAttribute(typeof(JsonAttribute)) as JsonAttribute)) != null) { bool notMapped = jattribut.NotMapped; if (notMapped) { goto IL_7E6; } bool flag24 = !string.IsNullOrEmpty(jattribut.TagName); if (flag24) { name = jattribut.TagName; } } JsonItem item2 = this.Find(name); bool flag25 = item2 == null; if (!flag25) { FieldPropertyInfo fprop = new FieldPropertyInfo(member); bool flag26 = !fprop.CanWrite || !fprop.CanRead || fprop.HasIndexParameters(); if (!flag26) { if (item2.ObjectType == JsonItemType.OBJ_OBJECT) { bool flag28 = !fprop.ObjectType.IsObject() && !fprop.ObjectType.IsDictionary(); if (!flag28) { object curvalue = fprop.GetValue(obj); if (curvalue == null) { if (fprop.ObjectType.IsDictionary()) { Type listType2 = typeof(Dictionary <,>); Type constructedListType2 = listType2.MakeGenericType(fprop.ObjectType.GetGenericArguments()); curvalue = Activator.CreateInstance(constructedListType2); for (int j = 0; j < item2.SubItems.Count; j++) { item2.SubItems[j].ExportTo <object>(ref curvalue); } fprop.SetValue(obj, curvalue); break; } curvalue = Activator.CreateInstance(fprop.ObjectType); } item2.ExportTo <object>(ref curvalue); fprop.SetValue(obj, curvalue); } } else { if (item2.ObjectType == JsonItemType.OBJ_ARRAY) { bool flag32 = !fprop.ObjectType.IsArray(); if (!flag32) { object curvalue2 = fprop.GetValue(obj); if (curvalue2 == null) { Type listType3 = typeof(List <>); bool isGenericType2 = fprop.ObjectType.IsGenericType; if (isGenericType2) { Type constructedListType3 = listType3.MakeGenericType(fprop.ObjectType.GetGenericArguments()); curvalue2 = Activator.CreateInstance(constructedListType3); } else { Type elemtype = fprop.ObjectType.GetElementType(); if (elemtype != null) { curvalue2 = Array.CreateInstance(elemtype, item2.SubItems.Count); } } } item2.ExportTo <object>(ref curvalue2); fprop.SetValue(obj, curvalue2); } } else { if (!fprop.ObjectType.IsObject() && !member.MemberType.IsArray()) { fprop.SetValue(obj, ReflectUtil.MatchType(item2.Value, fprop.ObjectType)); } } } } } } IL_7E6 :; } } } } }
public TemplateGenerationMetadata GenerateMetadata() { var timer = new Stopwatch(); timer.Start(); // load the templates we'll be generating into a state storage collection var templateData = CreateTemplateData(); foreach (var template in templateData.Templates) { HashSet <string> fieldKeys = GetBaseFieldSet(); // get fields on base type fieldKeys.Add(template.TypeName); // member names cannot be the same as their enclosing type so we add the type name to the fields collection foreach (var baseTemplate in template.Template.AllNonstandardBaseTemplates) // similarly names can't be the same as any of their base templates' names (this would cause an incompletely implemented interface) { if (templateData.Contains(baseTemplate.TemplateId)) { fieldKeys.Add(templateData[baseTemplate.TemplateId].TypeName); } else { fieldKeys.Add(baseTemplate.Name.AsIdentifier()); // NOTE: you could break this if you have a base template called Foo and a field called Foo that IS NOT on the Foo template (but why would you have that?) } } // generate item properties foreach (var field in template.Template.Fields) { if (_templateInputProvider.IsFieldIncluded(field.Id)) // query the template input provider and make sure the field is included { string propertyName = field.Name.AsNovelIdentifier(fieldKeys); var fieldInfo = new FieldPropertyInfo(field); fieldInfo.FieldPropertyName = propertyName; if (_parameters.EnableContentSearch) { fieldInfo.SearchFieldName = _indexFieldNameMapper.MapToSearchField(field); } fieldInfo.FieldType = _fieldMappingProvider.GetFieldType(field); if (fieldInfo.FieldType == null) { Log.Warn("Synthesis: Field type resolution for " + field.Template.Name + "::" + field.Name + " failed; no mapping found for field type " + field.Type, this); continue; // skip adding the field for generation } // record usage of the property name fieldKeys.Add(propertyName); // add the field to the metadata template.FieldsToGenerate.Add(fieldInfo); } } // generates interfaces to represent the Sitecore template inheritance hierarchy TemplateInfo baseInterface = GenerateInheritedInterfaces(template.Template, templateData); if (baseInterface != null) { template.InterfacesImplemented.Add(baseInterface); } } timer.Stop(); Log.Info($"Synthesis: Generated metadata for {templateData.Templates.Count} concrete templates and {templateData.Interfaces.Count} interface templates in {timer.ElapsedMilliseconds} ms", this); return(templateData); }