使用MongoDB C#驱动程序读取多种类型

发布于 2025-02-06 19:36:02 字数 2229 浏览 3 评论 0原文

我尝试创建一个时间线集合,该集合可以托管多种类型。我没有问题插入类型或读取特定类型。但是,当我想要特定用户的所有类型列表时,我会出现问题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文