public Venue Insert(Venue entity) { if (GetByFoursquareId(entity.FoursquareId) == null) { entity = _venueRepository.Insert(entity); entity.Location.VenueId = entity.Id; entity.Location = _locationRepository.Insert(entity.Location); Category insertedCategory; foreach (var category in entity.Categories) { var storedCategory = _categoryRepository.GetByFoursquareId(category.FoursquareId); if (storedCategory == null) { insertedCategory = _categoryRepository.Insert(category); _venueRepository.InsertCategory(insertedCategory, entity); } else { _venueRepository.InsertCategory(storedCategory, entity); } } } return(entity); }