无法循环从 PL 中的 DAL 实体框架返回的数据
我在我的项目中使用实体框架 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 中独立使用这些数据的最佳方法是什么?
像我一样使用静态方法是否安全使用过吗?
请推荐我。
I am using Entity Framework 4 in my project. One basic requirement is to fetch data from database using stored procedure and loop those data in presentation layer and also bind with dropdownlist, grid control etc.
I am following the flow as follow:
PL call BLL and BLL call DAL
Sample codes :
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();
* Here binding working fine. But i am not sure is it the best option. Please suggest me.
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. */
}
What is best way of fetching data from database using EF with Stored procedure and using those data Independently in PL?
Is is safe to use static method as i have used?
Please suggest me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理想情况下,您不想在 PL 中引用 DAL。您可以与 PL 和 BL 共享类型。 BL 会在返回之前将您的 DAL 类型转换为此类型。
对于您显示 yes 静态方法的示例应该没问题。
Ideally you don't want to reference your DAL in your PL. You could have a shared type with your PL and BL. The BL would convert your DAL type to this type before returning it.
For the example you have show yes static method should be fine.