// Parametre olarak gönderilen entity'i repository'e ekler ve unit of work üzerinden veritabanına kaydetme işlemini gerçekleştirir
 public virtual TEntity AddEntity(TEntity entity, bool saveChanges = true)
 {
     try
     {
         repository.AddEntity(entity);
         if (saveChanges)
         {
             unitOfWork.SaveChanges();
         }
         return(entity);
     }
     catch (Exception exc)
     {
         throw exc;
     }
 }
        public void CreateContact()
        {
            int id = 0;

            using (IDataContextAsync db = new DBTopAtlantaContext())
            using (IUnitOfWorkAsync unitOfWork = new UnitOfWorkBase(db))
            {
                IContactService contactService = new ContactService(unitOfWork.RepositoryAsync<Contact>());

                var contact = new Contact
                {
                    FirstName = "Mike",
                    LastName = "Smyth",
                    Gender = "Male",
                    CreateDate = DateTime.Now,
                    CreatedBy = "test",
                };

                contactService.Insert(contact);
                id = unitOfWork.SaveChanges();
            }

            //Select
            using (IDataContextAsync db = new DBTopAtlantaContext())
            using (IUnitOfWorkAsync unitOfWork = new UnitOfWorkBase(db))
            {
                IContactService contactService = new ContactService(unitOfWork.RepositoryAsync<Contact>());
                var contact = contactService.GetContactByName("Mike", "Smyth").First();
                id = contact.ContactId;
                Assert.IsNotNull(contact);
            }

            //Update
            using (IDataContextAsync db = new DBTopAtlantaContext())
            using (IUnitOfWorkAsync unitOfWork = new UnitOfWorkBase(db))
            {
                IContactService contactService = new ContactService(unitOfWork.RepositoryAsync<Contact>());
                var contact = contactService.Find(id);

                contact.Birthday = DateTime.Parse("10/01/1971");
                contactService.Update(contact);
                unitOfWork.SaveChanges();

            }

            //Check and Delete
            using (IDataContextAsync db = new DBTopAtlantaContext())
            using (IUnitOfWorkAsync unitOfWork = new UnitOfWorkBase(db))
            {
                IContactService contactService = new ContactService(unitOfWork.RepositoryAsync<Contact>());
                var contact = contactService.Find(id);

                Assert.AreEqual(DateTime.Compare(DateTime.Parse(contact.Birthday.ToString()), DateTime.Parse("10/01/1971")),0);
                contactService.Delete(contact);
                unitOfWork.SaveChanges();

            }
        }
        public void DeleteContactById()
        {
            using (IDataContextAsync fakeDbContext = new UnitTestFakeDbContext())
            using (IUnitOfWork unitOfWork = new UnitOfWorkBase(fakeDbContext))
            {
                unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 1, ObjectState = ObjectState.Added });

                unitOfWork.SaveChanges();

                unitOfWork.Repository<Contact>().Delete(1);

                unitOfWork.SaveChanges();

                var contact = unitOfWork.Repository<Contact>().Find(1);

                Assert.IsNull(contact);
            }
        }
        public void FindContactServiceById()
        {
            using (IDataContextAsync fakeDbContext = new UnitTestFakeDbContext())
            using (IUnitOfWorkAsync unitOfWork = new UnitOfWorkBase(fakeDbContext))
            {
                IContactService contactService = new ContactService(unitOfWork.RepositoryAsync<Contact>());

                contactService.Insert(new Contact { ContactId = 1, ObjectState = ObjectState.Added });
                unitOfWork.SaveChanges();

                var contact = contactService.Find(1);
                Assert.IsNotNull(contact);
                Assert.AreEqual(1, contact.ContactId);
            }
        }
        public void FindContactById()
        {
            using (IDataContextAsync fakeDbContext = new UnitTestFakeDbContext())
            using (IUnitOfWork unitOfWork = new UnitOfWorkBase(fakeDbContext))
            {
                unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 1, ObjectState = ObjectState.Added });
                unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 2, ObjectState = ObjectState.Added });
                unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 3, ObjectState = ObjectState.Added });

                unitOfWork.SaveChanges();

                var contact = unitOfWork.Repository<Contact>().Find(2);

                Assert.IsNotNull(contact);
                Assert.AreEqual(2, contact.ContactId);
            }
        }
        public void UpdateContact()
        {
            using (IDataContextAsync fakeDbContext = new UnitTestFakeDbContext())
            using (IUnitOfWork unitOfWork = new UnitOfWorkBase(fakeDbContext))
            {
                unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 2, FirstName = "Steve", ObjectState = ObjectState.Added });

                unitOfWork.SaveChanges();

                var contact = unitOfWork.Repository<Contact>().Find(2);

                Assert.AreEqual(contact.FirstName, "Steve", "Assert we are able to find the inserted Contact.");

                contact.FirstName = "Mike";
                contact.ObjectState = ObjectState.Modified;

                unitOfWork.Repository<Contact>().Update(contact);
                unitOfWork.SaveChanges();

                Assert.AreEqual(contact.FirstName, "Mike", "Assert that our changes were saved.");
            }
        }