コード例 #1
0
        //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();
        }
コード例 #2
0
ファイル: VehicleViewModel.cs プロジェクト: ducanh96/schedule
 public VehicleViewModel(int id, string licensePlate, double capacity, TypeVehicle typeVehicle)
 {
     Id           = id;
     LicensePlate = licensePlate;
     Capacity     = capacity;
     Type         = typeVehicle;
 }
コード例 #3
0
        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));
        }
コード例 #4
0
        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!");
            }
        }
コード例 #5
0
 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;
 }
コード例 #6
0
        public TypeVehicle TypeVehicleToObject(TYPEVEHICLE item)
        {
            TypeVehicle newTypeVehicle = new TypeVehicle()
            {
                IdTypVehicle    = item.IdTypeVehicle,
                NameTypeVehicle = item.NameTypeVehicle
            };

            return(newTypeVehicle);
        }
コード例 #7
0
        public async Task <IHttpActionResult> GetTypeVehicle(int id)
        {
            TypeVehicle typeVehicle = await db.TypeVehicles.FindAsync(id);

            if (typeVehicle == null)
            {
                return(NotFound());
            }

            return(Ok(typeVehicle));
        }
コード例 #8
0
        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));
        }
コード例 #9
0
        public async Task <bool> InsertAsync(TypeVehicle typeVehicle)
        {
            try
            {
                await _typeVehicles.AddAsync(typeVehicle);

                return(true);
            }
            catch (System.Exception)
            {
                return(false);
            }
        }
コード例 #10
0
        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));
        }
コード例 #11
0
        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();
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: leonardobolzan/SOLID
        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();
        }
コード例 #13
0
        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();
        }
コード例 #14
0
ファイル: Program.cs プロジェクト: nunovdb/SOLID
        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();
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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();
        }
コード例 #17
0
        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();
        }
コード例 #18
0
        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() }));
        }
コード例 #19
0
 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;
 }
コード例 #20
0
ファイル: Car.cs プロジェクト: antran2123153/CarRentalCompany
 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;
 }
コード例 #21
0
 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;
 }