/// <summary>
        /// 1- Get collection of Countries from Mongo
        ///     1.1- If collection is valid
        ///         1.1.2- Create an Empty Filter to get All Countries and create the corresponding task
        ///         1.1.3- await task result and if Result is not null return result
        ///     1.2- Else
        ///         Throw Code InvalidOperationException
        /// 2- Return null result if previous algorithm does not throw any exception and does not return any result.
        /// </summary>
        /// <returns>return all countries (a collection with single element if if exists and is supplied)
        /// or throw Exceptions :
        ///     InvalidOperationException if collection cannot be retrieved
        ///     ArgumentOutOfRangeException if objectID supplied can not be converted in MongoDB ID
        ///     System Exception as is if any other exception is thrown.
        /// </returns>
        public async Task <IEnumerable <HRCountry> > GetCountriesAsync()
        {
            IEnumerable <HRCountry>          retour     = null;
            Task <IAsyncCursor <HRCountry> > retourTask = null;

            //1-
            try
            {
                MondoDBConnexionParam        conParam   = MondoDBConnexionParamFactory.CreateMondoDBConnexionParam(_config);
                IMongoCollection <HRCountry> collection = MongoDBCollectionGetter <HRCountry> .GetCollection(conParam);

                //1.1-
                if (collection != null)
                {
                    //1.1.2-
                    FilterDefinitionBuilder <HRCountry> bld = new FilterDefinitionBuilder <HRCountry>();
                    retourTask = collection.FindAsync(bld.Empty);
                    //1.1.3-
                    await retourTask;
                    //Message IDE0067 Disposable object created by 'await retourTask' is never disposed whereas finally dispose exists ?

                    if (retourTask.Result != null)
                    {
                        //Force to list to avoid return asyn enum that can be enumerate only once.
                        retour = retourTask.Result.ToList();
                    }
                }
                //1.2-
                else
                {
                    String ErrorMessage = "Collection not found in MongoDBCountriesRepository GetCountriesAsync";
                    if (_logger != null)
                    {
                        _logger.LogError(ErrorMessage);
                    }
                    throw new InvalidOperationException(ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                if (_logger != null)
                {
                    _logger.LogError(ex.Message);
                }
                throw;
            }
            finally
            {
                if (retourTask != null)
                {
                    if (retourTask.Result != null)
                    {
                        retourTask.Result.Dispose();
                    }
                    retourTask.Dispose();
                }
            }
            //2-
            return(retour);
        }
        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="filterDefinition"></param>
        /// <returns></returns>
        private async Task <IEnumerable <Language> > GetHRLangages(FilterDefinition <HRCountry> filterDefinition)
        {
            Dictionary <String, Language> partialResult = new Dictionary <string, Language>();

            try
            {
                MondoDBConnexionParam        conParam   = MondoDBConnexionParamFactory.CreateMondoDBConnexionParam(_config);
                IMongoCollection <HRCountry> collection = MongoDBCollectionGetter <HRCountry> .GetCollection(conParam);

                if (collection != null)
                {
                    Task <IAsyncCursor <HRCountry> > retourTask = null;
                    try
                    {
                        retourTask = collection.FindAsync(filterDefinition);
                        await retourTask;
                        //Message IDE0067 Disposable object created by 'await retourTask' is never disposed whereas finally dispose exists ?
                        if (retourTask.Result != null)
                        {
                            Task <bool> cursorMovingTask = null;
                            try
                            {
                                cursorMovingTask = retourTask.Result.MoveNextAsync();
                                await cursorMovingTask;
                                while (cursorMovingTask.Result)
                                {
                                    FillUpDictionnary(partialResult, retourTask.Result.Current);
                                    cursorMovingTask.Dispose();
                                    cursorMovingTask = retourTask.Result.MoveNextAsync();
                                    await cursorMovingTask;
                                }
                            }
                            finally
                            {
                                if (cursorMovingTask != null)
                                {
                                    cursorMovingTask.Dispose();
                                }
                            }
                        }
                    }
                    finally
                    {
                        DisposeMongoDBTask(retourTask);
                    }
                }
            }
            catch (Exception ex)
            {
                if (_logger != null)
                {
                    _logger.LogError(ex.Message);
                }
                throw;
            }
            return(partialResult.Values);
        }
        /// <summary>
        /// Pick a country in collection by his ID (ALPHA 2 or 3 Code).
        /// </summary>
        /// <param name="id">The searched ID (Alpha2 or Alpha3)</param>
        /// <returns>The corrresponding HRCountry or null if not found. Can throw the following exception :
        /// </returns>
        public async Task <HRCountry> GetAsync(string id)
        {
            HRCountry retour = null;

            if (String.IsNullOrEmpty(id))
            {
                return(null);
            }
            try
            {
                String idToSearch = id.ToUpper();
                MondoDBConnexionParam        conParam   = MondoDBConnexionParamFactory.CreateMondoDBConnexionParam(_config);
                IMongoCollection <HRCountry> collection = MongoDBCollectionGetter <HRCountry> .GetCollection(conParam);

                if (collection != null)
                {
                    FilterDefinitionBuilder <HRCountry> bld        = new FilterDefinitionBuilder <HRCountry>();
                    Task <IAsyncCursor <HRCountry> >    retourTask = null;
                    try
                    {
                        retourTask = collection.FindAsync(bld.Where(country =>
                                                                    ((!String.IsNullOrEmpty(country.Alpha2Code)) && (country.Alpha2Code == idToSearch))
                                                                    ||
                                                                    ((!String.IsNullOrEmpty(country.Alpha3Code)) && (country.Alpha3Code == idToSearch))));
                        //1.1.3-
                        await retourTask;
                        //Message IDE0067 Disposable object created by 'await retourTask' is never disposed whereas finally dispose exists ?
                        if (retourTask.Result != null)
                        {
                            retour = retourTask.Result.FirstOrDefault();
                        }
                    }
                    finally
                    {
                        if (retourTask != null)
                        {
                            if (retourTask.Result != null)
                            {
                                retourTask.Result.Dispose();
                            }
                            retourTask.Dispose();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (_logger != null)
                {
                    _logger.LogError(ex.Message);
                }
                throw;
            }
            //2-
            return(retour);
        }
示例#4
0
        /// <summary>
        /// 1- Connect and Get MongoDB Collection
        /// 2- Create Filter (Empty if Region = All else Filter on Region)
        /// 3- Apply Filter on Collection and convert result to Enumerable (give up async in this pre-version) TODO
        /// 4- Release Disposable elements.
        /// </summary>
        /// <param name="region"></param>
        /// <returns>Corresponding Countries in Region or re throw any Exception raised buy MongoDB.</returns>
        public async Task <IEnumerable <HRCountry> > GetHRCountriesByContinentAsync(Region region)
        {
            IEnumerable <HRCountry> retour = null;

            try
            {
                //1-
                MondoDBConnexionParam        conParam   = MondoDBConnexionParamFactory.CreateMondoDBConnexionParam(_config);
                IMongoCollection <HRCountry> collection = MongoDBCollectionGetter <HRCountry> .GetCollection(conParam);

                if (collection != null)
                {
                    //2-
                    FilterDefinitionBuilder <HRCountry> filterDefinitionBuilder = new FilterDefinitionBuilder <HRCountry>();
                    Task <IAsyncCursor <HRCountry> >    retourTask = null;
                    try
                    {
                        FilterDefinition <HRCountry> filter = filterDefinitionBuilder.Empty;
                        if (region != Region.All)
                        {
                            filter = filterDefinitionBuilder.Where(country => country.Region == region);
                        }
                        //3-
                        retourTask = collection.FindAsync(filter);
                        await retourTask;
                        if (retourTask.Result != null)
                        {
                            retour = retourTask.Result.ToEnumerable <HRCountry>();
                        }
                    }
                    //4-
                    finally
                    {
                        if (retourTask != null)
                        {
                            retourTask.Dispose();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (_logger != null)
                {
                    _logger.LogError(ex.Message);
                }
                throw;
            }
            return(retour);
        }
        /// <summary>
        /// Get All Countries.
        /// </summary>
        /// <returns></returns>
        public async Task <IEnumerable <HRCountry> > GetFullsAsync()
        {
            IEnumerable <HRCountry> retour = null;

            //1-
            try
            {
                MondoDBConnexionParam        conParam   = MondoDBConnexionParamFactory.CreateMondoDBConnexionParam(_config);
                IMongoCollection <HRCountry> collection = MongoDBCollectionGetter <HRCountry> .GetCollection(conParam);

                //1.1-
                if (collection != null)
                {
                    //1.1.2-
                    FilterDefinitionBuilder <HRCountry> bld        = new FilterDefinitionBuilder <HRCountry>();
                    Task <IAsyncCursor <HRCountry> >    retourTask = null;
                    try
                    {
                        retourTask = collection.FindAsync(bld.Empty);


                        FilterDefinitionBuilder <HRCountry> builder = new FilterDefinitionBuilder <HRCountry>();
                        builder.Eq <Region>((HRCountry x) => x.Region, Region.Africa);
                        //1.1.3-
                        await retourTask;
                        if (retourTask.Result != null)
                        {
                            //Force to list to avoid return asyn enum that can be enumerate only once.
                            retour = retourTask.Result.ToList();
                        }
                    }
                    finally
                    {
                        if (retourTask != null)
                        {
                            if (retourTask.Result != null)
                            {
                                retourTask.Result.Dispose();
                            }
                            retourTask.Dispose();
                        }
                    }
                }
                else
                {
                    String ErrorMessage = "No Collection found in MongoDBCountriesRepository GetFullsAsync";
                    if (_logger != null)
                    {
                        _logger.LogError(ErrorMessage);
                    }
                    throw new InvalidOperationException(ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                if (_logger != null)
                {
                    _logger.LogError(ex.Message);
                }
                throw;
            }
            //2-
            return(retour);
        }