public void Update(int id, Movie movie) { //TODO: movie is not null if (movie == null) { throw new ArgumentNullException(nameof(movie)); } //TODO: id >= 0 if (id <= 0) { throw new ArgumentOutOfRangeException(nameof(id), "Id must be greater than zero"); } //Movie is valid ObjectValidator.ValidateFullObject(movie); //var results = new ObjectValidator().TryValidateFullObject(movie); //if (results.Count() > 0) //{ // foreach (var result in results) // { // return result.ErrorMessage; // }; //}; // Movie name is unique var existing = GetByName(movie.Name); if (existing != null && existing.Id != id) { throw new InvalidOperationException("Movie must be unique"); } //Generalize errors try { UpdateCore(id, movie); } catch (Exception e) { throw new InvalidOperationException("Update Failed", e); }; }
public Movie Add(Movie movie) { if (movie == null) { throw new ArgumentNullException(nameof(movie)); } ObjectValidator.ValidateFullObject(movie); var existing = GetByName(movie.Name); if (existing != null) { throw new InvalidOperationException("Movie must be unique"); } try { return(AddCore(movie)); } catch (Exception e) { throw new InvalidOperationException("Add Failed", e); }; }
/// <inheritdoc /> public void Update(int id, Movie movie) { //Validation if (id <= 0) { throw new ArgumentOutOfRangeException(nameof(id), "Id must be greater than 0."); } if (movie == null) { throw new ArgumentNullException(nameof(movie)); } ObjectValidator.Validate(movie); //Must be unique var existing = FindByTitle(movie.Title); if (existing != null && existing.Id != id) { throw new InvalidOperationException("Movie title must be unique."); } try { UpdateCore(id, movie); } catch (ArgumentException) { throw; } catch (InvalidOperationException) { throw; } catch (Exception e) { throw new Exception("Update failed", e); }; }
//Not on interface //public void Foo () { } /// <inheritdoc /> public Movie Add(Movie movie) { // Exception type is the base type of all exceptions // Arguments should always fail with Argument exceptions // Exception -> generic exceptions with a message // ArgumentException -> generic argument exception // ArgumentNullException -> argument is null and it shouldn't be // ArgumentOutOfRangeException -> argument is outside excepted range (generally numeric) // ValidationException -> IValidatableObject fails // InvalidOperationException -> The operation is not currently valid but may be in the future // SystemException -> Only generated by runtime // NullReferenceException -> null is on left side of member access (null.???) // StackOverflowException -> Stack overflowed // OutOfMemoryException -> Out of memory // Throw an exception using throw expression // throw-expression ::= throw E // E must be Exception // Movie is not null if (movie == null) { throw new ArgumentNullException(nameof(movie)); //Argument is null and it shouldn't be, pretty much all reference types } //Movie is valid ObjectValidator.ValidateFullObject(movie); //var results = new ObjectValidator().TryValidateFullObject(movie); //if (results.Count() > 0) //{ // foreach (var result in results) // { // error = result.ErrorMessage; // return null; // }; //}; // Movie name is unique //var existing = GetByName(movie.Name); //if (existing != null) // throw new InvalidOperationException("Movie must be unique"); // Throw expression ::= E ?? throw E var existing = GetByName(movie.Name); if (existing != null) { throw new InvalidOperationException("Movie must be unique"); } //{ // error = "Movie must be unique"; // return null; //}; // Generalize errors try { return(AddCore(movie)); } catch (Exception e) { //Throwing a new exception throw new InvalidOperationException("Add Failed", e); }; }
//Array - Type[] //public Movie[] Items { get; set; } public Movie Add(Movie movie) { // Exception type is the base type of all exceptions // Arguments should always fail with Argument exceptions // Exception -> generic expression with a message // ArgumentException -> generic argument exception // ArgumentNullException -> argument is null and it shouldn't be // ArgumentOutOfRangeException -> argument is outside excepted range (generally numeric) // ValidationException -> IValidatableObject fails // InvalidOperationException -> The operation is not currently valid but may be in the future // SystemException -> Only generated by runtime // NullReferenceException -> null is on left side of member access (null.???) // StackOverflowException -> Stack overflowed // OutOfMemoryException -> Out of memory // Throw an exception using throw expression // throw-expression ::= throw E // E must be exception // Movie is not null if (movie == null) { throw new ArgumentNullException(nameof(movie)); //Argument is null and it shouldn't be, pretty much all reference types } ObjectValidator.ValidateFullObject(movie); //if (results.Count() > 0) //{ // foreach (var result in results) // { // error = result.ErrorMessage; // return null; //}; //}; //TODO: Movie is valid // Movie name is unique var existing = GetByName(movie.Name); if (existing != null) { throw new InvalidOperationException("Movie must be unique"); } // Throw expression ::= E ?? throw E //var existing = GetByName(movie.Name) ?? throw new InvalidOperationException("Movie must be unique"); //error = "";//commented out after ivalidatable //Clone so argument can be modified without impacting our array //var item = CloneMovie(movie); //set a unique ID //item.Id = _id++; //add movie to array //_movies.Add(item); // Movie is a ref type thus movie and _movies[index] reference the same instance //Set ID on original object and return //movie.Id = item.Id; //TODO: Generalize errors try { return(AddCore(movie)); } catch (Exception e) { //Throwing a new exception throw new InvalidOperationException("Add Failed", e); }; //TODO: No more room // error = "No more room"; //return null; }