Exemplo n.º 1
0
        public List <Employee> FindAll()
        {
            _connection = new SqlConnection(_connectionString);

            _connection.Open();

            var cmd = _connection.CreateCommand();

            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM Employees";

            var reader = cmd.ExecuteReader();

            var list = new List <Employee>();

            while (reader.Read())
            {
                var e = new Employee();

                e.Id              = (int)reader["Id"];
                e.Name            = (string)reader["Name"];
                e.TotalPay        = (decimal)reader["TotalPay"];
                e.BonusCalculator =
                    createBonusCalculator((string)reader["BonusCalculatorType"]);

                var payType = (string)reader["PayCalculatorType"];

                switch (payType)
                {
                case nameof(FixedPayCalculator):
                    var fixedCalc = new FixedPayCalculator();
                    fixedCalc.MonthlySalary = (decimal)reader["MonthlySalary"];
                    break;

                case nameof(HourlyPayCalculator):
                    var hourlyCalc = new HourlyPayCalculator();
                    hourlyCalc.HourlySalary = (decimal)reader["HourlySalary"];
                    // TODO: pesca le schedulations di questo employee.
                    break;

                case nameof(CommissionPayCalculator):
                    var commCalc = new CommissionPayCalculator();
                    commCalc.CommissionPercentage = (decimal)reader["CommissionPercentage"];
                    //TODO: pesca le Commissions di questo Employee
                    break;

                case nameof(NullPayCalculator):
                    break;

                default:
                    throw new ArgumentException($"'{payType}' is not a known PayCalculator!");
                }

                list.Add(e);
            }

            _connection.Close();

            return(list);
        }
Exemplo n.º 2
0
        public void Save(Employee e, CommissionPayCalculator c)
        {
            var cmd = _connection.CreateCommand();

            cmd.CommandType = CommandType.Text;
            cmd.CommandText =
                " UPDATE Employees SET" +
                " PayCalculatorType = @PayCalculatorType," +
                " CommissionPercentage = @CommissionPercentage" +
                " WHERE Id = @Id;";
            cmd.Parameters.Add(new SqlParameter("PayCalculatorType", c.GetType().Name));
            cmd.Parameters.Add(new SqlParameter("CommissionPercentage", c.CommissionPercentage));
            cmd.Parameters.Add(new SqlParameter("Id", e.Id));

            cmd.ExecuteNonQuery();

            foreach (var s in c.Commissions)
            {
                var commissionCmd = _connection.CreateCommand();
                commissionCmd.CommandType = CommandType.Text;
                commissionCmd.CommandText =
                    " INSERT INTO Commissions" +
                    " (EmployeeId,Date,CommissionAmount)" +
                    " VALUES" +
                    " (@EmployeeId, @Date,@CommissionAmount);";

                commissionCmd.Parameters.Add(new SqlParameter("EmployeeId", e.Id));
                commissionCmd.Parameters.Add(new SqlParameter("Date", s.Key));
                commissionCmd.Parameters.Add(new SqlParameter("CommissionAmount", s.Value));

                commissionCmd.ExecuteNonQuery();
            }
        }