无法循环从 PL 中的 DAL 实体框架返回的数据

发布于 2025-01-04 18:17:18 字数 1718 浏览 4 评论 0原文

我在我的项目中使用实体框架 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾家有女初长成 2025-01-11 18:17:18
  1. 理想情况下,您不想在 PL 中引用 DAL。您可以与 PL 和 BL 共享类型。 BL 会在返回之前将您的 DAL 类型转换为此类型。

  2. 对于您显示 yes 静态方法的示例应该没问题。

  1. 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.

  2. For the example you have show yes static method should be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文