简体   繁体   中英

Concurrent processing service

I have method that gets called from different processes, sometimes concurrently. Somewhere within that method I'm suppose to calculate discount. For this, I've created a DiscountService that reads info from DB and calculates a discount for a given customer number. As mentioned above, the main method may be called from different locations with different customer number simultaneously.

Main Method:

// this may be called simultaneously
public void Handle(IDocument document)
{
  // document contains customer number
  ...
  // update document discount property
  this._discountService.Process(document);
  // continue processing

DiscountService:

public class DiscountService : IDiscountService
{
    private readonly IDiscountDataAccess discountDataAccess;

    private readonly ILog logger;

    public DiscountService(ILog logger, IDiscountDataAccess discountDataAccess)
    {
        this.discountDataAccess = discountDataAccess;
        this.logger = logger;
    }

    public void Process(IDiscountDocument discountDocument)
    {
        this.ProcessDiscount(discountDocument);
        discountDocument.Update();
    }

    private void ProcessDiscount(IDiscountDocument discountDocument)
    {
        Discount discount = this.GetAvailableDiscount(discountDocument);
        ...
    }
    private Discount GetAvailableDiscount(IDiscountDocument discountDocument)
    {
        // Stored proc 
        return this.discountDataAccess.GetAvailableDiscount(
            discountDocument.CustomerNumber, 
            discountDocument.StartDate, 
            discountDocument.EndDate);
    }
    ...
}

My question is that should my DiscountService be thread safe? If so how would I do it for this situation. Or would I need some type of manager that would "new" DiscountService for each main method call? Or this sufficient?

thanks

EDIT: Is there a pattern for doing things like this. Having ONE service that would operate (simultaneously)?

Is there a book, pluralsight videos that talks about the designs/patterns of stuff like this? gives examples?

In general if you a reading some stuff from the DB for a given ID, and from the data you retrieve you can calculate everything you need, then there is no need for thread-safety as you don't have a shared memory access.

What's not clear from your post is whether or not the provided discountDocument is accessed from multiple threads, or if it just lives for the time out the request processing.

From what I can see, I would assume that the discountDocument needs to be made thread-safe (within the object, but this is just a guess as I don't know what else happens to it), but the service is fine as-is.

From your code you are doing dependency injection with discountDataAccess :

public DiscountService(ILog logger, IDiscountDataAccess discountDataAccess)
{
    this.discountDataAccess = discountDataAccess;
    this.logger = logger;
}

then later you make use of this injected service like here :

private Discount GetAvailableDiscount(IDiscountDocument discountDocument)
{
    // Stored proc 
    return this.discountDataAccess.GetAvailableDiscount(
        discountDocument.CustomerNumber, 
        discountDocument.StartDate, 
        discountDocument.EndDate);
}

So for me the conclusion is that the discountDataAccess must be made thread safe or at least the getavailablediscount in particular. The chance is very high that you are either going to create or reuse an existing connection and if there is not protection mechanism will run into problems.

DiscountDocument comes in via the request and I am assuming there is a different document per request, so the problem is not likely in DiscountDocument.

If however the discountDocument is indeed also a shared resource, then it might need some locking of its own and then problems get bigger and bigger because you run the risk of having a deadlock in that situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM