How to use a number sequence

Now that we have created a number sequence, we need to use it to generate IDs for us.We need to use the global DataProviderFactory to get IDs from a sequence. The DataProviderFactory has the method GenerateNumber(..) that returns the next ID of a given sequence.

Here we see sample code where the ID of a bar code setup is being generated: (This sample code is from: SqlDataProviders.BarCodes.BarCodeSetupData.Save(..))

barCodeSetup.ID = DataProviderFactory.Instance.GenerateNumber<IBarCodeSetupData, BarCodeSetup>(entry);

The GenerateNumber(..) function takes an object as a parameter that implements the ISequenceable interface and another parameter that is the business object that relates to that data provider. This interface has two methods, one that returns the ID of the sequence and another one that makes sure that the ID that we are generating does not already exist (maybe somebody manually created it in a database table).

 

Here we see the part of the BarCodeSetupData class that we need for the number sequence.

Here we have a class that implements the ISequenceable interface through the IBarcodeSetupData interface. The first function returns a Boolean value telling us if the ID that we are trying to create exists in the database and the second function gives us the ID of the sequence (here BARCODESETUP).


public class BarCodeSetupData : SqlServerDataProviderBase, IBarCodeSetupData
{
	...
							
	public virtual bool Exists(IConnectionManager entry, RecordIdentifier barCodeSetupID)
	{
		return RecordExists(entry, "BARCODESETUP", "BARCODESETUPID", barCodeSetupID);
	}

	#region ISequenceable Members

	public virtual bool SequenceExists(IConnectionManager entry, RecordIdentifier id)
	{
		return Exists(entry, id);
	}

	public RecordIdentifier SequenceID
	{
		get { return "BARCODESETUP"; }
	}

	#endregion
}

To link a number sequence to a business object, the ID of the number sequence must be the same as the SequenceID property implemented from the ISequenceable interface.

 

Have you finished the tasks in the current lesson: Lesson 7 - Number sequences