Dapper 复杂映射 Dapper.Extensions Dapper.FluentMap
Dapper complex mapping Dapper.Extensions Dapper.FluentMap
我的代码有一点问题,我需要精巧的代码才能很好地使用它。
当我说我的代码是继承的,所以这不是我的设计。
我正在尝试替换 Entity Framework,因为对数据库的调用效率不高,所以我想更好地控制生成的 SQL,因此 Dapper 似乎是显而易见的选择。
我遇到的问题是我正在努力映射 poco 类 我必须使用小巧的多查询。
我遇到的问题如下:
public Feed GetFeedDapper(int feedId)
{
Feed feed = null;
var sql =
@"
SELECT * FROM Feeds WHERE FeedId= @FeedId
SELECT * FROM FeedFilterParameters WHERE FeedId = @FeedId
SELECT * FROM TeamFeeds WHERE FeedId = @FeedId";
using (var multi = DbConnection.QueryMultiple(sql, new { FeedId = feedId }))
{
feed = multi.Read<Feed>().Single();
feed.Parameters = multi.Read<FeedFilterParameter>().ToList();
feed.TeamFeeds = multi.Read<TeamFeed>().ToList();
}
return feed;
}
这让我从数据库中得到了正确的结果,这很好,问题是并非 Feed 对象上的所有属性都被映射。提要有一个名为 InboundProperties 的 属性 类型,如下所示,在数据库中它们存储为 InboundProperties_{PropName}。这些属性没有被映射,我在 DapperExtensions 或 FluentMap 中尝试的任何东西都不起作用。
public class InboundProperties
{
public string ExternalRef { get; set; }
public string ExternalRefPrevious { get; set; }
public string ExternalId { get; set; }
public string ExternalName { get; set; }
public string ExternalToken { get; set; }
public int ExternalAPICounts { get; set; }
public string ExternalLink { get; set; }
public string ExternalPicture { get; set; }
public string LastProcessedMessageId { get; set; }
public long? LastProcessedMessageTime { get; set; }
public DateTime? MessageCountStartDT { get; set; }
public Int32 TenancyId { get; set; }
public virtual int FeedScopeInt { get; set; }
}
谁能帮我映射这些属性?
你试过这样的事情吗:
Feed feed = null;
var sql =
@"
SELECT * FROM Feeds WHERE FeedId= @FeedId
SELECT InboundProperties_ExternalRef as ExternalRef, InboundProperties_ExternalRefPrevious as ExternalRefPrevious FROM Feeds as InboundProperties WHERE FeedId= @FeedId
SELECT * FROM FeedFilterParameters WHERE FeedId = @FeedId
SELECT * FROM TeamFeeds WHERE FeedId = @FeedId";
using (var multi = DbConnection.QueryMultiple(sql, new { FeedId = feedId }))
{
feed = multi.Read<Feed>().Single();
feed.InboundProperties = multi.Read<InboundProperties>().Single();
feed.Parameters = multi.Read<FeedFilterParameter>().ToList();
feed.TeamFeeds = multi.Read<TeamFeed>().ToList();
}
return feed;
我只映射了前 2 个属性,但如果它有效,您就会知道如何映射所有属性。
这就是我解决它的方法,但接受了#rraszewski 的回答,因为它也有效,但这意味着我需要以非常手动的方式处理所有选择。
public Feed GetFeedDapper(int feedId)
{
Feed feed = null;
var multiPredicate = new GetMultiplePredicate();
multiPredicate.Add<Feed>(Predicates.Field<Feed>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<InboundProperties>(Predicates.Field<InboundProperties>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<OutboundProperties>(Predicates.Field<OutboundProperties>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<FeedFilterParameter>(Predicates.Field<FeedFilterParameter>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<TeamFeed>(Predicates.Field<TeamFeed>(x => x.FeedId, Operator.Eq, feedId));
var result = DbConnection.GetMultiple(multiPredicate);
feed = result.Read<Feed>().Single();
feed.InboundProperties = result.Read<InboundProperties>().Single();
feed.OutboundProperties = result.Read<OutboundProperties>().Single();
feed.Parameters = result.Read<FeedFilterParameter>().ToList();
feed.TeamFeeds = result.Read<TeamFeed>().ToList();
return feed;
}
然后我映射 类:
public class FeedMapper : ClassMapper<Feed>
{
public FeedMapper()
{
base.Table("Feeds");
Map(f => f.FeedId).Key(KeyType.Identity);
Map(f => f.Owner).Ignore();
Map(f => f.TeamFeeds).Ignore();
Map(f => f.FeedDirection).Ignore();
Map(f => f.InboundProperties).Ignore();
Map(f => f.Parameters).Ignore();
Map(f => f.OutboundProperties).Ignore();
Map(f => f.RelatedTeams).Ignore();
AutoMap();
}
}
public class InboundPropertiesMapper : ClassMapper<InboundProperties>
{
public InboundPropertiesMapper()
{
base.Table("Feeds");
Map(f => f.FeedId).Key(KeyType.Identity);
Map(f => f.Channel).Ignore();
Map(f => f.FeedScope).Ignore();
Map(f => f.ChannelInt).Column("InboundProperties_ChannelInt");
Map(f => f.ExternalAPICounts).Column("InboundProperties_ExternalAPICounts");
Map(f => f.ExternalId).Column("InboundProperties_ExternalId");
Map(f => f.ExternalLink).Column("InboundProperties_ExternalLink");
Map(f => f.ExternalName).Column("InboundProperties_ExternalName");
Map(f => f.ExternalPicture).Column("InboundProperties_ExternalPicture");
Map(f => f.ExternalRef).Column("InboundProperties_ExternalRef");
Map(f => f.ExternalRefPrevious).Column("InboundProperties_ExternalRefPrevious");
Map(f => f.ExternalToken).Column("InboundProperties_ExternalToken");
Map(f => f.FeedScopeInt).Column("InboundProperties_FeedScopeInt");
Map(f => f.LastProcessedMessageId).Column("InboundProperties_LastProcessedMessageId");
Map(f => f.LastProcessedMessageTime).Column("InboundProperties_LastProcessedMessageTime");
Map(f => f.MessageCountStartDT).Column("InboundProperties_MessageCountStartDT");
Map(f => f.TenancyId).Column("InboundProperties_TenancyId");
AutoMap();
}
}
public class OutboundPropertiesMapper : ClassMapper<OutboundProperties>
{
public OutboundPropertiesMapper()
{
base.Table("Feeds");
Map(f => f.FeedId).Key(KeyType.Identity);
Map(f => f.Channel).Ignore();
Map(f => f.FeedType).Ignore();
Map(f => f.OutboundFeedTypeInt).Column("OutboundProperties_OutboundFeedTypeInt");
Map(f => f.TenancyId).Column("OutboundProperties_TenancyId");
AutoMap();
}
}
我对这里提供的解决方案不满意(太容易出错),所以我决定创建一个扩展方法来使用反射映射对象。
public static class ConnectionExtensions
{
public static IEnumerable<T> QueryIncludeNestedObjects<T>(this SqlConnection connection, string sql)
{
var queryResults = connection.Query<dynamic>(sql);
var typeOfTMain = typeof(T);
foreach(var row in queryResults)
{
var mappedObject = Activator.CreateInstance<T>();
foreach (var col in row)
{
var colKey = (string)col.Key;
var colValue = (object)col.Value;
if(colKey.Contains("_"))
{
var subObjNameAndProp = colKey.Split('_');
var subProperty = typeOfTMain.GetProperty(subObjNameAndProp[0]);
if (subProperty == null) continue;
var subObj = subProperty.GetValue(mappedObject);
if(subObj == null)
{
subObj = Activator.CreateInstance(subProperty.PropertyType);
typeOfTMain.GetProperty(subObjNameAndProp[0]).SetValue(mappedObject, subObj);
}
subObj.GetType().GetProperty(subObjNameAndProp[1])
.SetValue(subObj, colValue);
}
else
typeOfTMain.GetProperty(colKey)?.SetValue(mappedObject, colValue);
}
yield return mappedObject;
}
}
}
那你这样用:
dbConnection.QueryIncludeNestedObjects<Feed>("SELECT * FROM Feeds");
如果你想像在这个问题中那样在 QueryMultiple 中使用它,只需像这样修改方法:
public static IEnumerable<T> ReadIncludeNestedObjects<T>(this GridReader gridReader)
{
var queryResults = gridReader.Read<dynamic>();
...
我的代码有一点问题,我需要精巧的代码才能很好地使用它。
当我说我的代码是继承的,所以这不是我的设计。
我正在尝试替换 Entity Framework,因为对数据库的调用效率不高,所以我想更好地控制生成的 SQL,因此 Dapper 似乎是显而易见的选择。
我遇到的问题是我正在努力映射 poco 类 我必须使用小巧的多查询。
我遇到的问题如下:
public Feed GetFeedDapper(int feedId)
{
Feed feed = null;
var sql =
@"
SELECT * FROM Feeds WHERE FeedId= @FeedId
SELECT * FROM FeedFilterParameters WHERE FeedId = @FeedId
SELECT * FROM TeamFeeds WHERE FeedId = @FeedId";
using (var multi = DbConnection.QueryMultiple(sql, new { FeedId = feedId }))
{
feed = multi.Read<Feed>().Single();
feed.Parameters = multi.Read<FeedFilterParameter>().ToList();
feed.TeamFeeds = multi.Read<TeamFeed>().ToList();
}
return feed;
}
这让我从数据库中得到了正确的结果,这很好,问题是并非 Feed 对象上的所有属性都被映射。提要有一个名为 InboundProperties 的 属性 类型,如下所示,在数据库中它们存储为 InboundProperties_{PropName}。这些属性没有被映射,我在 DapperExtensions 或 FluentMap 中尝试的任何东西都不起作用。
public class InboundProperties
{
public string ExternalRef { get; set; }
public string ExternalRefPrevious { get; set; }
public string ExternalId { get; set; }
public string ExternalName { get; set; }
public string ExternalToken { get; set; }
public int ExternalAPICounts { get; set; }
public string ExternalLink { get; set; }
public string ExternalPicture { get; set; }
public string LastProcessedMessageId { get; set; }
public long? LastProcessedMessageTime { get; set; }
public DateTime? MessageCountStartDT { get; set; }
public Int32 TenancyId { get; set; }
public virtual int FeedScopeInt { get; set; }
}
谁能帮我映射这些属性?
你试过这样的事情吗:
Feed feed = null;
var sql =
@"
SELECT * FROM Feeds WHERE FeedId= @FeedId
SELECT InboundProperties_ExternalRef as ExternalRef, InboundProperties_ExternalRefPrevious as ExternalRefPrevious FROM Feeds as InboundProperties WHERE FeedId= @FeedId
SELECT * FROM FeedFilterParameters WHERE FeedId = @FeedId
SELECT * FROM TeamFeeds WHERE FeedId = @FeedId";
using (var multi = DbConnection.QueryMultiple(sql, new { FeedId = feedId }))
{
feed = multi.Read<Feed>().Single();
feed.InboundProperties = multi.Read<InboundProperties>().Single();
feed.Parameters = multi.Read<FeedFilterParameter>().ToList();
feed.TeamFeeds = multi.Read<TeamFeed>().ToList();
}
return feed;
我只映射了前 2 个属性,但如果它有效,您就会知道如何映射所有属性。
这就是我解决它的方法,但接受了#rraszewski 的回答,因为它也有效,但这意味着我需要以非常手动的方式处理所有选择。
public Feed GetFeedDapper(int feedId)
{
Feed feed = null;
var multiPredicate = new GetMultiplePredicate();
multiPredicate.Add<Feed>(Predicates.Field<Feed>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<InboundProperties>(Predicates.Field<InboundProperties>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<OutboundProperties>(Predicates.Field<OutboundProperties>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<FeedFilterParameter>(Predicates.Field<FeedFilterParameter>(x => x.FeedId, Operator.Eq, feedId));
multiPredicate.Add<TeamFeed>(Predicates.Field<TeamFeed>(x => x.FeedId, Operator.Eq, feedId));
var result = DbConnection.GetMultiple(multiPredicate);
feed = result.Read<Feed>().Single();
feed.InboundProperties = result.Read<InboundProperties>().Single();
feed.OutboundProperties = result.Read<OutboundProperties>().Single();
feed.Parameters = result.Read<FeedFilterParameter>().ToList();
feed.TeamFeeds = result.Read<TeamFeed>().ToList();
return feed;
}
然后我映射 类:
public class FeedMapper : ClassMapper<Feed>
{
public FeedMapper()
{
base.Table("Feeds");
Map(f => f.FeedId).Key(KeyType.Identity);
Map(f => f.Owner).Ignore();
Map(f => f.TeamFeeds).Ignore();
Map(f => f.FeedDirection).Ignore();
Map(f => f.InboundProperties).Ignore();
Map(f => f.Parameters).Ignore();
Map(f => f.OutboundProperties).Ignore();
Map(f => f.RelatedTeams).Ignore();
AutoMap();
}
}
public class InboundPropertiesMapper : ClassMapper<InboundProperties>
{
public InboundPropertiesMapper()
{
base.Table("Feeds");
Map(f => f.FeedId).Key(KeyType.Identity);
Map(f => f.Channel).Ignore();
Map(f => f.FeedScope).Ignore();
Map(f => f.ChannelInt).Column("InboundProperties_ChannelInt");
Map(f => f.ExternalAPICounts).Column("InboundProperties_ExternalAPICounts");
Map(f => f.ExternalId).Column("InboundProperties_ExternalId");
Map(f => f.ExternalLink).Column("InboundProperties_ExternalLink");
Map(f => f.ExternalName).Column("InboundProperties_ExternalName");
Map(f => f.ExternalPicture).Column("InboundProperties_ExternalPicture");
Map(f => f.ExternalRef).Column("InboundProperties_ExternalRef");
Map(f => f.ExternalRefPrevious).Column("InboundProperties_ExternalRefPrevious");
Map(f => f.ExternalToken).Column("InboundProperties_ExternalToken");
Map(f => f.FeedScopeInt).Column("InboundProperties_FeedScopeInt");
Map(f => f.LastProcessedMessageId).Column("InboundProperties_LastProcessedMessageId");
Map(f => f.LastProcessedMessageTime).Column("InboundProperties_LastProcessedMessageTime");
Map(f => f.MessageCountStartDT).Column("InboundProperties_MessageCountStartDT");
Map(f => f.TenancyId).Column("InboundProperties_TenancyId");
AutoMap();
}
}
public class OutboundPropertiesMapper : ClassMapper<OutboundProperties>
{
public OutboundPropertiesMapper()
{
base.Table("Feeds");
Map(f => f.FeedId).Key(KeyType.Identity);
Map(f => f.Channel).Ignore();
Map(f => f.FeedType).Ignore();
Map(f => f.OutboundFeedTypeInt).Column("OutboundProperties_OutboundFeedTypeInt");
Map(f => f.TenancyId).Column("OutboundProperties_TenancyId");
AutoMap();
}
}
我对这里提供的解决方案不满意(太容易出错),所以我决定创建一个扩展方法来使用反射映射对象。
public static class ConnectionExtensions
{
public static IEnumerable<T> QueryIncludeNestedObjects<T>(this SqlConnection connection, string sql)
{
var queryResults = connection.Query<dynamic>(sql);
var typeOfTMain = typeof(T);
foreach(var row in queryResults)
{
var mappedObject = Activator.CreateInstance<T>();
foreach (var col in row)
{
var colKey = (string)col.Key;
var colValue = (object)col.Value;
if(colKey.Contains("_"))
{
var subObjNameAndProp = colKey.Split('_');
var subProperty = typeOfTMain.GetProperty(subObjNameAndProp[0]);
if (subProperty == null) continue;
var subObj = subProperty.GetValue(mappedObject);
if(subObj == null)
{
subObj = Activator.CreateInstance(subProperty.PropertyType);
typeOfTMain.GetProperty(subObjNameAndProp[0]).SetValue(mappedObject, subObj);
}
subObj.GetType().GetProperty(subObjNameAndProp[1])
.SetValue(subObj, colValue);
}
else
typeOfTMain.GetProperty(colKey)?.SetValue(mappedObject, colValue);
}
yield return mappedObject;
}
}
}
那你这样用:
dbConnection.QueryIncludeNestedObjects<Feed>("SELECT * FROM Feeds");
如果你想像在这个问题中那样在 QueryMultiple 中使用它,只需像这样修改方法:
public static IEnumerable<T> ReadIncludeNestedObjects<T>(this GridReader gridReader)
{
var queryResults = gridReader.Read<dynamic>();
...