dtzd65908 2017-02-28 17:27
浏览 93

使用多个数据库(DAO模式)

I am creating a project which should be able to use different databases for the persistence of data. I have implemented a DAO pattern, but I have a problem when working with the ID in the struct, since for example MongoDB uses BSON whereas SQL databases use uint.

What solution do you recommend to implement the ID field in the struct.

What I can think of is to create an ID object that contains fields for BSON and uint IDs with GetBSON, GetUint methods and a GetID method that encodes either BSON or uint to MD5 (or some other encoding) I would use the GetBSON and GetUint methods to work with the Databases and the GetID method to work on the application as well as to send it through the REST API.

  • 写回答

1条回答 默认 最新

  • dongxia19772008 2017-02-28 17:59
    关注

    Cases like this your best bet is to use a universal identifier (UID) There are many ways to do this, though my personal favorite is just UUID v4. You can read the full UUID spec here https://tools.ietf.org/html/rfc4122.

    Just configure your SQL tables and code to ensure uniqueness with this identifier and you can easily do lookups between databases using whichever access pattern you wish to implement.

    e.g.

    SQL
    
    ID(Primary Key) | UID (unique)
             0      |   xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    
    MONGO
    
    {
      _id: ObjectID(),
      uid: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    }
    

    now just lookup on uid only and you can be database agnostic at the higher level.

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站