Esempio n. 1
0
        // 'GetHashCode() for String' does not guarantee unique code for different strings, but, nevertheless, the chance of collisions is not so high.
        // So, I use it and try to write a value to the DB, given the presence of an index in the database,
        // which will throw an error  if will getting collision and code will try again with a new id.
        private async Task <string> GenerateShortAlias(string fullLink, string userId)
        {
            var tryCount = 0;

            while (tryCount < 3)
            {
                try
                {
                    var id         = ObjectId.GenerateNewId().ToString();
                    var shortAlias = _shortenerService.GenerateShortString(id.GetHashCode());

                    var newLink = new Link(id, userId, shortAlias, fullLink);

                    await _linkRepository.CreateNewLink(newLink);

                    return(newLink.ShortAlias);
                }
                catch (MongoDB.Driver.MongoDuplicateKeyException)
                {
                    tryCount++;
                }
            }

            throw new InvalidOperationException("Three tries not enough for resolve collisions!");
        }