dongliao9233 2014-04-10 21:13
浏览 23
已采纳

Go中的数据抽象层

I am going to create a project in Go, but I want to separate my tiers. My goal is create a package that has code something like the DAO pattern, i.e. I wish that my caller should only communicate with the interface of the service (which might be another project) and not with the implementation. This is to avoid the situation of a future change to the database, the caller should not change and should be transparent to this change.

I was thinking of creating a project db that contains a file called persistence with a package persistence as well

http://play.golang.org/p/O9b93F4LJp

package persistence

// Interface that have the CRUD which I need to use from service(another project)
type Recorder interface {
    Selectkey([]byte) (err error)
    Insert([]byte, []byte) (err error)
    Update([]byte, []byte) (err error)
    Delete([]byte) (err error)
}

// Struct for Oracle
type ManageDataOracle struct {
}
// Struct for binaryTree
type ManageDataBInaryTree struct {
}
// Struct for MongoDB
type ManageDataMongoDb struct {
}

// Implemtations for Oracle
func (memory *ManageDataOracle) SelectKey(pKey []byte) error {
    // Logic for Oracle Here
    return nil
}
func (memory *ManageDataOracle) Insert(pKey []byte, pValue []byte) error {
        // Logic for Oracle Here
    return nil
}
func (memory *ManageDataOracle) Update(pKey []byte, pValue []byte) error {
    // Logic for Oracle Here
    return nil
}

func (memory *ManageDataOracle) Delete(pKey []byte) error {
    // Logic for Oracle Here
    return nil
}

// Implemtations for Binary Tree
func (memory *ManageDataBInaryTree) SelectKey(pKey []byte) error {
    // Logic for Binary tree Here
    return nil
}
func (memory *ManageDataBInaryTree) Insert(pKey []byte, pValue []byte) error {
        // Logic for Binary tree Here
    return nil
}
func (memory *ManageDataBInaryTree) Update(pKey []byte, pValue []byte) error {
    // Logic for Binary tree Here
    return nil
}    

func (memory *ManageDataBInaryTree) Delete(pKey []byte) error {
    // Logic for Binary tree Here
    return nil
}

// Implemtations for Mongo DB
func (memory *ManageDataMongoDb) SelectKey(pKey []byte) error {
    // Logic for MongoDB Here
    return nil
}
func (memory *ManageDataMongoDb) Insert(pKey []byte, pValue []byte) error {
        // Logic for MongoDB Here
    return nil
}
func (memory *ManageDataMongoDb) Update(pKey []byte, pValue []byte) error {
    // Logic for MongoDB Here
    return nil
}

func (memory *ManageDataMongoDb) Delete(pKey []byte) error {
    // Logic for MongoDB Here
    return nil
}

Any advice on how to do this separation of concepts, or how the previous code should be called from another project?

  • 写回答

2条回答 默认 最新

  • dongsu3654 2014-04-11 11:49
    关注

    You are on the right track but different persistence mechanisms will use different key types (for example an integer in an oracle and a string-like identifier in mongo).

    The serialization of your data into []byte should be handled by the abstraction layer because different underlying persistence engines will use different representations of your data. Consider for instance an integer and it's endianness.

    Hence I would change the signatures of your interface to the following:

    type Recorder interface {
        SelectByKey(interface{}) error
        DeleteByKey(interface{}) error
        Insert(interface{}, interface{}) error
        Update(interface{}, interface{}) error
    }
    

    this way you can also panic inside, say SelectKey if the given key is not of the correct type for a specific implementation. This wouldn't be possible if you serialized your key beforehand and would lead to trouble.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?