public void Update(int id, Movie movie) { if (id <= 0) { throw new ArgumentOutOfRangeException(nameof(id), "Id must be greater than zero"); } if (movie == null) { throw new ArgumentNullException(nameof(movie)); } ObjectValidator.ValidateFullObject(movie); var existing = GetByName(movie.Name); if (existing != null && existing.Id != id) { throw new InvalidOperationException("Movie must be unique"); } try { UpdateCore(id, movie); } catch (Exception e) { throw new InvalidOperationException("Update Failed", e); }; }
//public void Foo () { } // Array - T[] = type name followed with square brackets public Movie Add(Movie movie) { // Exception type is teh base type of all exceptions // Arguments should always fail with Argument exceptions // Exception -> ArgumentException // Argument Exception - reports problems with data passed to a method // ArgumentNullException -> argument is null and shouldnt be // ArgumentOutOfRangeException -> argument is outside of specified range // Validation Exception -> IValidateObject fails // InvalidOperationException -> The operation is not currently valid, but may be in the future // System Exception -> Only generated by runtime // NullRefernceException -> null is on left side of member access (null.???) // StackOverflowException -> Stack overflowed // OutOfMemoryException -> out of memory // Throw an expression using throw exception // throw-expression ::- throw E // E must be Exception //if (movie == null) // throw new ArgumentNullException(nameof(movie)); // Argument is null and shouldn't be, pretty much all reference types // Movie is valid //new ObjectValidator().ValidateFullObject(movie); ObjectValidator.ValidateFullObject(movie); // if (results.Count() > 0) // { // foreach (var result in results) // { // error = result.ErrorMessage; // return null; // } // } //TODO Movie is valid, name is unique var existing = GetByName(movie.Name); if (existing != null) { throw new InvalidOperationException("Movie must be unique"); } // { // error = "Movie must be unique!"; // return null; // } try { return(AddCore(movie)); } catch (Exception e) { //throwing a new exception throw new InvalidOperationException("Add Failed", e); } }
public void Update(int id, Movie movie) { //TODO: Validate Id if (id <= 0) { throw new ArgumentOutOfRangeException(nameof(id), "Id must be greater than zero"); } if (movie == null) { throw new ArgumentNullException(nameof(movie)); } // Movie exists //TODO: Movie is valid ObjectValidator.ValidateFullObject(movie); // Movie name is unique var existing = GetByName(movie.Name); if (existing != null && existing.Id != id) { throw new InvalidOperationException("Movie must be unique"); } try { UpdateCore(id, movie); } catch (Exception e) { //Throwing a new exception throw new InvalidOperationException("Update Failed", e); }; /* for (var index = 0; index<_movies.Length; ++index) * { * // Array element access ::= V{int} * //if (_movies[index] != null && _movies[index].Id == id ) * if (_movies[index]?.Id == id) // null conditional ?. if instance != null, access the member otherwise skip * { * * //Just because we are doing this in memory (reference type) * movie.Id = id; * _movies[index] = movie; // Movie is a ref type thus movie and _movies[index] reference the same instance * return ""; * // Add movie to array * } * };*/ }
public void Update(int id, Movie movie) { //Validation if (id <= 0) { throw new ArgumentOutOfRangeException(nameof(id), "Id must be greater than zero"); } if (movie == null) { throw new ArgumentNullException(nameof(movie)); } //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) { //Throwing a new exception 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); }; }
//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; }