优化 Python 数据处理:深入了解工作单元模式

工作单元(Unit of Work)设计模式在面向对象的系统中的应用,有助于有效地管理事务性数据,特别适用于需要进行数据库事务处理的应用,例如Web应用。这一模式提供了一种高效且易于维护的方式来处理数据库操作。

工作单元模式存储(Repository)模式通常被结合使用,以提供应用程序数据管理的全面解决方案。尽管它们相关联,但它们拥有各自不同的目标和清晰的职责。

存储模式 用作对持久性存储的抽象层,而工作单元模式则用作对原子操作的抽象层。

工作单元模式的职责在于管理事务并协调在单一事务边界内的数据操作。

存储模式则负责封装对数据存储的访问和查询逻辑,它充当应用程序与数据存储之间的抽象层,使应用程序能够访问和操作数据,而无需了解数据存储的底层细节。

Python的实现

UnitOfWork 负责管理数据库事务并提供上下文管理器接口。

在Python中,UnitOfWork的职责是管理数据库事务并提供上下文管理器接口。

python 复制代码
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

class UnitOfWork:
    def __init__(self):
        self.engine = create_engine('postgresql://user:password@host:port/database')
        self.Session = sessionmaker(bind=self.engine)
        self.session = None

    def __enter__(self):
        self.session = self.Session()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is None:
            self.session.commit()
        else:
            self.session.rollback()
        self.session.close()

    def add(self, entity):
        self.session.add(entity)

    def delete(self, entity):
        self.session.delete(entity)

当一段代码在with语句中执行时,会触发上下文管理器的__enter__方法,通常这个方法会返回一个在代码块内部被使用的对象。当代码块执行完毕后,__exit__方法将被调用,用于清理已经使用的资源。

在我们的案例中,__enter__()方法在事务开始时被调用,而__exit__()方法则在事务结束时被执行。如果在事务中没有发生异常,所做的更改将被提交;反之,更改将被回滚。

为了方便,在事务边界内向数据库添加和删除实体,我们提供了add()delete()方法。

csharp 复制代码
with UnitOfWork() as uow:
    customer = Customer(name='John Doe', email='[email protected]')
    uow.add(customer)

    order = Order(customer=customer, total=100.00)
    uow.add(order)

工作单元(UoW)的作用是确保在上下文管理器中进行的所有更改都以单个原子操作的方式保存到数据库中。

ini 复制代码
from sqlalchemy.orm import sessionmaker
from myapp.models import Order, OrderLineItem

class OrderService:
    def __init__(self, db_engine):
        Session = sessionmaker(bind=db_engine)
        self.session = Session()
    
    def place_order(self, customer_id, order_items):
        # Create a Unit of Work instance
        with self.session.begin():
            # Create a new Order instance
            order = Order(customer_id=customer_id)
            self.session.add(order)
            
            # Add order line items to the Order instance
            for item in order_items:
                line_item = OrderLineItem(
                    order_id=order.id,
                    product_id=item['product_id'],
                    quantity=item['quantity'],
                    price=item['price']
                )
                self.session.add(line_item)
                
            # Commit the changes to the database
            self.session.commit()

我们使用self.session.commit()来将更改提交至数据库。如果在此过程中发生任何错误,将回滚更改至其先前状态,以确保事务以原子方式完成,并保持事务一致性。

通过这种方式应用工作单元模式,我们能够在单个事务内执行多个数据库操作,从而提升性能,减少与数据库的往返次数。这在处理高并发情境或大量数据时尤为重要。

假设我们的应用程序需要支持多个数据源,如主数据库和备用数据库。我们可以运用工作单元模式,将数据库持久性的细节进行抽象,使应用程序能够轻松切换不同数据源。

python 复制代码
class CustomerService:
    def __init__(self, primary_db_engine, backup_db_engine):
        self.primary_unit_of_work = UnitOfWork(primary_db_engine)
        self.backup_unit_of_work = UnitOfWork(backup_db_engine)
    
    def get_customer_by_id(self, customer_id):
        with self.primary_unit_of_work as primary_uow:
            customer = primary_uow.session.query(Customer).get(customer_id)
            if not customer:
                with self.backup_unit_of_work as backup_uow:
                    customer = backup_uow.session.query(Customer).get(customer_id)
        return customer
    
    def create_customer(self, name, email):
        with self.primary_unit_of_work as uow:
            customer = Customer(name=name, email=email)
            uow.session.add(customer)
            uow.save_changes()

CustomerService类中,我们定义了用于按ID获取客户和创建新客户的方法。对于get_customer_by_id方法,我们首先尝试从主数据库中检索客户信息。如果客户在主数据库中不存在,我们会从备用数据库中检索客户信息。这使我们能够在主数据库不可用的情况下轻松切换数据源。

通过这种方式运用工作单元模式,我们可以轻松地支持多个数据源,并在需要时进行切换。我们能够将数据库持久性的细节进行抽象,将注意力集中于应用程序的业务逻辑。

此外,我们还可以将工作单元模式与存储模式一同使用。

python 复制代码
class CustomerRepository:
    def __init__(self, unit_of_work):
        self.unit_of_work = unit_of_work
        
    def get_by_id(self, customer_id):
        with self.unit_of_work as uow:
            customer = uow.session.query(Customer).get(customer_id)
        return customer
    
    def add(self, customer):
        with self.unit_of_work as uow:
            uow.session.add(customer)
            uow.save_changes()
python 复制代码
class CustomerService:
    def __init__(self, db_engine):
        self.db_engine = db_engine
        
    def get_customer(self, customer_id):
        with UnitOfWork(self.db_engine) as uow:
            customer_repo = CustomerRepository(uow)
            customer = customer_repo.get_by_id(customer_id)
        return customer
    
    def add_customer(self, name, email):
        customer = Customer(name=name, email=email)
        with UnitOfWork(self.db_engine) as uow:
            customer_repo = CustomerRepository(uow)
            customer_repo.add(customer)
        return customer.id

我们创建了一个名为CustomerService的类,它提供了一个简便的接口,用于获取和添加客户数据。在get_customer方法内,我们借助UnitOfWorkCustomerRepository类,按照客户的ID来检索Customer对象。

总结一下,工作单元模式是一个非常优秀的模式,用于简化应用程序中的数据库交互。通过将数据库持久性的细节进行抽象化,并提供一个执行原子操作的简单接口,工作单元模式可以有助于确保事务的一致性,简化代码,同时支持多个数据源。

相关推荐
爱喝喜茶爱吃烤冷面的小黑黑15 分钟前
小黑一层层削苹果皮式大模型应用探索:langchain中智能体思考和执行工具的demo
python·langchain·代理模式
超级小忍22 分钟前
如何配置 MySQL 允许远程连接
数据库·mysql·adb
吹牛不交税34 分钟前
sqlsugar WhereIF条件的大于等于和等于查出来的坑
数据库·mysql
hello早上好1 小时前
BeanFactory 实现
后端·spring·架构
hshpy1 小时前
setting up Activiti BPMN Workflow Engine with Spring Boot
数据库·spring boot·后端
白宇横流学长1 小时前
基于J2EE架构的在线考试系统设计与实现【源码+文档】
java·架构·java-ee
Blossom.1181 小时前
使用Python和Flask构建简单的机器学习API
人工智能·python·深度学习·目标检测·机器学习·数据挖掘·flask
Love__Tay2 小时前
【学习笔记】Python金融基础
开发语言·笔记·python·学习·金融
文牧之2 小时前
Oracle 审计参数:AUDIT_TRAIL 和 AUDIT_SYS_OPERATIONS
运维·数据库·oracle
篱笆院的狗2 小时前
如何使用 Redis 快速实现布隆过滤器?
数据库·redis·缓存