3

我有以下示例代码,它使用 MongoEngine 和 Peewee 作为数据库后端。

import mongoengine, peewee
from mongomodels import *
from mysqlmodels import *

class Parser(object):

    def __init__(self, line, dbBackend):
        if dbBackend in ["MongoDB","MySQL"]:
            self.line = line
            self.DB = dbBackend
            user = self.createUser()
            car = self.createCar(user)
            parking = self.createParking(car)
        else:
            raise Exception()

    def createUser(self):
        if self.DB == "MongoDB":
            newUserID = self._createMongoUser(self.line['firstname'], self.line['lastname'], '...')
        else:
            newUserID = self._createMySQLUser(self.line['firstname'], self.line['lastname'], '...')

        return newUserID

    def _createMongoUser(self, firstname, lastname, '...'):
        try:
            _user = MongoUserModel.objects.get(firstname=firstname, lastname=lastname)
        except mongoengine.errors.DoesNotExist as e:
            user = MongoUserModel(firstname=firstname, password)
            _user = user.save()
        finally:
            return _user



    def _createMySQLUser(self, firstname, lastname, '...'):
        try:
            _user = MySQLUserModel.get(MySQLUserModel.fistname == firstname, MySQLUserModel.lastname == lastname )
        except Exception as e:
            user = MySQLUserModel(fistname=fistname, lastname=lastname)
            _user = user.save()
        finally:
            return _user


    def createCar(self, user):
        pass

    def createParking(self, car):
        pass

是否有任何好的做法/技巧/模块来保持我的代码干燥并避免重新定义两种方法来创建我的模型?

我应该像 PHP 中的 PDO 一样创建一个新的抽象类“UserModel”吗?

4

1 回答 1

1

这是我最近经历的事情——我从 mongo 后端换成了 postgres。当我建立原始项目时,我有一些模型和一个 DataLayer。数据层 (dl) 有一个非常简单的界面,我在整个应用程序中都使用了它。

# note: this is half python / half pseudocode

class Model1(object):
    __collection__ = 'model1'
    __tablename__ = 'model1'
    # field definitions etc


class MongoDataLayer(object):
    def __init__(self, mongo_db_connection):
        self.conn = mongo_db_connection

    def load(self, model, conditions):
        raw = self.conn[model.__collection__].find(...)
        return model(**raw)

    def persist(self, obj):
        self.conn[obj.__collection__].save(obj.as_dict())


class SQLDataLayer(object):
    def __init__(self, sa_session_factory):
        self.Session = sa_session_factory
        self.session = self.Session()

    def load(self, model, conditions):
        return self.session.query(model).find_by(conditions).one() # ...etc

    def persist(self, obj):
        self.conn[obj.__collection__].save(obj)

# connections - mongo and postgres (I use SQLAlchemy)
dl_mongo = MongoDataLayer(db...)
dl_sql = SQLDataLayer(Session...)

# using them - you don't care which one you have
m = dl_mongo.load(models.Model1)
dl_mongo.persist(m)

m = dl_sql.load(models.Model1)
dl_sql.persist(m)

在我的应用程序中,我在初始加载时加载 dl,然后在需要进行数据访问时将其注入应用程序。然后应用程序本身知道模型,但不知道如何加载/保存它们的细节。

也许不是最好的方法,但对我来说效果很好。很想听听其他人如何处理它。

于 2013-10-10T09:55:13.967 回答