Question

I have an ASP.NET web site that has a entities version 4.0 DAL. I have several pages that have texts boxes where you can enter data, and gridview to see and edit that data. I am using LINQ to write the insert logic in the code behind file for one of the pages. However, I would like to implement a BLL(business logic layer) so that I can use this code in several places and only make modification in one place. Anyhow, I need to call these BLL function for a particular table in code behind and I want to attach them to EntityDataSources using the GUI of visual studio. I have managed to make a separate class file to write my custom logic, but I can't see to make the functions within the BLL file appear in the drop down of the EntityDataSources when I am using the GUI to pick separate update, insert, and delete functions. Am I decorating the functions in the BLL with the wrong property? Below is my attempt to make it work.

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;
    }
}
Was it helpful?

Solution

OK, so I seemed to have answered my own question. On our recent project we were using a Dataset not the entity framework, so when we made gridviews we attached them to ObjectDataSources, and the code above would provide the business logic that would be selectable within the ObjectDataSources. Also, the code was a bunch of functions involving table adapters. In this new project using entity, I was using an entityDataSource for the grid view and figured it was the replacement for an object datasource. So the solution was to use again ObjectDataSources, and use the code above which manipulates the entities. I'm still not sure if this is proper coding, but its working for now.

Edit: The only thing bad about using business logic with the entity framework is that when you bind things like gridviews to ObjectDataSources that call on business logic, you have to disable Paging and Sorting. I have found that if you want paging and sorting you have to add a bunch more code to the business logic so it sorts and pages server-side verses the client side sorting that was supported with table adapters. Quite a pain, but probably better for performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top