python-三层架构

2017-01-01 10:46

最近看自己的代码质量越来越差,有人建议我用三层优化一次

  • MODEL 实体类.业务之间尽量用实体类传递
  • DAL数据处理,用的最多的就是数据库方法
  • BLL 业务逻辑
  • COMMON 放一些公用的方法在里面

model层

用的schematics,不用自己写get,set了

from schematics.models import Model
from schematics.types import StringType,IntType
class monkeyconfig(Model):
    cmd = StringType()
    package_name = StringType()
    logdir = StringType() #本机的log存放地址
    remote_path = StringType()  #远程服务器地址,可以给开发查看
    phone_msg_log = StringType() #临时存放手机日志信息路径
    now = StringType()
    exceptions = StringType() #异常列表监控
    sum = StringType() #事件数量
    activity = StringType()
    monkey_log = StringType()
  • 如果我的实体类想放list怎么弄?ListType(IntType) 这样就可以了,是不是很酷,设置值就这样设置 [[111],[3333]]

DAL层

import configparser
import time
def monkeyConfig(mmonkeyconfig, init_file):
    config = configparser.ConfigParser()
    config.read(init_file)
    mmonkeyconfig.package_name = config['DEFAULT']['package_name']
    mmonkeyconfig.logdir = config['DEFAULT']['logdir']
    mmonkeyconfig.remote_path = config['DEFAULT']['remote_path']
    mmonkeyconfig.phone_msg_log = config['DEFAULT']['phone_msg_log']
    mmonkeyconfig.now = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))
    mmonkeyconfig.activity = config['DEFAULT']['activity']
    mmonkeyconfig.sum = int(config['DEFAULT']['sum'])
    mmonkeyconfig.monkey_log = mmonkeyconfig.logdir + "\\" + mmonkeyconfig.now + r"monkey.log"
    mmonkeyconfig.cmd = config['DEFAULT']['cmd'] + " " + str(mmonkeyconfig.sum) + ">>" + mmonkeyconfig.monkey_log
    return mmonkeyconfig

BLL层

from DAL import DMonkeyConfig
def monkeyConfig(mmonkeyconfig, init_file):
    return DMonkeyConfig.monkeyConfig(mmonkeyconfig, init_file)

UI层

# 只调用BLL层的东西
from BLL import BMatPlo
from BLL import BAdbCommon
from Common import OperateFile
from BLL import BMonkeyConfig
from Model import MMonkeyConfig
from Model import MMatplo
from BLL import BMenCpu

 ba = BAdbCommon
mconfig = MMonkeyConfig.monkeyconfig()
mc = BMonkeyConfig.monkeyConfig(mconfig, ini_file)

# 下面就可以取实体类的值
mc.cmd, mc.logdir, mc.now, mc.monkey_log

更多

参考我的github接口框架