开发者

Setting up a proper nTier application

开发者 https://www.devze.com 2023-01-12 00:33 出处:网络
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

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";

                _getDataAccessCompon开发者_开发问答ent = (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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号