C#验证字段的更好方法。(Linq到SQL)
我仍在努力学习C#编程语言,并一直在使用VisualStudio2010 IDE 我正在制作一个工资单程序,对于我的工资单周期模块,我在Payroll.DLL下有2个文件;IEEmployerPeriodService.cs(接口)和EmployerPeriodService.cs 代码(IEmployerPeriodService.cs):C#验证字段的更好方法。(Linq到SQL),c#,visual-studio-2010,linq-to-sql,C#,Visual Studio 2010,Linq To Sql,我仍在努力学习C#编程语言,并一直在使用VisualStudio2010 IDE 我正在制作一个工资单程序,对于我的工资单周期模块,我在Payroll.DLL下有2个文件;IEEmployerPeriodService.cs(接口)和EmployerPeriodService.cs 代码(IEmployerPeriodService.cs): 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用Payroll.DAL; 命名空
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Payroll.DAL;
命名空间Payroll.BLL
{
公共接口IEmployerPeriodService
{
IEnumerable GetAllEmployerPeriods();
EmployerPeriod GetEmployerPeriod(字符串代码);
无效的AddEmployerPeriod(EmployerPeriod EmployerPeriod);
void AddEmployerPeriod(EmployerPeriod EmployerPeriod,string taxYear,string dayHours,string weekHours,string monthDays,string monthHours,string两周,string weeksInMonth,string numberoffortunings);
作废UpdateEmployerPeriod(EmployerPeriod EmployerPeriod);
void UpdateEmployerPeriod(EmployerPeriod EmployerPeriod,string taxYear,string dayHours,string weekHours,string monthDays,string monthHours,string WeeKnowthDays,string WeeKnowthHours,string Weeknowths,string WeeknowerMonth,string NumberFrowthnings);
作废DeleteEmployerPeriod(EmployerPeriod EmployerPeriod);
字符串GenerateSAPCode();
}
}
代码(EmployerPeriodService.cs):
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Payroll.DAL;
使用Payroll.Util.Extensions;
使用Payroll.Util.Helpers;
命名空间Payroll.BLL
{
公共类EmployerPeriodService:IEEmployerPeriodService
{
私人只读IEEmployerPeriodRepository(U employerPeriodRepository);
公共雇员定期服务(
I雇员定期雇员定期雇员(定期雇员)
{
Guard.AgainstNullParameter(employerPeriodRepository,“employerPeriodRepository”);
_employerPeriodRepository=employerPeriodRepository;
}
公共IEnumerable GetAllEmployerPeriods()
{
return _employerPeriodRepository.SelectAll();
}
公共EmployerPeriod GetEmployerPeriod(字符串代码)
{
var rulesException=新rulesException();
//业务规则:应答Id不能小于1
如果(!code.HasValue())
rulesException.ErrorFor(x=>x.Code,“代码不能为空”);
if(rulesException.Errors.Any())抛出rulesException;
return _employerPeriodRepository.GetEntity(代码);
}
公共无效AddEmployerPeriod(EmployerPeriod EmployerPeriod)
{
//业务规则:0
var errors=DataAnnotationsValidationRunner.GetErrors(employerPeriod);
var rulesException=新rulesException();
rulesException.ErrorsForModel(错误);
//其他验证规则
if(rulesException.Errors.Any())抛出rulesException;
//保存到数据库
_employerPeriodRepository.插入(employerPeriod);
}
public void AddEmployerPeriod(EmployerPeriod EmployerPeriod,string taxYear,string dayHours,string weekHours,string monthDays,string monthHours,string fornightday,string fornightdhours,string weeksInMonth,string numberoffornights)
{
var rulesException=新rulesException();
var tYear=默认值(短);
如果(!short.TryParse(纳税年度,外年度))
rulesException.ErrorFor(x=>x.U_Tax_year,“Tax year”字段格式不正确);
else Employer Period.U\U Tax\U year=tYear;
var dHours=默认值(短);
如果(!短锥虫(昼小时,外小时))
rulesException.ErrorFor(x=>x.U_Day_hrs,“日字段中的小时数格式不正确”);
else Employer Period.U_Day_hrs=小时;
var wDays=默认值(短);
如果(!short.TryParse(工作日,外工作日))
rulesException.ErrorFor(x=>x.U_Week_days,“周数”字段的格式不正确);
else Employer Period.U\U Week\U days=wDays;
var wHours=默认值(短);
如果(!short.TryParse(weekHours,out wHours))
rulesException.ErrorFor(x=>x.U_Week_hrs,“一周的小时数”字段的格式不正确);
其他员工周期(周)小时=wHours;
var mDays=默认值(短);
如果(!短锥虫(月日,月日))
rulesException.ErrorFor(x=>x.U_Month_days,“月份字段中的天数格式不正确”);
else Employer Period.U\U Month\U days=mDays;
var mHours=默认值(短);
如果(!short.TryParse(月小时,月小时))
rulesException.ErrorFor(x=>x.U_Month_hrs,“月数”字段的格式不正确);
其他员工期限(月)小时=小时;
var fnDays=默认值(短);
如果(!短锥虫(两周一次,三天一次))
rulesException.ErrorFor(x=>x.U_fortunight_days,“两周字段中的天数格式不正确”);
其他员工两周内=fn天;
var FNHurs=默认值(短);
如果(!短锥虫(两周,两小时)
rulesException.ErrorFor(x=>x.U_fortunight_hrs,“两周字段中的小时数格式不正确”);
其他员工两周的工作时间=fn小时;
var wInMonth=默认值(短);
如果(!short.TryParse(周新月,月外))
rulesException.ErrorFor(x=>x.U_Weeks_in_month,“月数”字段的格式不正确);
其他雇员时期。
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();
}
}
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();
}
}
}
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);
}
}
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);
}
}