1

我有一个 RavenDB 文档的以下结构

{
    "RpcTechDataCollectionModel": {
        "Weekend": "March 16 - 17, 2013",
        "ServiceTitle": "Some Title",
        "Notes": "",
        "WeekendServices": [{
            "ServiceTime": "",
            "SiteName": "Bowridge",
            "SoundOperator": "Rob",
            "WorshipLeader": "Daryl",
            "Notes": "",
            "Songs": [{
                "SongName": "Foo",
                "MinSpl": "86",
                "MaxSpl": "92",
                "Note": ""
            }, {
                "SongName": "Bar",
                "MinSpl": "89",
                "MaxSpl": "96",
                "Note": ""
            }]
        }, {
            "ServiceTime": "",
            "SiteName": "Bearspaw",
            "SoundOperator": "Peter",
            "WorshipLeader": "Tim",
            "Notes": "",
            "Songs": [{
                "SongName": "Das",
                "MinSpl": "86",
                "MaxSpl": "91",
                "Note": ""
            }, {
                "SongName": "Bar",
                "MinSpl": "87",
                "MaxSpl": "99",
                "Note": ""
            }]
        }]
    }
}

现在我正在尝试为此建立一个模型,但我想知道构建它的最佳方法是什么。注意,没有子对象WeekendServiceSong将在父对象之外使用RpcTechCollectionModel

选项一是

namespace MyProject.Models {
    using System.Collections.Generic;
    public class RpcTechDataCollectionModel{
        RpcTechDataCollectionModel(){
            this.WeekendServices = new List<WeekendService>();
        }
        public string Weekend { get; set; }
        public string ServiceTitle { get; set; }
        public string Notes { get; set; }
        public List<WeekendService> WeekendServices { get; set; }
        public class WeekendService{
            WeekendService(){
                this.SongRecords = new List<SongRecord>();
            }
            public DateTime ServiceTime { get; set; }
            public string SiteName { get; set; }
            public string SoundOperator { get; set; }
            public string WorshipLeader { get; set; }
            public string Notes { get; set; }
            public List<Song> Songs { get; set; }
            public class Song {
                public string SongName { get; set; }
                public double MinSpl { get; set; }
                public double MaxMax { get; set; }
                public string Note { get; set; }
            }
        }
    }
}

我更喜欢这个,因为它既漂亮又干净,而且易于理解。问题在于它在技术上违反了“每个文件一个类”规则

选项二

public class RpcTechDataCollectionModel{
    RpcTechDataCollectionModel(){
        this.WeekendServices = new List<WeekendService>();
    }
    public string Weekend { get; set; }
    public string ServiceTitle { get; set; }
    public string Notes { get; set; }
    public List<WeekendService> WeekendServices { get; set; }
}
public class WeekendService{
        WeekendService(){
            this.SongRecords = new List<SongRecord>();
        }
        public DateTime ServiceTime { get; set; }
        public string SiteName { get; set; }
        public string SoundOperator { get; set; }
        public string WorshipLeader { get; set; }
        public string Notes { get; set; }
        public List<Song> Songs { get; set; }

}
public class Song {
    public string SongName { get; set; }
    public double MinSpl { get; set; }
    public double MaxMax { get; set; }
    public string Note { get; set; }
}

这并不违反该规则,但似乎有点痛苦。

以一种或另一种方式做这件事的利弊是什么,一种被认为是“正确/首选”的方式,还是在这种情况下它更“让你的船浮起来”?

4

1 回答 1

2

嵌套类通常用于隐藏实现细节,从而防止消费者滥用它们。由于这些类只是定义数据契约,似乎没有什么可隐藏的。即使你想阻止任何人修改它们(这对你拥有所有权的数据合约来说是有意义的),最好将它们密封起来。

我会将它们放在不同的文件中,并可能分组在文件夹中。

于 2013-03-17T04:42:39.880 回答