【发布时间】:2012-03-04 02:44:05
【问题描述】:
我在我的项目中使用 Entity Framework 4。一项基本要求是使用存储过程从数据库中获取数据,并在表示层中循环这些数据,并与下拉列表、网格控件等绑定。
我的流程如下:
PL 调用 BLL 和 BLL 调用 DAL
示例代码:
DAL:
namespace DAL.Repository
{
public class CountryRepository
{
public static IList GetCountry(string CountryId)
{
using(MyDBEntities context=new MyDBEntities())
{
// it calls SP in DB thru EF to fetch data
return context.GetCountry(CountryId).ToList();
}
}
}
}
BLL
namespace BLL
{
public class Country
{
public static IList GetCountry(string CountryId)
{
return DAL.Repository.CountryRepository.GetCountry(CountryId);
}
}
}
PL
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryId";
ddlCountry.DataSource= BLL.Country.GetCountry(string.Empty);
ddlCountry.DataBind();
* 这里绑定工作正常。但我不确定它是否是最佳选择。请给我建议。
System.Collections.IEnumerator lst= BLL.Country.GetCountry(string.Empty).GetEnumerator();
while(lst.MoveNext())
{
string s = ((DAL.Entity.ORM.Country)(lst.Current)).Countryname;
/* This is the main issue. To get data from [current]
reference of [DAL.Entity.ORM.Country] is required.
It is strongly not good practice to keep reference
of DAL in PL. */
}
使用带有存储过程的 EF 从数据库中获取数据并在 PL 中独立使用这些数据的最佳方法是什么?
使用静态方法是否安全?
请给我建议。
【问题讨论】:
标签: loops stored-procedures entity-framework-4 data-access-layer