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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?