设置正确的 nTier 应用程序

发布于 2024-09-14 10:21:00 字数 2588 浏览 1 评论 0原文

虽然我知道有很多方法可以做到这一点,但我只是想知道这是否离谱。

我有一个包含三个 DLL、UI(asp.net Web 应用程序)、业务层和 DAL 的解决方案。所以我的代码主要如下所示(非常原始的示例代码):

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

    }
}

数据提供程序(业务 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 接口

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

数据访问

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);
        }
    }
}

我在设置中遇到的主要问题是 DBProvider 类。出于某种原因,它困扰着我,我似乎无法弄清楚为什么,或者摆脱它并继续前进。这几乎就像它导致了我的作家的阻碍。我有同样的模式在另一个应用程序中运行得很好,一切都很好,但似乎有很多额外的代码没有任何好处。

任何提示都会有帮助。

另外,我现在正在开发 3.5,但是一旦我可以获得 VS 2010,就考虑迁移到 4.0。

编辑:周末刚刚拿起 VS 2010,所以我将应用程序迁移到 4.0,希望有更好的 EF 或LINQ 到 SQL 支持。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎无痕… 2024-09-21 10:21:00

我完全同意吉米·霍法的观点。您不应该在 2010 年进行自定义数据访问,除非您有一个非常充分的理由。您会感到非常头疼,并且在您将在未来几年内发誓之后,任何必须支持该产品的开发人员都会感到头疼;)

像 nHibernate 和 nHibernate 这样的 ORM。实体框架甚至 Codesmith 或 T4 的代码生成很久以前就解决了这个问题。

整个 n 层堆栈现在几乎是一种商品:

ORM ->网络服务 -> UI,

如果您是一家仅限 MS 的商店,则相当于:
EF-> WCF(领域服务/数据服务)-> ASP.NET(MVC)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文