asp.net - 将我的 DAL 代码重构为领域驱动设计或更现代的设计 (C# 3.5)?

标签 asp.net architecture repository data-access-layer

开发仅限于 Visual Studio 2010(客户批准的软件)。我们需要通过存储过程访问数据。我想避免因日程安排过于复杂而变得过于复杂。我看到的大部分设计都涉及EF和LINQ,不知道如何设计procs?

我想创建一个单独的代码库项目(使用 Web UI):

Application.Domain
      -  Interact get/put stored procedures, entities

Application.Web
      - containing Web UI (JQuery, AJAX), WCF Service

任何人都可以给我有关如何访问 Application.Domain 的示例代码吗?

我读过的例子:

DAL\AppDAL.cs:

public static IEnumerable<TasCriteria> GetTasCriterias()
    {
        using (var conn = new SqlConnection(_connectionString))
        {
            var com = new SqlCommand();
            com.Connection = conn;
            com.CommandType = CommandType.StoredProcedure;

            com.CommandText = "IVOOARINVENTORY_GET_TASCRITERIA";
            var adapt = new SqlDataAdapter();
            adapt.SelectCommand = com;
            var dataset = new DataSet();
            adapt.Fill(dataset);

            var types = (from c in dataset.Tables[0].AsEnumerable()
                         select new TasCriteria()
                         {
                              TasCriteriaId = Convert.ToInt32(c["TasCriteriaId"]),
                              TasCriteriaDesc= c["CriteriaDesc"].ToString()
                         }).ToList<TasCriteria>();

            return types;
        }

    }

模型\TasCriteria.cs:

public class TasCriteria
    {
        public int TasCriteriaId { get; set; }
        public string TasCriteriaDesc { get; set; }
    }

服务\Service.svc:

 [OperationContract]
        [WebInvoke(ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.WrappedRequest, Method = "GET")]
        public List<TasCriteria> GetTasCriteriaLookup()
        {
            var tc = InventoryDAL.GetTasCriterias();
            return tc.ToList();
        }

最佳答案

如果你:

  • 日程安排很紧
  • 大部分业务逻辑已经通过存储过程/ View 位于数据库端
  • 之前没有使用过 EF

我建议你看看Microsoft Enterprise Library ,尤其是数据应用 block 。它将简化您的所有 DAL 功能(不使用任何 ORM 框架),并且在 Unity(Microsoft 的依赖注入(inject)容器)的帮助下遵循依赖倒置原则。

一些有用的数据应用程序 block 概念:

输出映射器

An output mapper takes the result set returned from a database (in the form of rows and columns) and converts the data into a sequence of objects.

// Create and execute a sproc accessor that uses default parameter and output mappings 
var results = db.ExecuteSprocAccessor<Customer>("CustomerList", 2009, "WA");

阅读全文 Retrieving Data as Objects主题。

参数映射器

A parameter mapper takes the set of objects you want to pass to a query and converts each one into a DbParameter object.

// Use a custom parameter mapper and the default output mappings
IParameterMapper paramMapper = new YourCustomParameterMapper();
var results = db.ExecuteSprocAccessor<Customer>("Customer List", paramMapper, yourCustomParamsArray);

对于实体生成,我会尝试使用这个 tool 。它根据存储过程返回的结果集构建 POCO 类。我还没有尝试过这个工具,也许有更好的选择,但它可以帮助您开始,所以您不必手动执行此操作。

如果您使用的是 .NET 3.5,则必须使用 Enterprise Library 5.0。

我希望这能引导您走向正确的方向。

关于asp.net - 将我的 DAL 代码重构为领域驱动设计或更现代的设计 (C# 3.5)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23813905/

相关文章:

c# - asp.net mvc 5异步 Action 方法

python - DDD 与 Python : did I get it right?

web-services - 什么是SOA(面向服务的架构)?

windows - 如何在 Windows 上设置 git?

javascript - 使用 npm 将 fork 存储库安装为依赖项时出错

c# - 如何在 ASP.NET Core Web Api 中完成版本控制

c# - 将具有多列的 GridView 显示为两组列

repository - 创建本地 debian 存储库

javascript - 在asp.net中从客户端浏览器向服务器发送文件有哪些方法?

php - 来自一个路由脚本 : what are the pros and cons? 的 Web 应用程序