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)
-
0
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.
-
0
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 ?
-
0
Hi @Ricavir,
If you use [UnitOfWork(IsolationLevel.ReadUncommitted)], you can achieve what you want.
-
0
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 ?
-
0
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 } }
-
0
I'll do it this way then. Thank you @ismcagdas