0

我有一个具有实体版本 4.0 DAL 的 ASP.NET 网站。我有几个页面,其中包含可以输入数据的文本框,以及用于查看和编辑该数据的 gridview。我正在使用 LINQ 在其中一页的代码隐藏文件中编写插入逻辑。但是,我想实现一个 BLL(业务逻辑层),以便我可以在多个地方使用此代码,并且只在一个地方进行修改。无论如何,我需要在后面的代码中为特定表调用这些 BLL 函数,并且我想使用 Visual Studio 的 GUI 将它们附加到 EntityDataSources。我已经设法创建了一个单独的类文件来编写我的自定义逻辑,但是当我使用 GUI 选择单独的更新、插入、和删除功能。我是否用错误的属性装饰了 BLL 中的函数?下面是我尝试使它工作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyTestModel;

/// <summary>
/// Used as custom logic for running queries againts the Location table
/// </summary>
public class LocationBLL
{
    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
    public IQueryable<RnLoc> viewLocation(string name)
    {
        MyTestEntities db = new MyTestEntities();
        var L = (from a in db.Location
                   where a.Location == name
                   select a);
        return L;
    }

    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, false)]
    public bool InsertLocation(string location, string longName, string comments, bool active, bool current)
    {
        MyTestEntities db = new MyTestEntities();

        Location L = new Location();
        L.Location = location;
        L.LongName = longName;
        L.Comments = comments;
        L.Active = active;
        L.Current = current;

        L.Edited = DateTime.Now;
        L.Created = DateTime.Now;
        L.EditedBy = "EIC";
        L.CreatedBy = "EIC";
        L.AreaID = 1;

        db.AddToLocations(L);
        db.SaveChanges();

        return true;
    }
}
4

1 回答 1

0

好的,所以我似乎已经回答了我自己的问题。在我们最近的项目中,我们使用的是数据集而不是实体框架,所以当我们制作网格视图时,我们将它们附加到 ObjectDataSources,上面的代码将提供可以在 ObjectDataSources 中选择的业务逻辑。此外,代码是一堆涉及表适配器的函数。在这个使用实体的新项目中,我将 entityDataSource 用于网格视图,并认为它是对象数据源的替代品。所以解决方案是再次使用 ObjectDataSources,并使用上面的代码来操作实体。我仍然不确定这是否是正确的编码,但它现在可以工作。

编辑:将业务逻辑与实体框架一起使用的唯一不好的地方是,当您将诸如网格视图之类的内容绑定到调用业务逻辑的 ObjectDataSources 时,您必须禁用分页和排序。我发现如果你想要分页和排序,你必须在业务逻辑中添加更多代码,以便它在服务器端排序和分页,而不是表适配器支持的客户端排序。相当痛苦,但可能对性能更好。

于 2012-02-10T16:03:40.367 回答