开发者

Design Pattern - Using one service to logically acquire/release resources from an other service

开发者 https://www.devze.com 2023-03-06 08:54 出处:网络
I\'m having a service, which provides access to a database. This service is used by multiple applications.

I'm having a service, which provides access to a database. This service is used by multiple applications. One of that applications needs special functionality. There should only be onl开发者_运维问答y one (administrative) user, which can manipulate data simultaneously. This application also communicates with an other service. I want to extend the functionality of this second service with two methods:

bool Acquire()
    1. Check if an other user currently holds ownership.
        1.1 if true => return false (acquirement failed).
        1.2 if false => proceed.
    2. set ownership to client connection at server side.
    3. return true.

void Release()
    1. Check if the client connection currently holds ownership.
        1.1 if true => remove ownership of client connection at server side.
        1.2 if false => do nothing.

My question is: Is this a good design pattern? In my opinion, I should not change the database service for only one special application requirement.


Agreed, this sounds like a leaky abstraction. I think it would be more appropriate to turn this into a security/authorization problem.

Define a special security token that can be granted to only one user at a time.

Now you can create a Decorator around your data access Gateway that checks whether that particular security token is part of the current security context.

By separating concerns into separate classes you stay true to the SOLID principles.

0

精彩评论

暂无评论...
验证码 换一张
取 消