我仍然在尝试学习C#编程语言,并且一直在使用Visual Studio 2010 IDE。
我正在制作薪资计划,而对于我的工资核算期间模块,我在Payroll.DLL下有2个文件; IEmployerPeriodService.cs(接口)和EmployerPeriodService.cs。
代码(IEmployerPeriodService.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Payroll.DAL;
namespace Payroll.BLL
{
public interface IEmployerPeriodService
{
IEnumerable<EmployerPeriod> GetAllEmployerPeriods();
EmployerPeriod GetEmployerPeriod(string code);
void AddEmployerPeriod(EmployerPeriod employerPeriod);
void AddEmployerPeriod(EmployerPeriod employerPeriod, string taxYear, string dayHours, string weekDays, string weekHours, string monthDays, string monthHours, string fortnightDays, string fortnightHours, string weeksInMonth, string numberOfFortnights);
void UpdateEmployerPeriod(EmployerPeriod employerPeriod);
void UpdateEmployerPeriod(EmployerPeriod employerPeriod, string taxYear, string dayHours, string weekDays, string weekHours, string monthDays, string monthHours, string fortnightDays, string fortnightHours, string weeksInMonth, string numberOfFortnights);
void DeleteEmployerPeriod(EmployerPeriod employerPeriod);
string GenerateSAPCode();
}
}
代码(EmployerPeriodService.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using Payroll.DAL;
using Payroll.Util.Extensions;
using Payroll.Util.Helpers;
namespace Payroll.BLL
{
public class EmployerPeriodService : IEmployerPeriodService
{
private readonly IEmployerPeriodRepository _employerPeriodRepository;
public EmployerPeriodService(
IEmployerPeriodRepository employerPeriodRepository)
{
Guard.AgainstNullParameter(employerPeriodRepository, "employerPeriodRepository");
_employerPeriodRepository = employerPeriodRepository;
}
public IEnumerable<EmployerPeriod> GetAllEmployerPeriods()
{
return _employerPeriodRepository.SelectAll();
}
public EmployerPeriod GetEmployerPeriod(string code)
{
var rulesException = new RulesException<EmployerPeriod>();
// Business rule: Answer Id cannot be less than 1
if (!code.HasValue())
rulesException.ErrorFor(x => x.Code, "Code cannot be null");
if (rulesException.Errors.Any()) throw rulesException;
return _employerPeriodRepository.GetEntity(code);
}
public void AddEmployerPeriod(EmployerPeriod employerPeriod)
{
// Business rule: 0
var errors = DataAnnotationsValidationRunner.GetErrors(employerPeriod);
var rulesException = new RulesException();
rulesException.ErrorsForModel(errors);
// Other validation rules
if (rulesException.Errors.Any()) throw rulesException;
// Save to database
_employerPeriodRepository.Insert(employerPeriod);
}
public void AddEmployerPeriod(EmployerPeriod employerPeriod, string taxYear, string dayHours, string weekDays, string weekHours, string monthDays, string monthHours, string fortnightDays, string fortnightHours, string weeksInMonth, string numberOfFortnights)
{
var rulesException = new RulesException<EmployerPeriod>();
var tYear = default(short);
if (!short.TryParse(taxYear, out tYear))
rulesException.ErrorFor(x => x.U_Tax_year, "The Tax Year field is not in the correct format");
else employerPeriod.U_Tax_year = tYear;
var dHours = default(short);
if (!short.TryParse(dayHours, out dHours))
rulesException.ErrorFor(x => x.U_Day_hrs, "The Number of Hours in a Day field is not in the correct format");
else employerPeriod.U_Day_hrs = dHours;
var wDays = default(short);
if (!short.TryParse(weekDays, out wDays))
rulesException.ErrorFor(x => x.U_Week_days, "The Number of Days in a Week field is not in the correct format");
else employerPeriod.U_Week_days = wDays;
var wHours = default(short);
if (!short.TryParse(weekHours, out wHours))
rulesException.ErrorFor(x => x.U_Week_hrs, "The Number of Hours in a Week field is not in the correct format");
else employerPeriod.U_Week_hrs = wHours;
var mDays = default(short);
if (!short.TryParse(monthDays, out mDays))
rulesException.ErrorFor(x => x.U_Month_days, "The Number of Days in a Month field is not in the correct format");
else employerPeriod.U_Month_days = mDays;
var mHours = default(short);
if (!short.TryParse(monthHours, out mHours))
rulesException.ErrorFor(x => x.U_Month_hrs, "The Number of Hours in a Month field is not in the correct format");
else employerPeriod.U_Month_hrs = mHours;
var fnDays = default(short);
if (!short.TryParse(fortnightDays, out fnDays))
rulesException.ErrorFor(x => x.U_Fortnight_days, "The Number of Days in a Fortnight field is not in the correct format");
else employerPeriod.U_Fortnight_days = fnDays;
var fnHours = default(short);
if (!short.TryParse(fortnightHours, out fnHours))
rulesException.ErrorFor(x => x.U_Fortnight_hrs, "The Number of Hours in a Fortnight field is not in the correct format");
else employerPeriod.U_Fortnight_hrs = fnHours;
var wInMonth = default(short);
if (!short.TryParse(weeksInMonth, out wInMonth))
rulesException.ErrorFor(x => x.U_Weeks_in_month, "The Number of Weeks in a Month field is not in the correct format");
else employerPeriod.U_Weeks_in_month = wInMonth;
var nOfFn = default(short);
if (!short.TryParse(numberOfFortnights, out nOfFn))
rulesException.ErrorFor(x => x.U_No_of_Fortnights, "The Number of Fortnights in a Month field is not in the correct format");
else employerPeriod.U_No_of_Fortnights = nOfFn;
if (rulesException.Errors.Any()) throw rulesException;
AddEmployerPeriod(employerPeriod);
}
public void UpdateEmployerPeriod(EmployerPeriod employerPeriod)
{
// Business rule: 0
var errors = DataAnnotationsValidationRunner.GetErrors(employerPeriod);
var rulesException = new RulesException();
rulesException.ErrorsForModel(errors);
// Other validation rules
if (rulesException.Errors.Any()) throw rulesException;
// Save to database
_employerPeriodRepository.Update(employerPeriod);
}
public void UpdateEmployerPeriod(EmployerPeriod employerPeriod, string taxYear, string dayHours, string weekDays, string weekHours, string monthDays, string monthHours, string fortnightDays, string fortnightHours, string weeksInMonth, string numberOfFortnights)
{
var rulesException = new RulesException<EmployerPeriod>();
var tYear = default(short);
if (!short.TryParse(taxYear, out tYear))
rulesException.ErrorFor(x => x.U_Tax_year, "The Tax Year field is not in the correct format");
else employerPeriod.U_Tax_year = tYear;
var dHours = default(short);
if (!short.TryParse(dayHours, out dHours))
rulesException.ErrorFor(x => x.U_Day_hrs, "The Number of Hours in a Day field is not in the correct format");
else employerPeriod.U_Day_hrs = dHours;
var wDays = default(short);
if (!short.TryParse(weekDays, out wDays))
rulesException.ErrorFor(x => x.U_Week_days, "The Number of Days in a Week field is not in the correct format");
else employerPeriod.U_Week_days = wDays;
var wHours = default(short);
if (!short.TryParse(weekHours, out wHours))
rulesException.ErrorFor(x => x.U_Week_hrs, "The Number of Hours in a Week field is not in the correct format");
else employerPeriod.U_Week_hrs = wHours;
var mDays = default(short);
if (!short.TryParse(monthDays, out mDays))
rulesException.ErrorFor(x => x.U_Month_days, "The Number of Days in a Month field is not in the correct format");
else employerPeriod.U_Month_days = mDays;
var mHours = default(short);
if (!short.TryParse(monthHours, out mHours))
rulesException.ErrorFor(x => x.U_Month_hrs, "The Number of Hours in a Month field is not in the correct format");
else employerPeriod.U_Month_hrs = mHours;
var fnDays = default(short);
if (!short.TryParse(fortnightDays, out fnDays))
rulesException.ErrorFor(x => x.U_Fortnight_days, "The Number of Days in a Fortnight field is not in the correct format");
else employerPeriod.U_Fortnight_days = fnDays;
var fnHours = default(short);
if (!short.TryParse(fortnightHours, out fnHours))
rulesException.ErrorFor(x => x.U_Fortnight_hrs, "The Number of Hours in a Fortnight field is not in the correct format");
else employerPeriod.U_Fortnight_hrs = fnHours;
var wInMonth = default(short);
if (!short.TryParse(weeksInMonth, out wInMonth))
rulesException.ErrorFor(x => x.U_Weeks_in_month, "The Number of Weeks in a Month field is not in the correct format");
else employerPeriod.U_Weeks_in_month = wInMonth;
var nOfFn = default(short);
if (!short.TryParse(numberOfFortnights, out nOfFn))
rulesException.ErrorFor(x => x.U_No_of_Fortnights, "The Number of Fortnights in a Month field is not in the correct format");
else employerPeriod.U_No_of_Fortnights = nOfFn;
if (rulesException.Errors.Any()) throw rulesException;
UpdateEmployerPeriod(employerPeriod);
}
public void DeleteEmployerPeriod(EmployerPeriod employerPeriod)
{
var bm = GetEmployerPeriod(employerPeriod.Code);
if (bm != null)
{
// Delete from database
_employerPeriodRepository.Delete(bm);
}
}
public string GenerateSAPCode()
{
var result = default(long);
var codesList = from b in GetAllEmployerPeriods()
select new { Code = long.Parse(b.Code) };
codesList = codesList.OrderBy(x => x.Code);
var lastRecord = codesList.LastOrDefault();
if (lastRecord != null)
result = lastRecord.Code + 1;
return result.ToString();
}
}
}
代码(PayRollPeriodForm.cs):
这是表单背后的代码
private void btn_add_Click(object sender, EventArgs e)
{
try
{
// Get service instance
var employerPeriodService = Program.Kernel.Get<IEmployerPeriodService>();
// Get new code
var newCode = employerPeriodService.GenerateSAPCode();
// Create object
var employerPeriodAdd =
new EmployerPeriod
{
Code = newCode,
Name = newCode,
U_Starting_period = dateTimePicker1.Value,
U_Ending_period = dateTimePicker2.Value,
};
// Save record
employerPeriodService.AddEmployerPeriod(
employerPeriodAdd,
cb_tax_year.Text,
cb_number_hours.Text,
cb_number_days_week.Text,
txt_number_hours_week.Text,
cb_number_days_month.Text,
txt_number_hours_month.Text,
cb_number_days_fortnight.Text,
txt_number_hours_fortnight.Text,
cb_avg_weeks_month.Text,
cb_avg_fortnights_month.Text);
MessageBox.Show("Employer Payroll Period Added Successfully. Intake Ref: " + newCode.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//clear textfields after input
cb_tax_year.Text = null;
cb_number_hours.Text = null;
cb_number_days_week.Text = null;
txt_number_hours_week.Text = "";
cb_number_days_month.Text = null;
txt_number_hours_month.Text = "";
cb_number_days_fortnight.Text = null;
txt_number_hours_fortnight.Text = "";
cb_avg_weeks_month.Text = null;
cb_avg_fortnights_month.Text = null;
dateTimePicker1.Text = null;
dateTimePicker2.Text = null;
btn_add.Enabled = false;
}
catch (RulesException ex)
{
MessageBox.Show(ex.GetErrorMessages(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
有没有更好的方法来验证我在AddEmployerPeriod的重载方法中的字段。我有一个包含30多个字段的表格,我认为必须有更好的方法来验证字段。
我也想使用类似的东西来更新(编辑)细节。
编辑代码是:
private void btn_update_Click(object sender, EventArgs e)
{
try
{
// Get service instance
var employerPeriodService = Program.Kernel.Get<IEmployerPeriodService>();
// Get record
var employerPeriodUpdate = employerPeriodService.GetEmployerPeriod(SAPCodePeriod);
if (employerPeriodUpdate != null)
{
// Update record
employerPeriodUpdate.U_Tax_year = short.Parse(cb_tax_year.Text);
employerPeriodUpdate.U_Day_hrs = short.Parse(cb_number_hours.Text);
employerPeriodUpdate.U_Week_days = short.Parse(cb_number_days_week.Text);
employerPeriodUpdate.U_Week_hrs = short.Parse(txt_number_hours_week.Text);
employerPeriodUpdate.U_Month_days = short.Parse(cb_number_days_month.Text);
employerPeriodUpdate.U_Month_hrs = short.Parse(txt_number_hours_month.Text);
employerPeriodUpdate.U_Fortnight_days = short.Parse(cb_number_days_fortnight.Text);
employerPeriodUpdate.U_Fortnight_hrs = short.Parse(txt_number_hours_fortnight.Text);
employerPeriodUpdate.U_Weeks_in_month = short.Parse(cb_avg_weeks_month.Text);
employerPeriodUpdate.U_No_of_Fortnights = short.Parse(cb_avg_fortnights_month.Text);
employerPeriodUpdate.U_Starting_period = dateTimePicker1.Value;
employerPeriodUpdate.U_Ending_period = dateTimePicker2.Value;
// Save record
//employerPeriodService.UpdateEmployerPeriod(employerPeriodUpdate);
MessageBox.Show("Tax Year Details Edited Successfully");
}
}
catch (RulesException ex)
{
MessageBox.Show(ex.GetErrorMessages(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
我是否使用与add add方法类似的东西?我希望我的问题足够清楚。
答案 0 :(得分:2)
有许多库可以处理这种验证,您始终可以构建自定义验证框架。
我使用的库是the enterprise library validation application block。您可以在需要验证的控件下的视图中创建验证器,并通过向需要验证的属性添加属性来在模型类(在您的情况下为EmployerPeriod)中编写验证规则。