mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	- 增加 [Description] 元数据注释,优先级低于 c# 代码注释;
This commit is contained in:
		@@ -2,6 +2,7 @@
 | 
				
			|||||||
using FreeSql.DataAnnotations;
 | 
					using FreeSql.DataAnnotations;
 | 
				
			||||||
using Microsoft.AspNetCore.Mvc;
 | 
					using Microsoft.AspNetCore.Mvc;
 | 
				
			||||||
using Microsoft.Extensions.Logging;
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
 | 
					using System.ComponentModel;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace aspnetcore_transaction.Controllers
 | 
					namespace aspnetcore_transaction.Controllers
 | 
				
			||||||
@@ -45,6 +46,7 @@ namespace aspnetcore_transaction.Controllers
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        public SongService(BaseRepository<Song> repoSong, BaseRepository<Detail> repoDetail, SongRepository repoSong2)
 | 
					        public SongService(BaseRepository<Song> repoSong, BaseRepository<Detail> repoDetail, SongRepository repoSong2)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
					            var tb = repoSong.Orm.CodeFirst.GetTableByEntity(typeof(Song));
 | 
				
			||||||
            _repoSong = repoSong;
 | 
					            _repoSong = repoSong;
 | 
				
			||||||
            _repoDetail = repoDetail;
 | 
					            _repoDetail = repoDetail;
 | 
				
			||||||
            _repoSong2 = repoSong2;
 | 
					            _repoSong2 = repoSong2;
 | 
				
			||||||
@@ -81,9 +83,14 @@ namespace aspnetcore_transaction.Controllers
 | 
				
			|||||||
        public SongRepository(UnitOfWorkManager uowm) : base(uowm?.Orm, uowm) { }
 | 
					        public SongRepository(UnitOfWorkManager uowm) : base(uowm?.Orm, uowm) { }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    [Description("123")]
 | 
				
			||||||
    public class Song
 | 
					    public class Song
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// 自增
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
        [Column(IsIdentity = true)]
 | 
					        [Column(IsIdentity = true)]
 | 
				
			||||||
 | 
					        [Description("自增id")]
 | 
				
			||||||
        public int Id { get; set; }
 | 
					        public int Id { get; set; }
 | 
				
			||||||
        public string Title { get; set; }
 | 
					        public string Title { get; set; }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,6 +4,10 @@
 | 
				
			|||||||
    <TargetFramework>netcoreapp3.1</TargetFramework>
 | 
					    <TargetFramework>netcoreapp3.1</TargetFramework>
 | 
				
			||||||
  </PropertyGroup>
 | 
					  </PropertyGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
 | 
				
			||||||
 | 
					    <DocumentationFile>aspnetcore_transaction.xml</DocumentationFile>
 | 
				
			||||||
 | 
					  </PropertyGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  <ItemGroup>
 | 
					  <ItemGroup>
 | 
				
			||||||
    <PackageReference Include="FreeSql.DynamicProxy" Version="1.3.0" />
 | 
					    <PackageReference Include="FreeSql.DynamicProxy" Version="1.3.0" />
 | 
				
			||||||
    <PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0" />
 | 
					    <PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0" />
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										18
									
								
								Examples/aspnetcore_transaction/aspnetcore_transaction.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								Examples/aspnetcore_transaction/aspnetcore_transaction.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					<?xml version="1.0"?>
 | 
				
			||||||
 | 
					<doc>
 | 
				
			||||||
 | 
					    <assembly>
 | 
				
			||||||
 | 
					        <name>aspnetcore_transaction</name>
 | 
				
			||||||
 | 
					    </assembly>
 | 
				
			||||||
 | 
					    <members>
 | 
				
			||||||
 | 
					        <member name="P:aspnetcore_transaction.Controllers.Song.Id">
 | 
				
			||||||
 | 
					            <summary>
 | 
				
			||||||
 | 
					            自增
 | 
				
			||||||
 | 
					            </summary>
 | 
				
			||||||
 | 
					        </member>
 | 
				
			||||||
 | 
					        <member name="T:FreeSql.TransactionalAttribute">
 | 
				
			||||||
 | 
					            <summary>
 | 
				
			||||||
 | 
					            使用事务执行,请查看 Program.cs 代码开启动态代理
 | 
				
			||||||
 | 
					            </summary>
 | 
				
			||||||
 | 
					        </member>
 | 
				
			||||||
 | 
					    </members>
 | 
				
			||||||
 | 
					</doc>
 | 
				
			||||||
@@ -128,7 +128,7 @@ namespace FreeSql.Tests.Odbc.PostgreSQL
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            var dt1 = select.Limit(10).ToDataTable();
 | 
					            var dt1 = select.Limit(10).ToDataTable();
 | 
				
			||||||
            var dt2 = select.Limit(10).ToDataTable("id, 222");
 | 
					            var dt2 = select.Limit(10).ToDataTable("id, 222");
 | 
				
			||||||
            var dt3 = select.Limit(10).ToDataTable(a => new { a.Id, a.Type.Name, now = DateTime.Now });
 | 
					            var dt3 = select.Limit(10).ToDataTable(a => new { id = a.Id, name2 = a.Type.Name, now = DateTime.Now });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        class TestDto
 | 
					        class TestDto
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -128,7 +128,7 @@ namespace FreeSql.Tests.PostgreSQL
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            var dt1 = select.Limit(10).ToDataTable();
 | 
					            var dt1 = select.Limit(10).ToDataTable();
 | 
				
			||||||
            var dt2 = select.Limit(10).ToDataTable("id, 222");
 | 
					            var dt2 = select.Limit(10).ToDataTable("id, 222");
 | 
				
			||||||
            var dt3 = select.Limit(10).ToDataTable(a => new { a.Id, a.Type.Name, now = DateTime.Now });
 | 
					            var dt3 = select.Limit(10).ToDataTable(a => new { id = a.Id, name2 = a.Type.Name, now = DateTime.Now });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        class TestDto
 | 
					        class TestDto
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2901,6 +2901,13 @@
 | 
				
			|||||||
            AsType, Ctor, ClearData 三处地方需要重新加载
 | 
					            AsType, Ctor, ClearData 三处地方需要重新加载
 | 
				
			||||||
            </summary>
 | 
					            </summary>
 | 
				
			||||||
        </member>
 | 
					        </member>
 | 
				
			||||||
 | 
					        <member name="M:FreeSql.Internal.CommonUtils.GetPropertyCommentByDescriptionAttribute(System.Type)">
 | 
				
			||||||
 | 
					            <summary>
 | 
				
			||||||
 | 
					            动态读取 DescriptionAttribute 注释文本
 | 
				
			||||||
 | 
					            </summary>
 | 
				
			||||||
 | 
					            <param name="type"></param>
 | 
				
			||||||
 | 
					            <returns></returns>
 | 
				
			||||||
 | 
					        </member>
 | 
				
			||||||
        <member name="M:FreeSql.Internal.CommonUtils.GetProperyCommentBySummary(System.Type)">
 | 
					        <member name="M:FreeSql.Internal.CommonUtils.GetProperyCommentBySummary(System.Type)">
 | 
				
			||||||
            <summary>
 | 
					            <summary>
 | 
				
			||||||
            通过属性的注释文本,通过 xml 读取
 | 
					            通过属性的注释文本,通过 xml 读取
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -371,6 +371,48 @@ namespace FreeSql.Internal
 | 
				
			|||||||
            return iidx == 1 ? sb.Remove(0, 5).Remove(sb.Length - 1, 1).ToString() : sb.Remove(0, 4).ToString();
 | 
					            return iidx == 1 ? sb.Remove(0, 5).Remove(sb.Length - 1, 1).ToString() : sb.Remove(0, 4).ToString();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// 动态读取 DescriptionAttribute 注释文本
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="type"></param>
 | 
				
			||||||
 | 
					        /// <returns></returns>
 | 
				
			||||||
 | 
					        public static Dictionary<string, string> GetPropertyCommentByDescriptionAttribute(Type type)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var dic = new Dictionary<string, string>();
 | 
				
			||||||
 | 
					            GetDydesc(null); //class注释
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var props = type.GetPropertiesDictIgnoreCase().Values;
 | 
				
			||||||
 | 
					            foreach (var prop in props)
 | 
				
			||||||
 | 
					                GetDydesc(prop);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return dic;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            void GetDydesc(PropertyInfo prop)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                object[] attrs = null;
 | 
				
			||||||
 | 
					                try
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    attrs = prop == null ? 
 | 
				
			||||||
 | 
					                        type.GetCustomAttributes(false).ToArray() : 
 | 
				
			||||||
 | 
					                        prop.GetCustomAttributes(false).ToArray(); //.net core 反射存在版本冲突问题,导致该方法异常
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                catch { }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                var dyattr = attrs?.Where(a => {
 | 
				
			||||||
 | 
					                    return ((a as Attribute)?.TypeId as Type)?.Name == "DescriptionAttribute";
 | 
				
			||||||
 | 
					                }).FirstOrDefault();
 | 
				
			||||||
 | 
					                if (dyattr != null)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    var valueProp = dyattr.GetType().GetProperties().Where(a => a.PropertyType == typeof(string)).FirstOrDefault();
 | 
				
			||||||
 | 
					                    var comment = valueProp?.GetValue(dyattr, null)?.ToString();
 | 
				
			||||||
 | 
					                    if (string.IsNullOrEmpty(comment) == false)
 | 
				
			||||||
 | 
					                        dic.Add(prop == null ? 
 | 
				
			||||||
 | 
					                            "" : 
 | 
				
			||||||
 | 
					                            prop.Name, comment);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 通过属性的注释文本,通过 xml 读取
 | 
					        /// 通过属性的注释文本,通过 xml 读取
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
@@ -378,6 +420,8 @@ namespace FreeSql.Internal
 | 
				
			|||||||
        /// <returns>Dict:key=属性名,value=注释</returns>
 | 
					        /// <returns>Dict:key=属性名,value=注释</returns>
 | 
				
			||||||
        public static Dictionary<string, string> GetProperyCommentBySummary(Type type)
 | 
					        public static Dictionary<string, string> GetProperyCommentBySummary(Type type)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
					            if (type.Assembly.IsDynamic) return null;
 | 
				
			||||||
 | 
					            //动态生成的程序集,访问不了 Assembly.Location/Assembly.CodeBase
 | 
				
			||||||
            var regex = new Regex(@"\.(dll|exe)", RegexOptions.IgnoreCase);
 | 
					            var regex = new Regex(@"\.(dll|exe)", RegexOptions.IgnoreCase);
 | 
				
			||||||
            var xmlPath = regex.Replace(type.Assembly.Location, ".xml");
 | 
					            var xmlPath = regex.Replace(type.Assembly.Location, ".xml");
 | 
				
			||||||
            if (File.Exists(xmlPath) == false)
 | 
					            if (File.Exists(xmlPath) == false)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -69,7 +69,10 @@ namespace FreeSql.Internal
 | 
				
			|||||||
            var propsLazy = new List<NaviteTuple<PropertyInfo, bool, bool, MethodInfo, MethodInfo>>();
 | 
					            var propsLazy = new List<NaviteTuple<PropertyInfo, bool, bool, MethodInfo, MethodInfo>>();
 | 
				
			||||||
            var propsNavObjs = new List<PropertyInfo>();
 | 
					            var propsNavObjs = new List<PropertyInfo>();
 | 
				
			||||||
            var propsComment = CommonUtils.GetProperyCommentBySummary(entity);
 | 
					            var propsComment = CommonUtils.GetProperyCommentBySummary(entity);
 | 
				
			||||||
 | 
					            var propsCommentByDescAttr = CommonUtils.GetPropertyCommentByDescriptionAttribute(entity);
 | 
				
			||||||
            trytb.Comment = propsComment != null && propsComment.TryGetValue("", out var tbcomment) ? tbcomment : "";
 | 
					            trytb.Comment = propsComment != null && propsComment.TryGetValue("", out var tbcomment) ? tbcomment : "";
 | 
				
			||||||
 | 
					            if (string.IsNullOrEmpty(trytb.Comment) && propsCommentByDescAttr != null && propsCommentByDescAttr.TryGetValue("", out tbcomment)) trytb.Comment = tbcomment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var columnsList = new List<ColumnInfo>();
 | 
					            var columnsList = new List<ColumnInfo>();
 | 
				
			||||||
            foreach (var p in trytb.Properties.Values)
 | 
					            foreach (var p in trytb.Properties.Values)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
@@ -148,6 +151,9 @@ namespace FreeSql.Internal
 | 
				
			|||||||
                };
 | 
					                };
 | 
				
			||||||
                if (propsComment != null && propsComment.TryGetValue(p.Name, out var trycomment))
 | 
					                if (propsComment != null && propsComment.TryGetValue(p.Name, out var trycomment))
 | 
				
			||||||
                    col.Comment = trycomment;
 | 
					                    col.Comment = trycomment;
 | 
				
			||||||
 | 
					                if (string.IsNullOrEmpty(col.Comment) && propsCommentByDescAttr != null && propsCommentByDescAttr.TryGetValue(p.Name, out trycomment))
 | 
				
			||||||
 | 
					                    col.Comment = trycomment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (colattr.IsIgnore)
 | 
					                if (colattr.IsIgnore)
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    trytb.ColumnsByCsIgnore.Add(p.Name, col);
 | 
					                    trytb.ColumnsByCsIgnore.Add(p.Name, col);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user