【发布时间】:2013-11-19 21:22:54
【问题描述】:
由于我是 ASP.NET 网络表单和实体框架的新手,我正在尝试一个宠物项目。
在此期间,我遇到了以下我想了解的内容:
- 我有一个 ObjectDataSource(称为
EmployerObjectDataSource),它使用业务逻辑层 (BLL) 对象的方法来选择数据 - 该方法是GetEmployer - 在我的页面的
Page_PreRender回调中,我调用了一个方法populateFields来填充FormView 中的字段 - 在
populateFields中,我调用EmployerObjectDataSource.Select()以获取Employer记录。 - 如果返回了任何记录,则使用返回记录中的值填充文本框。
代码如下:
//Following Dmytro's comment, I will use Page_Load instead, however this
//does not resolve the problem
//protected void Page_PreRender(object sender, EventArgs e)
protected void Page_Load(object sender, EventArgs e)
{
_username = "Lefteris";
_version = 1;
if (!Page.IsPostBack)
{
populateFields();
}
}
private bool populateFields()
{
//IEnumerable<Employer> empl = ((IEnumerable<Employer>)EmployerObjectDataSource.Select()).ToList();
//The GetEmployer method of BLL is called here (as expected)
List<Employer> empl = (List<Employer>)EmployerObjectDataSource.Select();
System.Threading.Thread.Sleep(1000);
if (empl.Count() == 1)
{
Employer employer = empl.First();
//The GetEmployer method of BLL is called here (WHY????)
((RadTextBox)EmployerFormView.Row.FindControl("txtAme")).Text = employer.AME.ToString();
((RadTextBox)EmployerFormView.Row.FindControl("txtAfm")).Text = employer.EmplrAFM.ToString();
((RadTextBox)EmployerFormView.Row.FindControl("txtName")).Text = employer.EmplrLastName.ToString();
...
GetEmployer 如下所示:
public List<Employer> GetEmployer(string username, short version)
{
DateTime today = DateTime.Today;
List<Employer> employers = (ikaRepository.GetEmployers(username, today, version)).ToList<Employer>();
Debug.Assert(employers.Count() <= 1, "This is a logical Error - Can we have more than one active Employer records per user?");
return employers;
}
这是一个问题:
当我附加调试器时,我看到 BLL 的GetEmployer 方法被调用了两次。第一次在.Select() 上,第二次尝试获取Employer 记录的第一个字段的值。
谢谢
【问题讨论】:
-
为什么要在 PreRender 中而不是在 Load 中绑定数据?
-
@DmytroRudenko 我试图解决另一个问题,然后就这样离开了。没错,Page_Load 更合适,但这对我发布的问题没有影响。
-
也许你在employer.AME 财产的getter 中有一些小窍门?我找不到任何其他可能会调用 Select() 的原因。
标签: c# asp.net webforms repository-pattern objectdatasource