When avoiding global vars (/state), i find myself linking objects backwards to its parent. Am I doing this right? if not explain why? and how-else?

huangapple go评论138阅读模式
英文:

When avoiding global vars (/state), i find myself linking objects backwards to its parent. Am I doing this right? if not explain why? and how-else?

问题

注意:我只会翻译代码部分,不会回答关于翻译的问题。以下是翻译的内容:

注意:我只是选择当前的结构/示例来解释问题。

type MsgBoxFactory struct{
    db *dbSql //包含连接池和其他与数据库相关的设置/标志
}

func (f *MsgBoxFactory) NewMsgBox(userId string) {
    return MsgBox{userId, f.db} //f.db链接是不可避免的
}

type MsgBox struct {
    ownerId string
    db *dbSql
}

func (m *MsgBox) NewMessage(content string) *Message {
    return Message{content, *m.dbSql} //m.dbSql链接是不可避免的
}

type Message struct {
    content string
    //其他字段,如收件人、是否已读、创建时间等

    db *dbSql
}

func (m *Message) Send(to string) {
    message.to = to  //想象一下,这将消息保存到数据库中。
    m.db.Save(message)
}

我倾向于称之为“反向引用”[我不知道实际名称]...这是唯一的方法吗?以前我是“反向引用”整个父对象。现在我发现自己在“反向引用”配置文件/数据库连接等对象。

这是一个好方法吗?有更好的方法吗?

哦,我还尝试了闭包,至少从视图中摆脱它。

type Message Struct{
    content string

    Send func(string) error //问题是需要添加`json:"-"`。否则,对象不兼容JSON
}

func (m *MsgBox) NewMsg(content string) *Message {
    msg := &Message{ content }
    msg.Send = func(to string) error {
        return m.db.Save(msg)
    }
}

基本上,代码看起来几乎一样混乱,有不必要的复杂性/代码。

编辑:问题不仅限于Go语言。只是因为我使用Go语言而发布的。欢迎提供标签建议,以便向更广泛的社区提问。

英文:

Note: Im just picking the current struct/example to explain the problem.

type MsgBoxFactory struct{
    db *dbSql //contains conn-pool and other DB related settings/flags
}

func (f *MsgBoxFactory) NewMsgBox(userId string) {
    return MsgBox{userId, f.db} //f.db link is inevitable
}

type MsgBox struct {
    ownerId string
    db *dbSql
}

func (m *MsgBox) NewMessage(content string) *Message {
    return Message{content, *m.dbSql} //m.dbSql link is inevitable
}

type Message struct {
    content string
    //other fields such as recipents, isRead, created time etc.

    db *dbSql
}

func (m *Message) Send(to string) {
    message.to = to  //just imagine this saves the message to database.
    m.db.Save(message)
}

I tend to call this "backward-referencing"[i don't know actual name]... Is this the only way? Previously i was "backward-referencing" entire parent objects. Now i find myself "backward-referencing" objects such as config/dbconn etc...

Is this a good way? what is better?

Oh i have also tried closure to get rid of it atleast from view.

type Message Struct{
    content string

    Send func(string) error // the problem is `json:"-"` needs to be added. Else the objects are not json compatible
}

func (m *MsgBox) NewMsg(content string) *Message {
    msg := &Message{ content }
    msg.Send = func(to string) error {
        return m.db.Save(msg)
    }
}

Basically the code looks almost equally cluttered with unnecessary complexity/code

EDIT: The question is not specific to go. Just posted it because i use go. Any tag suggestion is appreciated to open the question for wider community.

答案1

得分: 1

我通常实现一个模型助手关系。

其中,MsgBox是你的模型,它具有所有特定数据元素(不涉及数据库的元素)。

MsgBoxHelper负责处理与数据库相关的工作。
(例如:

err := MsgBoxHelper.Save(MsgBox)
msgBox, err := MsgBoxHelper.Load(Key)

编辑:

这种方法的优点是将模型与数据存储解耦,这样如果你希望更改底层技术(这种情况并不经常发生),会更容易。在实践中,如果你开始进行缓存等操作,这种方法会更有用。

如果在你的模型中通用地引用其他结构,例如:

type MsgBox struct {
    Colour *MsgBoxColour
    ...
}

type MsgBoxColor struct {
    ID int
    ...
}

那么在MsgBoxHelper中加载模型时,你可以使用存储在MsgBoxColour表中的ID调用MsgBoxColourHelper,然后返回与返回的MsgBox关联的MsgBoxColour。

英文:

I usually implement a model helper relationship.

Where MsgBox is your model which has all the data specific elements (No DB related elements).

The MsgBoxHelper does all your database related work.
(i.e.

err := MsgBoxHelper.Save(MsgBox)
msgBox, err := MsgBoxHelper.Load(Key)

)

Edit:

The advantage with this method is it decouples your Model from the datastore, which should inturn make it easier should you wish to change your underlying technology (Which doesn't often happen). In practice it's more useful should you start doing things like caching.

If generically you are referencing other structures within your model i.e.

type MsgBox struct {
    Colour *MsgBoxColour
    ...
}

type MsgBoxColor struct {
    ID int
    ...
}

then when you load the Model in your MsgBoxHelper you call the MsgBoxColourHelper with the ID you stored in the MsgBoxColour table, this then returns your MsgBoxColour that you then associate with the returning MsgBox.

huangapple
  • 本文由 发表于 2014年9月15日 16:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/25844121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定