Scenario
Orders, articles or other business entities often require that you have user-friendly Id or Code fields that end-users can memorize and use during phone conversations. They are usually sequential, but some gaps can be allowed as well (e.g., when an order is deleted). Refer to this StackOverFlow thread for more information on this common scenario, and a possible implementation.
Steps to implement
1. Add a new business class to your platform-agnostic module, and override its AfterConstruction method, as shown in the Solution2.Module\BusinessObjects\DomainObject1.xx file;
2. Build your platform-agnostic project and double-click on the Solution2.Module\Module.xx file to invoke the Module Designer;
3. Refer to the Exported Types section within the designer and expand the Referenced Assemblies | DevExpress.Persistent.BaseImpl node;
4. Select and press the space bar on the OidGenerator and ServerPrefix nodes to include these types into the business model of your module:
IMPORTANT NOTES
1. The DistributedIdGeneratorHelper class demonstrated in this solution creates the IDGeneratorTable and ServerPrefix tables to store the information about the last sequential number of a type.
Although this particular solution is simpler to implement than the How to generate and assign a sequential number for a business object within a database transaction, while being a part of a successful saving process (XAF) example, it is pretty safe and should also work in the most typical business scenarios (except for the middle-tier Application Server scenario, because the Session.DataLayer property is not initialized in this case).
2. You can find functional EasyTest scripts for this scenario in the Solution2.Module\FunctionalTests\E4904.ets file.
Example Comments
Added By: Gareth- at: 10/2/2013 8:58:17 AM
Thanks, works perfectly - I've seeded the ID by manually changing the [Oid] in the IDGeneratorTable, do I need to worry about the [OptimisicLockField] or will this update automatically?
Added By: Dennis (DevExpress Support) at: 10/2/2013 10:10:50 AM@Gareth: No, you do not need to care about the OptimisticLockField.
Added By: MohammedFarooq at: 12/2/2013 4:07:31 AMHi,
The code is really helpful but it increments the value of code even if the record is deleted! How can we stop the code to increase its value when a record is deleted?
Added By: MohammedFarooq at: 12/2/2013 4:25:16 AMI manage to figure out the solution. I have just added the IsDeleted checking and it worked like a charm!!!
Protected Overrides Sub OnSaving()
If (Not IsDeleted) Then
Me.codeCore = String.Format("N{0:D6}", DistributedIdGeneratorHelper.Generate(Me.Session.DataLayer, Me.GetType().FullName, String.Empty))
End If
MyBase.OnSaving()
End Sub
@Mohammed: Yes, your solution is correct. I will consider updating this example accordingly. Thanks for your input in this regard.