使用实体DAL(数据访问层)向ASP.NET WebSite添加自定义业务逻辑

时间:2012-02-07 23:50:06

标签: c# entity entities bll

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

1 个答案:

答案 0 :(得分:0)

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

编辑:在实体框架中使用业务逻辑唯一不好的地方是,当您将像gridviews这样的东西绑定到调用业务逻辑的ObjectDataSources时,您必须禁用分页和排序。我发现,如果你想要分页和排序,你必须在业务逻辑中添加更多代码,以便对服务器端进行排序和分页,以及表适配器支持的客户端排序。相当痛苦,但可能更好的表现。

相关问题