开发者

How to provide Unique ID in Offline mode?

In a client-server accounting application in invoice form when a user saves an invoce it gets An invoice number like 90134 from server and saves the invoice with that number

The invoice number is needed for the customer.

So in Offline mode (like when the network dropped) how provide a unique id?

Is it good to use String Id like this pattern: client + incremental number?

I don't want to use开发者_如何转开发 GUIDs.


If you know in advance how many invoice numbers you will generate per client during an offline period, would you be able to pre-allocate invoice numbers? e.g. if each client is likely only to generate 4 invoices per offline period, you could allocate a block of 4 numbers to each client. This may involve an extra column in your DB to store a value indicating whether the number is an invoice already created, or a preallocation of a number. Depending on the structure and constraints within your DB, you may also need to store some dummy data to enforce referential integrity.

The downsides would be that your block of numbers may not get used sequentially, or indeed at all, so your invoice numbers would not be in chronological order. Also, you would run into problems if the pool of available numbers is used up.


You can use Guid:

var myUniqueID = Guid.NewID();

In SQL server is corresponding type uniqueidentifier.

In general the Guid is 128-bit number.

More about Guid you can read:

http://en.wikipedia.org/wiki/Globally_unique_identifier

http://msdn.microsoft.com/en-us/library/system.guid.aspx


I suppose the invoice number (integer) is incremental: in this case, since you have no way of knowing the last invoice number, you could save the invoice in a local db/cache/xml without the invoice Number and wait for the network connection to insert the new records in the DB (the invoice number would be generated then)


You could start your numbers for each client at a different range... e.g.:

  • client 1: 1,000,000
  • client 2: 2,000,000
  • client 3: 3,000,000

Update them every now and then when there is a connection to avoid overlaps.
It's not 100% bulletproof, but at least it's better than nothing.

My favorite would still be a GUID for this, since they're always unique.


There is a workaround, but it is merely a "dirty hack", you should seriously reconsider accepting new data entries while offline, especially when dealing with unique IDs to be inserted in many tables.

Say you have an "orders" table and another "orderDetails" table in your local dataset:

1- add a tmpID of type integer in your "orders" table to temporarily identify each unique order.

2- use the tmpID of your newly created order in the rest of the process (say for adding products to the current order in the orderDetails table)

--> once you are connected to the server, in a single transaction do the following

1- insert the first order in the "orders" table

2- get its uniqueID generated on your SQL server

3- search for each line in "orderDetails" that have a tmpID of currentOrder.tmpID and insert them in the "orderDetails" table on your server

4- commit the transaction and continue to the following row.

Keep in mind that this is very bad coding and that it can get real dirty and hard to maintain.


it looks like impossible to create unique numbers with two different systems both offline when it must be chronological and without missing numbers.

imho there is no way if the last number (on the server) was 10, to know if i should return 11 or 12; i would have to know if 11 was already used by another person.

I can only imagine to use a temporary number and later on renumber those numbers, but if the invoices are printed and the number can not be changed, i don't know how you could accomplish such a solution.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜