tags:

views:

67

answers:

1

While I know there are many ways of doing this, I'm just wondering if this is way off base.

I have a solution that has three DLL's, UI (asp.net web application), Business layer, and DAL. So my code mainly looks like this (very raw example code):

UI

protected void Page_Load(object sender, EventArgs e)
{
    Beanpole.Business.Project myProject = new Beanpole.Business.Project();
    myProject.LoadProject(Convert.ToInt32(Request["id"].ToString()));

    Response.Write(myProject.ProjectName + "<br>" + myProject.ProjectDescription);
}

BLL

using ...
using Business.Providers;

namespace Business
{
    public class Project
    {
        public string ProjectName { get; set; }
        public string ProjectDescription { get; set; }

        public bool LoadProject(int projectId)
        {
            DataTable dt = DBProvider.Instance().LoadProject(projectId).Tables[0];

            if (dt.Rows.Count == 0)
                return false;

            LoadProjectFromRow(dt.Rows[0]);
            return true;
        }

        internal void LoadProjectFromRow(DataRow row)
        {
            this.ProjectName = (string)row["Name"];
            this.ProjectDescription = (string)row["Description"];
        }

    }
}

Data provider (Business dll)

namespace Business.Providers
{
    public class DBProvider
    {
        private static IDataAccess _getDataAccessComponent = null;
        public static IDataAccess Instance()
        {
            if (_getDataAccessComponent == null)
            {
                const string className = "My.Data.DataAccess, My.Data";

                _getDataAccessComponent = (IDataAccess)Activator.CreateInstance(Type.GetType(className));
            }
            return _getDataAccessComponent;
        }
    }
}

DAL Interface

namespace My.Data
{
    public interface IDataAccess
    {
        DataSet LoadProject(int projectId);
    }
}

Data access

namespace My.Data
{
    public class DataAccess : IDataAccess
    {
        public DataSet LoadProject(int projectId)
        {
            SqlParameter[] _params = new SqlParameter[1];
            _params[0] = new SqlParameter("@ProjectId", SqlDbType.Int) { Value = projectId };

            return SqlHelper.ExecuteDataset(connString, "up_LoadProject", _params);
        }
    }
}

The main issue I have with this set up is the DBProvider class. It bothers me for some reason and I just can't seem to figure out why, or shake it and keep going. It's almost like it's causing my writers block. I have this same pattern working very well in another application and all is good, but it seems like a lot of extra code for no gain.

Any tips would be helpful.

Also I'm working on 3.5 right now, but thinking of moving to 4.0 once I can get VS 2010.

Edit: Just picked up VS 2010 over the weekend, so I'm moving the app over to 4.0 in hopes of better EF or LINQ to SQL support.

A: 

I'm going to agree totally with Jimmy Hoffa. You should not be doing custom data access in 2010, unless you have an amazingly good reason. You are trebling your headache and any developer that has to support that product after you is going to be swearing your name for years to come ;)

ORMs like nHibernate & Entity Framework or even code gen with Codesmith or T4 solved this problem a long time ago.

The whole n-tier stack is pretty much a commodity now:

ORM -> Web Services -> UI,

which if you're an MS only shop equals: EF -> WCF (domain services / data services) -> ASP.NET (MVC)

Doobi
Alright, maybe it's just me but I guess I just don't understand EF and how to hook it up to the front end like I do the above code. I like the idea of not writing all that data access code, but it seems almost easier. By the way the app as of now will be using WebForms with some WCF, not MVC. But thats subject to change if I can get the chance to learn MVC. So do you have any good examples of using EF with WCF on webforms?
Tim Meers
I really like using WCF Data Services, you go from database to entity model to web access in minutes;http://msdn.microsoft.com/en-us/data/ee720180.aspx
Doobi