使用MongoDB C#驱动程序读取多种类型
我尝试创建一个时间线集合,该集合可以托管多种类型。我没有问题插入类型或读取特定类型。但是,当我想要特定用户的所有类型列表时,我会出现问题。
public interface ITimelineItem
{
public string EventType { get; set; }
public string EventId { get; set; }
public Guid UserId { get; set; }
}
public class TimelineItemBase
{
public string EventType { get; set; }
public string EventId { get; set; }
public Guid UserId { get; set; }
public List<MediaInfo> Media { get; set; }
}
public class AlbumEvent : TimelineItemBase, ITimelineItem
{
public MediaAlbum Album { get; set; }
}
public class BirthdayEvent : TimelineItemBase, ITimelineItem
{
}
public class BlogPostEvent : TimelineItemBase, ITimelineItem
{
public BlogPost Post { get; set; }
}
public static class TimelineRepo
{
private static IMongoCollection<T> GetCollection<T>() where T : ITimelineItem
{
return Connection.Database.GetCollection<T>("Timeline");
}
public static async Task Add<T>(T data) where T : ITimelineItem
{
data.EventType = typeof(T).Name;
var coll = GetCollection<T>();
await coll.InsertOneAsync(data);
}
public static async Task<List<T>> GetEventsByUserId<T>(Guid userId) where T : ITimelineItem
{
return await GetCollection<T>().Find(p => p.UserId == userId).ToListAsync();
}
}
public class GenerateTimeline
{
public static async Task<List<ITimelineViewModel>> GetTimeline(Guid userId)
{
var items = await TimelineRepo.GetEventsByUserId<???>(userId);
foreach (var item in items)
{
if (item is BirthdayEvent be)
{
// do work
}
else if (item is BlogPostEvent bp)
{
// do work
}
else if (item is AlbumEvent ae)
{
// do work
ae.Album.Header
}
}
}
}
[HttpGet("/api/user/{userid}/timeline"), Authorize]
public async Task<List<ITimelineViewModel>> GetTimeline(Guid userId)
{
return await GenerateTimeline.GetTimeline(userId);
}
如何获得包含用户不同类型的列表?还是这是为用户创建时间表的不好方法?
I try to create a timeline collection that contians multiple types. I have no problem to insert the type or read a specific type. But when i want a list of all types for a specific user, then i got problem.
public interface ITimelineItem
{
public string EventType { get; set; }
public string EventId { get; set; }
public Guid UserId { get; set; }
}
public class TimelineItemBase
{
public string EventType { get; set; }
public string EventId { get; set; }
public Guid UserId { get; set; }
public List<MediaInfo> Media { get; set; }
}
public class AlbumEvent : TimelineItemBase, ITimelineItem
{
public MediaAlbum Album { get; set; }
}
public class BirthdayEvent : TimelineItemBase, ITimelineItem
{
}
public class BlogPostEvent : TimelineItemBase, ITimelineItem
{
public BlogPost Post { get; set; }
}
public static class TimelineRepo
{
private static IMongoCollection<T> GetCollection<T>() where T : ITimelineItem
{
return Connection.Database.GetCollection<T>("Timeline");
}
public static async Task Add<T>(T data) where T : ITimelineItem
{
data.EventType = typeof(T).Name;
var coll = GetCollection<T>();
await coll.InsertOneAsync(data);
}
public static async Task<List<T>> GetEventsByUserId<T>(Guid userId) where T : ITimelineItem
{
return await GetCollection<T>().Find(p => p.UserId == userId).ToListAsync();
}
}
public class GenerateTimeline
{
public static async Task<List<ITimelineViewModel>> GetTimeline(Guid userId)
{
var items = await TimelineRepo.GetEventsByUserId<???>(userId);
foreach (var item in items)
{
if (item is BirthdayEvent be)
{
// do work
}
else if (item is BlogPostEvent bp)
{
// do work
}
else if (item is AlbumEvent ae)
{
// do work
ae.Album.Header
}
}
}
}
[HttpGet("/api/user/{userid}/timeline"), Authorize]
public async Task<List<ITimelineViewModel>> GetTimeline(Guid userId)
{
return await GenerateTimeline.GetTimeline(userId);
}
How can I get a list containing different types for a user? Or is this a bad way to create a timeline for users?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论