//OCP - Princípio Aberto/Fechado //As mudanças faz parte do nosso dia-a-dia //Como é possível alterar alguma coisa no código sem quebrar o que já funciona. //Vamos pensar com o código que seja extensível e evite quebrar o que já existe. //Uma das maneiras é usando Herança. static void Main(string[] args) { //Violação do principio. //Parece que tá tudo certo, mas não! //Alguns dos principios, por exemplo SRP, não está sendo respeitado. //Na mesma classe estamos tratando carro e moto. //Não está fechada para modificação, se precisasse de outro veiculo, teriamos que mudar. //Vamos aplicar o principio. Veja o projeto OCP //TypeVehicle type = TypeVehicle.CAR; TypeVehicle type = TypeVehicle.MOTORCYCLE; if (type == TypeVehicle.CAR) { Vehicle vehicle = new Vehicle("Azul", 2022, 2.0, 5, 4); vehicle.Car(); } else { Vehicle vehicle = new Vehicle("Branco", 2021, 250, 1, 0); vehicle.Motorcycle(); } Console.ReadLine(); }
public VehicleViewModel(int id, string licensePlate, double capacity, TypeVehicle typeVehicle) { Id = id; LicensePlate = licensePlate; Capacity = capacity; Type = typeVehicle; }
public async Task <IHttpActionResult> PutTypeVehicle(int id, TypeVehicle typeVehicle) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != typeVehicle.Id) { return(BadRequest()); } db.Entry(typeVehicle).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TypeVehicleExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public void Insert(string chassisSeries, int chassisNumber, TypeVehicle typeVehicle, string color) { var objVehicle = this.Find($"{chassisSeries}-{chassisNumber}"); if (objVehicle == null) { switch (typeVehicle) { case TypeVehicle.Bus: _repository.Insert(new Bus(chassisNumber, chassisSeries, color)); break; case TypeVehicle.Truck: _repository.Insert(new Truck(chassisNumber, chassisSeries, color)); break; case TypeVehicle.Car: _repository.Insert(new Car(chassisNumber, chassisSeries, color)); break; default: throw new DomainException("Type invalid"); } } else { throw new DomainException("ChassisID Duplicate!"); } }
public Vehicle(string licensePlates, string color, int price, TypeVehicle typeVehicle, int age, TypeStatusVehicle status, int mileage) { this.licensePlates = licensePlates; this.color = color; this.price = price; this.typeVehicle = typeVehicle; this.age = age; this.status = status; this.mileage = mileage; }
public TypeVehicle TypeVehicleToObject(TYPEVEHICLE item) { TypeVehicle newTypeVehicle = new TypeVehicle() { IdTypVehicle = item.IdTypeVehicle, NameTypeVehicle = item.NameTypeVehicle }; return(newTypeVehicle); }
public async Task <IHttpActionResult> GetTypeVehicle(int id) { TypeVehicle typeVehicle = await db.TypeVehicles.FindAsync(id); if (typeVehicle == null) { return(NotFound()); } return(Ok(typeVehicle)); }
public async Task <IHttpActionResult> PostTypeVehicle(TypeVehicle typeVehicle) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.TypeVehicles.Add(typeVehicle); await db.SaveChangesAsync(); return(CreatedAtRoute("DefaultApi", new { id = typeVehicle.Id }, typeVehicle)); }
public async Task <bool> InsertAsync(TypeVehicle typeVehicle) { try { await _typeVehicles.AddAsync(typeVehicle); return(true); } catch (System.Exception) { return(false); } }
public async Task <IHttpActionResult> DeleteTypeVehicle(int id) { TypeVehicle typeVehicle = await db.TypeVehicles.FindAsync(id); if (typeVehicle == null) { return(NotFound()); } db.TypeVehicles.Remove(typeVehicle); await db.SaveChangesAsync(); return(Ok(typeVehicle)); }
static void Main(string[] args) { TypeVehicle type = TypeVehicle.CAR; if (type == TypeVehicle.CAR) { Car vihicle = new Car("Preto", 2022, 2.0, 5, 4); } else { Motorcycle vihicle = new Motorcycle("Branca", 2023, 600); } Console.ReadLine(); }
static void Main(string[] args) { TypeVehicle type = TypeVehicle.MOTORCYCLE; if (type == TypeVehicle.CAR) { Car vehicle = new Car("Vermelho", 2022, 2.0, 5, 4); } else { Motorcycle vehicle = new Motorcycle("Branca", 2023, 300); } Console.ReadLine(); }
static void Main(string[] args) { TypeVehicle typeVehicle = TypeVehicle.MOTORCYCLE; if (typeVehicle == TypeVehicle.CAR) { Vehicle vehicle = new Vehicle("Azul", 2021, 2.0, 5, 4); vehicle.Car(); } else { Vehicle vehicle = new Vehicle("Branco", 2020, 250, 1, 0); vehicle.Motorcycle(); } Console.ReadLine(); }
static void Main(string[] args) { // Testar Carro e mota //TypeVehicle type = TypeVehicle.CAR; TypeVehicle type = TypeVehicle.MOTORCYCLE; if (type == TypeVehicle.CAR) { Car car = new Car("Red", 2022, 2.0, 5, 4); } else { Mortorcycle mortorcycle = new Mortorcycle("Yellow", 2021, 250); } Console.ReadLine(); }
public List <TypeVehicle> TypeVehicleToList(List <TYPEVEHICLE> typeVehicleList) { List <TypeVehicle> listTypeVehicle = new List <TypeVehicle>(); foreach (var item in typeVehicleList) { TypeVehicle newTypeVehicle = new TypeVehicle() { IdTypVehicle = item.IdTypeVehicle, NameTypeVehicle = item.NameTypeVehicle }; listTypeVehicle.Add(newTypeVehicle); } return(listTypeVehicle); }
static void Main(string[] args) { //Outra forma de trabalhar com OCP é o uso de interface //TypeVehicle type = TypeVehicle.CAR; TypeVehicle type = TypeVehicle.MOTORCYCLE; if (type == TypeVehicle.CAR) { Car vehicle = new Car("Azul", 2022, 2.0, 5, 4); } else { Motocycle vehicle = new Motocycle("Branco", 2021, 250); } Console.ReadLine(); }
static void Main(string[] args) { //Vamos aplicar o conceito da herança; //Por exemplo, a moto não tem porta e/ou quantidade de assentos, vamos tratar isso. TypeVehicle type = TypeVehicle.CAR; //TypeVehicle type = TypeVehicle.MOTORCYCLE; if (type == TypeVehicle.CAR) { Car vehicle = new Car("Azul", 2022, 2.0, 5, 4); } else { Motocycle vehicle = new Motocycle("Branco", 2021, 250); } Console.ReadLine(); }
public async Task <IActionResult> Add([FromBody] TypeVehicle model) { if (ModelState.IsValid) { var result = await _typeVehicleController.InsertAsync(model); if (result) { await _uow.SaveChangesAsync(); return(Ok()); } else { return(Ok(new { error = "متاسفانه مشکلی در ثبت اطلاعات به وجود آمده است." })); } } return(Ok(new { error = ModelState.AllMessage() })); }
public Truck(string licensePlates, string color, int price, TypeVehicle typeVehicle, int age, TypeStatusVehicle status, int mileage, int weight) : base(licensePlates, color, price, typeVehicle, age, status, mileage) { this.weight = weight; }
public Car(string licensePlates, string color, int price, TypeVehicle typeVehicle, int age, TypeStatusVehicle status, int mileage, int seating) : base(licensePlates, color, price, typeVehicle, age, status, mileage) { this.seating = seating; }
public Moto(string licensePlates, string color, int price, TypeVehicle typeVehicle, int age, TypeStatusVehicle status, int mileage, int cylinderVolume) : base(licensePlates, color, price, typeVehicle, age, status, mileage) { this.cylinderVolume = cylinderVolume; }