面向未来的 DAL

发布于 2024-10-12 19:56:02 字数 619 浏览 7 评论 0 原文

我们正处于一个非常漫长的开发项目的开始阶段,其中有几个子项目。基本上每个子项目都需要几个月的时间来开发。代码本身将被拆分为多个 C# 项目,但物理数据库将由所有项目共享。

问题是可维护性。如果我们向表中添加一列,或者将表拆分为两个较小的表,则必须返回并修改 C# DAL 以支持这些更改。这是不可接受的,因为我们将不断调整数据库以满足整个公司的需求,而不仅仅是单个程序的需求。不断更改旧代码将是一项永无休止的任务。

我们的数据库人员提出了不同的观点。我们通过存储过程完成所有 CRUD,并在多个表中使用 Linq 来执行 SELECT 语句。然后,如果我们在几年后重组数据库,我们可以简单地提供相同的存储过程和视图,而不必修改我们的旧代码。

我们的问题是,对于这样的事情应该使用什么 ORM? EF 似乎有点大材小用(也许不是)。像 SubSonic 这样的带有 T4 模板的东西会允许更简单(也许更快)的 DAL 吗?

或者也许有人知道如何让整个过程不那么痛苦?我们不想在应用程序中添加另一层,但我们也不想每次更改数据库时都返回并修改代码。

编辑1: 所以当我说“我真的不想添加更多层”时。这主要是因为我们已经有几个层了。我们有 Silverlight 视图、视图模型、BLL 对象(通过 CSLA),然后有 DAL,最后是 SQL 表本身。

We are in the beginning of a really long development project with several sub projects. Basically each sub-project will take several months to develop. The code itself will be split up into several C# projects, but the physical database will be shared by all projects.

The problem is maintainability. If we add a column to a table, or split a table into two smaller tables, we'll have to go back and modify our C# DAL to support these changes. This is not acceptable, as we'll constantly be adapting the DB conform to the needs of the company as a whole, and not just the needs of a single program. Constantly changing old code would be a neverending task.

Our DB people suggested a different view. We do all our CRUD through stored procedures, and use Linq across several tables to perform our SELECT statements. Then if we restructure the DB several years from now, we can simply supply the same stored procs and views and not have to modify our older code.

The question we have, is what ORM should be used for something like this? EF seems a bit overkill (maybe it's not). Would something like SubSonic with it's T4 templating allow for a simpiler (and perhaps faster) DAL?

Or perhaps someone has an idea on how to make this entire process less painful? We'd rather not add another layer to our application, but neither do we want to go back and modify code everytime we make a db change.

Edit 1:
So when I said "I don't really want to add more layers". This is mostly because we already have several layers. We have Silverlight views, view-models, BLL objects (via CSLA) then we have the DAL, and finally the SQL tables themseleves.

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

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

发布评论

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

评论(3

静待花开 2024-10-19 19:56:02

C# DAL...不仅仅是单个程序的需求。 C# DAL 作为单独的程序集的全部意义在于它可以在任何类型的 .NET 应用程序中重用。您将遇到的主要问题是,如果数据库发生更改,则 DAL 必须更改(一次),然后所有依赖于 DAL 的应用程序都必须使用新的 DAL 重新部署。您还存在非 .NET 应用程序无法使用 DAL 的问题。

好的,那么如何集中 DAL 以便不必为每个应用程序重新部署它呢?想想 SOA。您可以构建一个 WCF 服务来包含 DAL(也可能是 BLL)。您的所有应用程序(如果您使用 Web 服务,甚至那些不是 .NET 的应用程序)都可以使用此服务。当数据库发生更改时,您可以更新 WCF 服务并部署一次。只要确保您没有进行任何重大更改即可!如果您需要添加/更改功能,请创建 MyMethod2。

注意:当您听到 n-tier 时,通常是
指的是三层,其中每层
是独立的软件并且通常在
单独的服务器:演示(UI),
应用程序(您的 BLL/DAL)、数据(您的
SQL 数据库)。这种架构有其优点。

我们不想向我们的应用程序添加另一层。好的,所以三层可能不是您的情况的最佳方法。

我们不想每次更改数据库时都返回并修改代码那么您的 DBA 人员建议的就是唯一的方法。

但是,请考虑一下:更改存储过程与修改代码相同吗?基本上是同一件事。 SQL 存储过程通常不受版本控制或测试,但它们应该如此。 SQL 不具备 .NET 等语言的丰富性。 WCF 可以在网络场中轻松扩展。一旦考虑了这些和其他原因,也许值得采用三层/SOA 方法。

这实际上取决于您的项目规模、员工技能、未来发展等,而这只有您才能决定。

C# DAL... not just the needs of a single program. The whole point of a C# DAL as a separate assembly is that it can be reused across any type of .NET application. The main problem you'll encounter is that if the database changes, then the DAL must change (once), then all applications that depend on the DAL must be re-deployed with the new DAL. You also have the problem that the DAL can't be used by non-.NET applications.

Ok, so how can you centralize the DAL so that you don't have to re-deploy it for every application? Think SOA. You can build a WCF service to contain the DAL (and probably BLL). All your applications (if you use web services, even those that are not .NET) can use this service. When the database changes, you update your WCF service and deploy once. Just make sure you don't make any breaking changes! Create a MyMethod2 if you need to add/change functionality.

Note: When you hear n-tier, it usually
refers to three-tier, where each tier
is separate software and usually on
separate servers: presentation (UI),
application (your BLL/DAL), data (your
SQL database). There is merit to this architecture.

We'd rather not add another layer to our application. Ok, so three-tier may not be the best approach in your case.

neither do we want to go back and modify code everytime we make a db change Then what your DBA people suggested is the only way.

However, consider this: is changing a stored procedure the same as modifying code? It basically is the same thing. SQL stored procedures are often not version-controlled or tested, but they should be. SQL doesn't have the richness of a language like .NET. WCF can easily be scaled in a web farm. Once you factor these and other reasons, it may be worth going the three-tier/SOA approach.

It really depends on the size of your project, skills of staff, future growth, etc. and that is something only you can determine.

吻泪 2024-10-19 19:56:02

我已经开始根据 BLToolkit rel="nofollow">http://ormeter.net/

您可以在简单的类文件中定义您的模型,添加一些应用了属性的方法,然后您就拥有了 DAL。将表分成两部分,您可以维护原始类文件,同时创建新的类文件来支持拆分表。只需确保您创建一个测试项目,该项目能够命中每个方法,以确保它们都适用于每个版本

    public class DirectoryListing
    {
        [PrimaryKey, Identity]
        public Int64 Id { get; set; }
        public Int64? OldId { get; set; }
        public Int32 CategoryId { get; set; }
        [Nullable]
        public String CompanyName { get; set; }
}

常规选择或表值函数:

[SqlQuery("SELECT * FROM Ajax_CategorySearch(@SearchString, @ResultCount)")]
[Cache(MaxCacheTime = 10, IsWeak = false)]
public abstract List<String> AjaxCategorySearch(String @SearchString, Int32 @ResultCount = 10);

或者使用存储过程:

[ActionName("SelectById")]
public abstract Model.DirectoryListing SelectById(Int64 @Id);

这将调用 SP DirectoryListing_SelectById

哦,并且在更多中执行操作经典方法也很简单

    using (BIFDbManager db = new BIFDbManager())
    {
        var output = db.SetCommand(
            "SQL GOES HERE",
            db.Parameter("@Id", 1))
            .ExecuteList<DAL.Model.DirectoryListing>();

        totalrecords = output.Count();

        return output;
    }

最后一个难题是数据库管理器,它还支持 LINQ。

public class BIFDbManager : DbManager
{
    public BIFDbManager() : base("Connection string name") { }

    public Table<DirectoryListing> DirectoryListings { get { return GetTable<DirectoryListing>(); } }
}

I have started to use BLToolkit based on the performance info from http://ormeter.net/

You can define your model in simple class files, add some methods with attributes applied and presto you have a DAL. Split a table in two and you can maintain the original class file while creating the new ones to support the split tables. Just make sure you create a testing project that hits every method to make sure they all work with each release

Class

    public class DirectoryListing
    {
        [PrimaryKey, Identity]
        public Int64 Id { get; set; }
        public Int64? OldId { get; set; }
        public Int32 CategoryId { get; set; }
        [Nullable]
        public String CompanyName { get; set; }
}

General select or table valued function:

[SqlQuery("SELECT * FROM Ajax_CategorySearch(@SearchString, @ResultCount)")]
[Cache(MaxCacheTime = 10, IsWeak = false)]
public abstract List<String> AjaxCategorySearch(String @SearchString, Int32 @ResultCount = 10);

Or to use a stored proc:

[ActionName("SelectById")]
public abstract Model.DirectoryListing SelectById(Int64 @Id);

This will call the SP DirectoryListing_SelectById

Oh, and doing things in a more classic way is easy too

    using (BIFDbManager db = new BIFDbManager())
    {
        var output = db.SetCommand(
            "SQL GOES HERE",
            db.Parameter("@Id", 1))
            .ExecuteList<DAL.Model.DirectoryListing>();

        totalrecords = output.Count();

        return output;
    }

The last piece of the puzzle is the db manager, which also enables LINQ support.

public class BIFDbManager : DbManager
{
    public BIFDbManager() : base("Connection string name") { }

    public Table<DirectoryListing> DirectoryListings { get { return GetTable<DirectoryListing>(); } }
}
给不了的爱 2024-10-19 19:56:02

没有数据库你能工作多久?如果有问题,晚点再介绍。是的,这可能意味着您添加一个层。

How long can you work without the database? If it is a problem, introduce it late. And yes, that probably means you add a layer.

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