static void Main(string[] args)
 {
     if (args.Length == 1)
     {
         _fileName = args[0];
     }
     using (var engine = new FileHelperAsyncEngine <ImportPerson>())
     {
         engine.BeginReadFile(_fileName);
         int success   = 0;
         int failed    = 0;
         var stopwatch = new Stopwatch();
         stopwatch.Start();
         using (var uow = new EFUnitOfWorkFactory().Create())
         {
             foreach (ImportPerson importPerson in engine)
             {
                 try
                 {
                     var person = new Person();
                     Mapper.Map(importPerson, person);
                     TryAddEmailAddress(importPerson, person);
                     TryAddPhoneNumber(importPerson, person);
                     person.HomeAddress = FixAddress(person.HomeAddress, changeMissingDataToNull: false); // False sets missing data to <unknown>.
                     person.WorkAddress = FixAddress(person.WorkAddress, changeMissingDataToNull: false); // False sets missing data to <unknown>.
                     if (!person.Validate().Any())
                     {
                         success++;
                         PeopleRepository.Add(person);
                         if (success % 30 == 0)
                         {
                             uow.Commit(true);
                         }
                     }
                     else
                     {
                         //TODO Handle invalid data. Log it, or deal with it in some way.
                         failed++;
                     }
                 }
                 catch (Exception ex)
                 {
                     //TODO Handle error. Log it, or deal with it in some way.
                     Console.WriteLine("Failed to import row " + engine.LineNumber);
                     Console.WriteLine("Error " + ex.Message);
                     failed++;
                 }
                 long   elapsed    = stopwatch.ElapsedMilliseconds;
                 double timeForOne = (elapsed / (double)success) / 1000;
                 double perSecond  = 1 / timeForOne;
                 UpdateUI(success, failed, elapsed, perSecond, importPerson);
             }
         }
         stopwatch.Stop();
         engine.Close();
     }
 }
Esempio n. 2
0
        public void ValidationErrorsThrowModelValidationException()
        {
            var    uow = new EFUnitOfWorkFactory().Create();
            Action act = () =>
            {
                var repo = new PeopleRepository();
                repo.Add(new Person());
                uow.Commit(true);
            };

            act.ShouldThrow <ModelValidationException>().WithMessage("The FirstName field is required", ComparisonMode.Substring);
            uow.Undo();
        }
 public void CanExecuteQueryAgainstDataContext()
 {
     using (var uow = new EFUnitOfWorkFactory().Create())
     {
         var    context  = DataContextFactory.GetDataContext();
         string username = Guid.NewGuid().ToString().Substring(0, 10);
         var    user     = new User {
             UserName = username, Email = "*****@*****.**", PasswordHash = "123456"
         };
         context.Users.Add(user);
         uow.Commit(false);
         var personCheck = context.Users.SingleOrDefault(x => x.UserName == username);
         personCheck.Should().NotBeNull();
         context.Users.Remove(user);
     }
 }