RavenDB 音乐数据库 [英] RavenDB for Music Database

查看:32
本文介绍了RavenDB 音乐数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您的建议,RavenDB 是否适合构建音乐数据库.我想在 C# Windows 应用程序中使用嵌入式版本.

I need your advice whether RavenDB would be suitable for building a Music Database. I would like to use the embedded version in a C# Windows Application.

目前数据库基于 SQL 并进行了规范化,例如有表格艺术家、专辑、流派、共享(音乐收藏的主文件夹)、文件夹、歌曲,然后是一堆表格来建立关系,如专辑艺术家、流派歌曲、艺术家歌曲、作曲家歌曲、指挥宋等.我想你会明白的.

Currently the database is based on SQL with normalisation in place, having tables for e.g. Artist, Album, Genre, Share (main folder of music collection), Folder, Song and then a bunch of tables to build the relations like AlbumArtist, GenreSong, ArtistSong, ComposerSong, ConductorSOng, etc. I think you'll get it.

现在使用 RavenDB,我可以将每首歌曲存储为一个包含所有信息的文档,但随后我会将 ArtistNAme、AlbumName 甚至每首歌曲的文件夹相乘.

Now with RavenDB I could store every Song as a Document, containing all the information, but then I would multiply ArtistNAme, AlbumName and even the Folder for every song.

发现我可以将艺术家、流派等分开并在我的查询中使用包含,但是我将如何运行一个查询,该查询为我提供所有具有摇滚"流派的歌曲或特定艺术家的所有专辑?

Figured out that I could separate Artist, Genre, etc. and use Includes in my Query, but how would i run then a query which gives me all Songs with a Genre of "Rock" or all Albums for a specific Artist?

我的理解是,我需要一个索引才能将包含的文档中的属性用作查询的一部分.否则我会得到编译错误.正确的?所以基本上我需要构建一个包含用户可能执行查询的所有字段的大型索引.

My understanding is that i would need an Index to be able to use Properties from an included document as part of a query. Otherwise I would get compilation errors. Right? So basically I would need to build one large index containing all the fields that a user might do a query.

或者有没有我没有看到的其他方式?

Or is there any other way, which I don't see?

推荐答案

虽然您可以在索引中包含"来自其他文档的属性(使用 LoadDocument),但不建议广泛使用,因为索引需要更频繁地重建.

While you can "include" properties from other documents in an index (using LoadDocument) it's not recommended to use extensively as the index need to be rebuilt more often.

在您的情况下,您可以对您的歌曲文档进行建模,以通过 id 和查询包含对艺术家、流派等的引用,然后使用转换器将结果转换为所需的视图模型".在转换器中使用 LoadDocument 来获取艺术家姓名、流派名称等并返回转换后的结果.转换是根据请求在服务器端执行的.

In your case you could model your Song document to include references to Artist, Genre etc by id and query on that and then use a Transformer to transform the result to desired "view model". Use LoadDocument in the transformer to fetch artist name, genre name etc and return the transformed result. The transformation is performed server side on request.

您的歌曲实体(简化版)可能如下所示:

Your song entity (simplified) might look like this:

public class Song
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string ArtistId { get; set; }
}

还有这样的索引:

public class Song_ByArtist : AbstractIndexCreationTask<Song>
{
    public Song_ByArtist()
    {
        Map = songs => from song in songs
                       select new
                       {
                           song.Name,
                           song.ArtistId
                       };
    }
}

结合变压器:

public class Song_Artist_Transformer : AbstractTransformerCreationTask<Song>
{
    public Song_Artist_Transformer()
    {
        TransformResults = results => from song in results
                                      let artist = LoadDocument<Artist>(song.ArtistId)
                                      select new SongArtistViewModel
                                      {
                                          SongName = song.Name,
                                          ArtistName = artist.Name
                                      };
    }
}

您可以按艺术家查询歌曲并返回包含艺术家姓名的视图模型:

You can query for songs by artists and return a view model including the artist name with:

using (var session = _documentStore.OpenSession())
{
    var results = session.Query<Song, Song_ByArtist>()
        .Where(x => x.ArtistId == "artists/1")
        .TransformWith<Song_Artist_Transformer, SongArtistViewModel>();
}

这将返回艺术家artists/1"的所有歌曲,转换为带有歌曲名称和艺术家姓名的视图模型.

This would return all songs for artist "artists/1" transformed as a view model with song name and artist name.

所以底线是:对您的歌曲文档进行建模,以在需要的地方包含对其他文档的引用(如果遵循 DDD,则为聚合),然后通过使用转换器包含所需的信息.可以将 Transformer 视为关系数据库中的视图".

So the bottom line is: model your song document to include references to other documents (aggregates if following DDD) where needed and then include the information needed by using transformers. Transformers could be looked at sort of like a "View" in a relational db.

注意:为您的歌曲文档制作一个组合索引,您可以在其中索引所有属性(歌曲属性和引用),然后根据需要使用多个转换器来呈现数据.对于同一文档类型,每个文档使用一个大"索引通常比使用几个小索引更好.在这个例子中,为了简单起见,我只映射了名字和艺术家 ID.

Note: Make one combined index for your song document where you index all properties (both song properties and references) and then use multiple transformers to present the data as needed. It's often better to use one "large" index per document instead of several small for the same document type. In this example I only mapped the name and artist id to keep it simple.

希望这会有所帮助!

这篇关于RavenDB 音乐数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆