static void Main(string[] args)
        {
            ITimetableRepository repo = new TimetableRepository(new WebConfigConnectionStringRepository());
            IList<Loadtest> loadtestsInPeriod = repo.GetLoadtestsForTimePeriod(DateTime.UtcNow.AddYears(-1), DateTime.UtcNow.AddYears(1));
            List<Loadtest> toBeInserted = new List<Loadtest>();
            List<Loadtest> toBeUpdated = new List<Loadtest>();
            Loadtest ltNewOne = new Loadtest
                (Guid.NewGuid(),
                new LoadtestParameters(DateTime.UtcNow.AddDays(1), 100, 90),
                Guid.Parse("52d4e276-193d-4ff3-a40e-c45381969d24"),
                Guid.Parse("cb5c3463-d1cb-4c1c-b667-f1c6a065edd1"),
                Guid.Parse("c1cf1179-98a7-4c58-bcaa-a0e7c697293f"),
                Guid.Parse("612cf872-3967-41e7-a30d-28e26df66dcc"),
                Guid.Parse("c3d79996-7045-4bce-a6cd-fcf398717ae5"),
                Guid.Parse("4d27ad00-14d8-4c1c-98b9-64556e957daf"));
            Loadtest ltNewTwo = new Loadtest
                (Guid.NewGuid(),
                new LoadtestParameters(DateTime.UtcNow.AddDays(5), 500, 300),
                Guid.Parse("b9b42875-414f-46b9-8dd2-417668e23e83"),
                Guid.Parse("f966ccf4-7802-4796-8767-637611b611de"),
                Guid.Parse("a4ae54f4-e662-4922-a2ce-4df9af9d23c8"),
                Guid.Parse("95202f85-0c8e-426b-b0ea-ed74f4d1ccbc"),
                Guid.Parse("1e4871ba-de8b-4e2c-98f4-3364b9d82558"),
                Guid.Parse("4d27ad00-14d8-4c1c-98b9-64556e957daf"));
            toBeInserted.Add(ltNewOne);
            toBeInserted.Add(ltNewTwo);

            Loadtest ltUpdOne = new Loadtest
            (Guid.Parse("71b29573-8f67-49ab-8ee0-f8dd0bbceefd"),
            new LoadtestParameters(DateTime.UtcNow.AddDays(14), 50, 300),
            Guid.Parse("52d4e276-193d-4ff3-a40e-c45381969d24"),
            Guid.Parse("5b16880e-b0dd-4d66-bff9-f79eba6490ec"),
            Guid.Parse("40ccb6aa-c9a6-466d-be02-c73122d6edca"),
            Guid.Parse("612cf872-3967-41e7-a30d-28e26df66dcc"),
            Guid.Parse("e2caa1f0-2ee9-4e8f-86a0-51de8aba4eca"),
            Guid.Parse("4d27ad00-14d8-4c1c-98b9-64556e957daf"));
            toBeUpdated.Add(ltUpdOne);

            AddOrUpdateLoadtestsValidationResult validationRes = new AddOrUpdateLoadtestsValidationResult(toBeInserted, toBeUpdated,
                new List<Loadtest>(), "Validation summary");
            repo.AddOrUpdateLoadtests(validationRes);

            //TestSelectWithWhereClause();
            //TestReplacement();
            //Seed();
            /*
            string mongoDbConnectionString = "mongodb://localhost:27017";
            MongoClient mongoClient = new MongoClient(mongoDbConnectionString);
            IMongoDatabase testDatabase = mongoClient.GetDatabase("Cartoons");
            Task t = testDatabase.CreateCollectionAsync("Disney");
            Task.WaitAll(t);
            //Task.Run(() => testDatabase.CreateCollectionAsync("Disney"));*/
        }
        public AddOrUpdateLoadtestsValidationResult AddOrUpdateLoadtests(IList <Loadtest> loadtestsAddedOrUpdated)
        {
            List <Loadtest> toBeInserted         = new List <Loadtest>();
            List <Loadtest> toBeUpdated          = new List <Loadtest>();
            List <Loadtest> failed               = new List <Loadtest>();
            StringBuilder   resultSummaryBuilder = new StringBuilder();
            string          NL = Environment.NewLine;

            foreach (Loadtest loadtest in loadtestsAddedOrUpdated)
            {
                Loadtest existing = (from l in Loadtests where l.Id == loadtest.Id select l).FirstOrDefault();
                if (existing != null)                 //update
                {
                    LoadtestValidationSummary validationSummary = OkToAddOrModify(loadtest);
                    if (validationSummary.OkToAddOrModify)
                    {
                        existing.Update
                            (loadtest.Parameters, loadtest.AgentId, loadtest.CustomerId,
                            loadtest.EngineerId, loadtest.LoadtestTypeId, loadtest.ProjectId, loadtest.ScenarioId);
                        toBeUpdated.Add(existing);
                        resultSummaryBuilder.Append(string.Format("Load test ID {0} (update) successfully validated.{1}", existing.Id, NL));
                    }
                    else
                    {
                        failed.Add(loadtest);
                        resultSummaryBuilder.Append(string.Format("Load test ID {0} (update) validation failed: {1}{2}.",
                                                                  existing.Id, validationSummary.ReasonForValidationFailure, NL));
                    }
                }
                else                 //insertion
                {
                    LoadtestValidationSummary validationSummary = OkToAddOrModify(loadtest);
                    if (validationSummary.OkToAddOrModify)
                    {
                        Loadtests.Add(loadtest);
                        toBeInserted.Add(loadtest);
                        resultSummaryBuilder.Append(string.Format("Load test ID {0} (insertion) successfully validated.{1}", loadtest.Id, NL));
                    }
                    else
                    {
                        failed.Add(loadtest);
                        resultSummaryBuilder.Append(string.Format("Load test ID {0} (insertion) validation failed: {1}{2}.",
                                                                  loadtest.Id, validationSummary.ReasonForValidationFailure, NL));
                    }
                }
            }
            AddOrUpdateLoadtestsValidationResult validationResult = new AddOrUpdateLoadtestsValidationResult(toBeInserted, toBeUpdated, failed, resultSummaryBuilder.ToString());
            TimetableChangedEventArgs            args             = new TimetableChangedEventArgs(validationResult);

            DomainEventMediator.RaiseEvent(args);
            return(validationResult);
        }
 public AddOrUpdateLoadtestsValidationResult AddOrUpdateLoadtests(IList<Loadtest> loadtestsAddedOrUpdated)
 {
     List<Loadtest> toBeInserted = new List<Loadtest>();
     List<Loadtest> toBeUpdated = new List<Loadtest>();
     List<Loadtest> failed = new List<Loadtest>();
     StringBuilder resultSummaryBuilder = new StringBuilder();
     string NL = Environment.NewLine;
     foreach (Loadtest loadtest in loadtestsAddedOrUpdated)
     {
         Loadtest existing = (from l in Loadtests where l.Id == loadtest.Id select l).FirstOrDefault();
         if (existing != null) //update
         {
             LoadtestValidationSummary validationSummary = OkToAddOrModify(loadtest);
             if (validationSummary.OkToAddOrModify)
             {
                 existing.Update
                     (loadtest.Parameters, loadtest.AgentId, loadtest.CustomerId,
                         loadtest.EngineerId, loadtest.LoadtestTypeId, loadtest.ProjectId, loadtest.ScenarioId);
                 toBeUpdated.Add(existing);
                 resultSummaryBuilder.Append(string.Format("Load test ID {0} (update) successfully validated.{1}", existing.Id, NL));
             }
             else
             {
                 failed.Add(loadtest);
                 resultSummaryBuilder.Append(string.Format("Load test ID {0} (update) validation failed: {1}{2}.",
                     existing.Id, validationSummary.ReasonForValidationFailure, NL));
             }
         }
         else //insertion
         {
             LoadtestValidationSummary validationSummary = OkToAddOrModify(loadtest);
             if (validationSummary.OkToAddOrModify)
             {
                 Loadtests.Add(loadtest);
                 toBeInserted.Add(loadtest);
                 resultSummaryBuilder.Append(string.Format("Load test ID {0} (insertion) successfully validated.{1}", loadtest.Id, NL));
             }
             else
             {
                 failed.Add(loadtest);
                 resultSummaryBuilder.Append(string.Format("Load test ID {0} (insertion) validation failed: {1}{2}.",
                     loadtest.Id, validationSummary.ReasonForValidationFailure, NL));
             }
         }
     }
     AddOrUpdateLoadtestsValidationResult validationResult = new AddOrUpdateLoadtestsValidationResult(toBeInserted, toBeUpdated, failed, resultSummaryBuilder.ToString());
     TimetableChangedEventArgs args = new TimetableChangedEventArgs(validationResult);
     DomainEventMediator.RaiseEvent(args);
     return validationResult;
 }
        public void AddOrUpdateLoadtests(AddOrUpdateLoadtestsValidationResult addOrUpdateLoadtestsValidationResult)
        {
            LoadTestingContext context = LoadTestingContext.Create(base.ConnectionStringRepository);
            if (addOrUpdateLoadtestsValidationResult.ValidationComplete)
            {
                if (addOrUpdateLoadtestsValidationResult.ToBeInserted.Any())
                {
                    IEnumerable<LoadtestMongoDb> toBeInserted = addOrUpdateLoadtestsValidationResult.ToBeInserted.PrepareAllForInsertion();
                    context.Loadtests.InsertMany(toBeInserted);
                }

                if (addOrUpdateLoadtestsValidationResult.ToBeUpdated.Any())
                {
                    foreach (Loadtest toBeUpdated in addOrUpdateLoadtestsValidationResult.ToBeUpdated)
                    {
                        Guid existingLoadtestId = toBeUpdated.Id;
                        var loadtestInDbQuery = context.Loadtests.Find<LoadtestMongoDb>(lt => lt.DomainId == existingLoadtestId);
                        LoadtestMongoDb loadtestInDb = loadtestInDbQuery.SingleOrDefault();
                        loadtestInDb.AgentId = toBeUpdated.AgentId;
                        loadtestInDb.CustomerId = toBeUpdated.CustomerId;
                        loadtestInDb.EngineerId = toBeUpdated.EngineerId;
                        loadtestInDb.LoadtestTypeId = toBeUpdated.LoadtestTypeId;
                        LoadtestParameters ltDomainParameters = toBeUpdated.Parameters;
                        loadtestInDb.Parameters.DurationSec = ltDomainParameters.DurationSec;
                        loadtestInDb.Parameters.ExpectedEndDateUtc = ltDomainParameters.StartDateUtc.AddSeconds(ltDomainParameters.DurationSec);
                        loadtestInDb.Parameters.StartDateUtc = ltDomainParameters.StartDateUtc;
                        loadtestInDb.Parameters.UserCount = ltDomainParameters.UserCount;
                        loadtestInDb.ProjectId = toBeUpdated.ProjectId;
                        loadtestInDb.ScenarioId = toBeUpdated.ScenarioId;
                        context.Loadtests.FindOneAndReplace<LoadtestMongoDb>(lt => lt.DbObjectId == loadtestInDb.DbObjectId, loadtestInDb);
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("Validation is not complete. You have to call the AddOrUpdateLoadtests method of the Timetable class first.");
            }
        }