根本的问题是,我似乎遇到了很多。You有一个具有一些共同点的对象集合,但有关对象的其他信息可能适用于其中一些对象,但不适用于另一些对象。此外,随着应用程序的增长,这类信息的数量肯定会随着时间的推移而增加。挑战是以一种正常的方式来管理这些信息。编码时间是一个问题;存储空间不是问题。
以下是我处理这个问题的最新尝试的核心。我会把一些相关的课程放在下面。
public class TournamentModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public HashSet Entrants { get; set; }
public List Rounds { get; set; } = new List();
public GameModel Game { get; set; }
public Dictionary Details { get; set; }
}
除了细节之外,上面的每一件事都是我所期望的绝大多数赛事都会有的。但是,我们来到了像播种,奖品和其他属性的比赛可能或可能没有的事情。作为进一步的挑战,播种和奖品都不会总是采用相同的形式。例如,一个种子可以也可能不是所有玩家的种子,一些种子将玩家分成组而不是线性排序。一场比赛可能会为第一名到第三名颁发奖品,而另一项比赛可能会因为最有趣的比赛而颁发奖品。
下面是LinearSeeding和PlacePrizes的详细信息类:
public class LinearSeedingModel: ITournamentDetails
{
public List SeededEntrants { get; set; }
}
public class PlacePrizesModel: ITournamentDetails
{
public List Prizes { get; set; }
}
下面是将事物粘合在一起的TournamentDetailsService。可以预计,随着时间的推移,这一数字还会增加。
public class TournamentDetailsService
{
public const string LinearSeedingKey = "LinearSeeding";
public const string PlacePrizesKey = "PlacePrizes";
public LinearSeedingModel GetSeeding(TournamentModel tournament)
=> tournament.Details.GetValue(LinearSeedingKey) as LinearSeedingModel;
public void SetSeeding(TournamentModel tournament, LinearSeedingModel seeding)
=> tournament.Details.SetValue(LinearSeedingKey, seeding);
public PlacePrizesModel GetPlacePrizes(TournamentModel tournament)
=> tournament.Details.GetValue(PlacePrizesKey) as PlacePrizesModel;
public void SetPlacePrizes(TournamentModel tournament, PlacePrizesModel prizes)
=> tournament.Details.SetValue(PlacePrizesKey, prizes);
}
下面是服务使用的DictionaryExtensions类:
public static class DictionaryExtensions
{
public static T GetValue(this Dictionary dictionary, string key, T defaultValue = default(T))
=> dictionary.ContainsKey(key) ? dictionary[key] : defaultValue;
public static void SetValue(this Dictionary dictionary, string key, T value)
where T : class
{
if (value == null)
{
dictionary.Remove(key);
}
else
{
dictionary[key] = value;
}
}
}
如果我按照上面的模式,它可能会重复很多次。例如,可以想象TournamentEntrantModel和TournamentRoundModel上的详细信息字段。
作为另一种选择,我还没有排除拥有一个包含属性列表的胖TournamentModel类的可能性,这些属性将随着时间的推移而增长,其中许多属性将不适用于特定的锦标赛。不知道那可能是更多的麻烦还是更少的麻烦。
该模型将通过Sql (未显示,但不会出现问题)持久化,并显示在各种前端,包括web,最终可能是移动的。通常,需要访问详细信息的类将在其构造函数中注入TournamentDetailsService。
发布于 2018-12-08 14:33:13
https://softwareengineering.stackexchange.com/questions/381371
复制相似问题