public static IDGenerator GetMemberIDGenerator(ApplicationDbContext dbContext) { if (_memberIDGenerator == null) { lock (typeof(IDGenerator)) { if(_memberIDGenerator == null) { var lastMemberID = dbContext.Members .OrderByDescending(m => m.RegisterDate) .Take(1) .Select(m => m.MemberID) .FirstOrDefault(); _memberIDGenerator = IDGenerator.CreateFromID(lastMemberID, "BE"); } } } return _memberIDGenerator; }
public static IDGenerator GetSaleToCustomerIDGenerator(ApplicationDbContext dbContext) { if (_saleToCustomerIDGenerator == null) { lock (typeof(IDGenerator)) { if (_saleToCustomerIDGenerator == null) { var lastMemberID = dbContext.SaleToCustomer .OrderByDescending(m => m.CreateDate) .Take(1) .Select(m => m.ID) .FirstOrDefault(); _saleToCustomerIDGenerator = IDGenerator.CreateFromID(lastMemberID, "STC"); } } } return _saleToCustomerIDGenerator; }
private static IDGenerator CreateFromID(string lastID, string prefix) { IDGenerator g = new IDGenerator(prefix, 1); if (string.IsNullOrWhiteSpace(lastID) == false) { string newPrefix = g.GetPrefix(); if (lastID.StartsWith(newPrefix)) { int currIdx; int.TryParse(lastID.Substring(newPrefix.Length), out currIdx); if(currIdx > 0) { g.SetNextIdx(currIdx + 1); } } } return g; }