“主对象”需要在没有数据库调用的情况下存在 [英] 'Master Object' needs to exist without database calls

查看:122
本文介绍了“主对象”需要在没有数据库调用的情况下存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在建立一个游戏,这样可能有助于更好地将对象置于上下文中。

We're building a game, so that may help to put the objects into context better.

我有一种情况,我有一种这样的结构..

I have a situation where I have a structure kind of like this..

- Template
  - Data(ICollection<Statistics>)

  - Character: Template
    - Data (ICollection<Statistics>)

详细说明...
让我们假设每个角色都有一个属性健康。现在,健康的默认值为15。

To elaborate... Let us assume that every character has a property "Health". Now, the default for "Health" is 15.

现在让我们假设每个角色从那开始。作为一生中的一个角色,它可能会添加新的值健康。但是他们仍然需要保留默认的引用。

Now let us assume that every character starts with that. As a character over its lifetime, it might add to "Health" with new values. But they still need to retain the default reference.

现在,'Character'从'Template'继承默认值,但每个字符都将自己的追加的数据集。原始继承不起作用,因为Item附加,它不会覆盖。原来的默认值仍然需要存在。

Now, 'Character' inherits defaults from 'Template', but each character will have its own set of data that appends the defaults. Raw inheritance does not work because the Item appends, it does not overwrite. The original defaults still need to exist.

可以解决它这样..

- Character
 - Template
 - Data (ICollection<Statistic>)

但这对我来说是多余的。它需要大量额外的数据库调用。基本上每个对一个项目的调用都必须执行相同的代码,因为它也必须构造一个Template对象。

But this is redundant to me. It requires a lot of extra database calls. Basically every call to a item has to do the same code twice because it has to construct a Template object too.

有没有更合乎逻辑的方法呢?

Is there a more logical way to go about this?

推荐答案

如果我正确理解你的话,这听起来像是要在模板中拥有静态值,然后动态更新这些价值观的继承者?如果所有的角色需要有相同的默认数据,你可以这样做吗?

If I'm understanding you correctly, it sounds like you want to have static values in the template, and then dynamically update the inheritors of those values? If all characters need to have the same default data, can you do something like this?

abstract class Template { 
    ICollection<Statistics> DefaultData;
}

class CharacterTemplate : Template { 
    private CharacterTemplate() {
        //appropriate data initialization
    }

    private static readonly CharacterTemplate _instance = new CharacterTemplate();
    public static Template Instance { get { return _instance; } }
}

class Character { 
    Template template = CharacterTemplate.Instance; /* CharacterTemplate */
    ICollection<Statistics> Data ;
}

让Template实现者实现Singleton模式,您必须从数据库最多一次。

Have the Template implementors implement the Singleton pattern, and you have to initialize it from the database at most once.

对于单例模式的背景,请检查维基百科: http://en.wikipedia.org/wiki/Singleton_pattern#C.23

For the background on the singleton pattern, check wikipedia: http://en.wikipedia.org/wiki/Singleton_pattern#C.23

这篇关于“主对象”需要在没有数据库调用的情况下存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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