示例#1
0
        public static void Shuffle <T>(this List <T> list)
        {
            int n = list.Count;

            while (n > 1)
            {
                n--;
                int k     = RandomHelpers.Next(n + 1);
                T   value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }
示例#2
0
        /// <summary>
        /// Shuffles an IList using Cryptographically secure randomization
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static IList <T> Shuffle <T>(this IList <T> source)
        {
            if (source.Count == 0)
            {
                return(Activator.CreateInstance <IList <T> >());
            }

            var destination = source.DeepCloneObject() as IList <T>;

            var length = destination?.Count() ?? 0;

            for (var currentIndex = 0; currentIndex < length; currentIndex++)
            {
                var swapIndex = RandomHelpers.Next(length);
                destination.Swap(currentIndex, swapIndex);
            }

            return(destination);
        }
        /// <summary>
        /// Shuffles the specified source.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The source.</param>
        /// <returns>IEnumerable&lt;T&gt;.</returns>
        public static IEnumerable <T> Shuffle <T>(this IEnumerable <T> source)
        {
            var shuffled = source.OrderBy(src => RandomHelpers.Next(source.Count()));

            return(shuffled);
        }
示例#4
0
        /// <summary>
        /// Generates the seed data.
        /// </summary>
        /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
        private void GenerateSeedData(bool overwrite = false)
        {
            if (overwrite == false)
            {
                return;
            }

            Log.Debug($"Generating seed data with overwrite set to {true}");

            Directory.CreateDirectory(BasePathGeneratedSeed);

            #region -- Generate Seed for Speciality --

            List <Speciality> specialityList = SeedHelper.ParseSourceFile <Speciality>(SpecialitiesFileName);

            var specialitiesJsonString = JsonConvert.SerializeObject(specialityList, new JsonSerializerSettings {
                Formatting = Formatting.Indented
            });

            SeedHelper.SaveOrOverwriteGeneratedFile(SpecialitiesFileName, specialitiesJsonString, true);

            #endregion

            #region -- Generate Seed for doctors --

            List <string> firstNames = SeedHelper.ParseSourceFile <string>(FirstNamesFileName)
                                       .Select(fn => fn.Trim()).Distinct().Shuffle().ToList();

            List <string> middleNames = SeedHelper.ParseSourceFile <string>(MiddleNamesFileName)
                                        .Select(mn => mn.Trim()).Distinct().Shuffle().ToList();

            List <string> lastNames = SeedHelper.ParseSourceFile <string>(LastNamesFileName)
                                      .Select(ln => ln.Trim()).Distinct().Shuffle().ToList();

            var doctorList = new List <Doctor>();

            for (int i = 1; i < 500; i++)
            {
                doctorList.Add(GetRandomDoctor(i));
            }

            var doctorListJsonString = JsonConvert.SerializeObject(doctorList, _defaultJsonSerializerSettings);

            SeedHelper.SaveOrOverwriteGeneratedFile(DoctorFileName, doctorListJsonString, true);

            // Local function
            Doctor GetRandomDoctor(int doctorID)
            {
                return(new Doctor
                {
                    DoctorID = doctorID,
                    SpecialityID = specialityList.GetRandomShuffled().SpecialtyID,
                    Firstname = firstNames.GetRandomShuffled(),
                    Middlename = middleNames.GetRandomShuffled(),
                    Lastname = lastNames.GetRandomShuffled()
                });
            }

            #endregion

            #region -- Generate Seed for Addresses --

            List <Address> addressList = SeedHelper.ParseSourceFile <Address>(AddressesFileName);

            var addressJsonString = JsonConvert.SerializeObject(addressList, _defaultJsonSerializerSettings);

            SeedHelper.SaveOrOverwriteGeneratedFile(AddressesFileName, addressJsonString, true);

            #endregion

            #region -- Generate Seed for Hospital --

            List <Hospital> hospitalList = SeedHelper.ParseSourceFile <Hospital>(HospitalsFileName);

            var hospitalJsonString = JsonConvert.SerializeObject(hospitalList, _defaultJsonSerializerSettings);

            SeedHelper.SaveOrOverwriteGeneratedFile(HospitalsFileName, hospitalJsonString, true);

            #endregion

            #region -- Generate Seed for HospitalDoctor --

            List <HospitalDoctor> hospitalDoctorList = new List <HospitalDoctor>();

            for (int i = 1; i < 10000; i++)
            {
                hospitalDoctorList.Add(GetRandomHospitalDoctor(i));
            }

            HospitalDoctor GetRandomHospitalDoctor(int hospitalDoctorID)
            {
                return(new HospitalDoctor
                {
                    HospitalDoctorID = hospitalDoctorID,
                    HospitalID = hospitalList.GetRandomShuffled().HospitalID,
                    DoctorID = doctorList.GetRandomShuffled().DoctorID
                });
            }

            var hospitalDoctorJsonString = JsonConvert.SerializeObject(hospitalDoctorList, _defaultJsonSerializerSettings);

            SeedHelper.SaveOrOverwriteGeneratedFile(HospitalDoctorsFileName, hospitalDoctorJsonString, true);

            #endregion

            #region  -- Generate Seed for HospitalSpeciality --

            List <HospitalSpeciality> hospitalSpecialities = new List <HospitalSpeciality>();

            int countHospitalSpeciality = 1;

            hospitalList.ForEach(hospital =>
            {
                var randomNumberOfSpecialities = RandomHelpers.Next(specialityList.Count);

                for (int i = 0; i < randomNumberOfSpecialities; i++)
                {
                    var hs = new HospitalSpeciality()
                    {
                        HospitalSpecialityID = countHospitalSpeciality++,
                        HospitalID           = hospital.HospitalID,
                        SpecialtyID          = specialityList.GetRandomShuffled().SpecialtyID
                    };

                    hospitalSpecialities.Add(hs);
                }
            });

            int currentHs = 1;

            hospitalSpecialities = hospitalSpecialities
                                   .DistinctBy(hs => new { hs.HospitalID, hs.SpecialtyID })
                                   .Select(hs => new HospitalSpeciality()
            {
                HospitalSpecialityID = currentHs++,
                HospitalID           = hs.HospitalID,
                SpecialtyID          = hs.SpecialtyID
            })
                                   .ToList();

            var hospitalSpecialitiesJsonString = JsonConvert.SerializeObject(hospitalSpecialities, _defaultJsonSerializerSettings);

            SeedHelper.SaveOrOverwriteGeneratedFile(HospitalSpecialityFileName, hospitalSpecialitiesJsonString, true);


            #endregion

            Log.Debug("Completed generation of seed data");
        }