Base solution for your next web application
Open Closed

Continuous ID counting on tenant entity #4016


User avatar
0
Ricavir created

Hi,

I need to have a unique and continuous ID per tenant. I'm building an invoicing module and each tenant needs to have continuous ID's for their invoices (eg : 1,2,3,4,5...). I can't have any number missing in this serie (like 1,2,4,5 >> this must not be possible) I can't use the primary key of invoice table has it is shared by all tenants.

What would you recommend to do this ?

I was planning to implement this in Invoice AppService while creating an invoice entity. Before creating the entity, I plan to find the highest ID for current tenant and increment it ; then save entity. Is it the righ approach or do you have other suggestions ?

Tks


6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Ricavir,

    You are right, using primary key is not a good idea. You can add a new field (InvoiceId for example) and query last number generated for that tenant and then you can use +1 of that number for new invoice.

    Also consider locking while querying and inserting records on your method, otherwise duplicate numbers may appear on your DB.

  • User Avatar
    0
    Ricavir created

    Thks for quick answer.

    You mentioned to lock the process ; I totaly agree. As the process operation is being done in AppService, this should be safe by Unit Of Work concept no ?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Ricavir,

    If you use [UnitOfWork(IsolationLevel.ReadUncommitted)], you can achieve what you want.

  • User Avatar
    0
    Ricavir created

    tks @ismcagdas

    To be clear, do you mean that I need to add

    CurrentUnitOfWork.Options.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
    

    in each appservice method where I generate the ID ?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Ricavir,

    If you are using invoice number generation logic in more than one app service, you can move it to a domain service and then you can add [UnitOfWork(IsolationLevel.ReadUncommitted)] attribute on invoice number generation method.

    For example:

    public class MyInvoiceNumberGenerator : IMyInvoiceNumberGenerator {
    
         [UnitOfWork(IsolationLevel.ReadUncommitted)]
        public virtual int Generate(){
            //generate new number here
        }
    
    }
    
  • User Avatar
    0
    Ricavir created

    I'll do it this way then. Thank you @ismcagdas