当前位置:首页 > 行业动态 > 正文

Asp.net三层架构如何实现复杂多条件检索功能?

csharp,// 创建DAL层,BLL层和UI层,通过传递条件对象到BLL层,再由BLL层调用DAL层实现多条件检索。,

# ASP.NET 三层架构实现多条件检索示例

ASP.NET 应用开发中,采用三层架构(表示层、业务逻辑层、数据访问层)可以有效地分离关注点,提高代码的可维护性和可扩展性,以下是一个使用三层架构实现多条件检索的示例。

## 一、数据库设计

假设我们有一个名为 `Products` 的表,包含以下列:`ProductID`(主键)、`ProductName`、`Category`、`Price`、`InStock`。

| 字段名 | 数据类型 | 说明 |

|————|———-|————|

| ProductID | int | 产品编号 |

| ProductName| nvarchar | 产品名称 |

| Category | nvarchar | 产品类别 |

| Price | money | 价格 |

| InStock | bit | 是否库存 |

## 二、数据访问层(DAL)

1. 创建数据模型类

“`csharp

public class Product

public int ProductID { get; set; }

public string ProductName { get; set; }

public string Category { get; set; }

public decimal Price { get; set; }

public bool InStock { get; set; }

“`

2. 创建数据访问接口

“`csharp

public interface IProductRepository

List GetProducts(string category, string productName, decimal? minPrice, decimal? maxPrice, bool? inStock);

“`

3. 实现数据访问接口

“`csharp

public class ProductRepository : IProductRepository

private readonly string _connectionString;

public ProductRepository(string connectionString)

{

_connectionString = connectionString;

}

public List GetProducts(string category, string productName, decimal? minPrice, decimal? maxPrice, bool? inStock)

{

var products = new List ();

string query = “SELECT FROM Products WHERE 1=1”;

if (!string.IsNullOrEmpty(category))

{

query += ” AND Category = @Category”;

}

if (!string.IsNullOrEmpty(productName))

{

query += ” AND ProductName LIKE @ProductName”;

}

if (minPrice.HasValue)

{

query += ” AND Price >= @MinPrice”;

}

if (maxPrice.HasValue)

{

query += ” AND Price

}

if (inStock.HasValue)

{

query += ” AND InStock = @InStock”;

}

using (SqlConnection connection = new SqlConnection(_connectionString))

{

SqlCommand command = new SqlCommand(query, connection);

if (!string.IsNullOrEmpty(category))

{

command.Parameters.AddWithValue(“@Category”, category);

}

if (!string.IsNullOrEmpty(productName))

{

command.Parameters.AddWithValue(“@ProductName”, “%” + productName + “%”);

}

if (minPrice.HasValue)

{

command.Parameters.AddWithValue(“@MinPrice”, minPrice.Value);

}

if (maxPrice.HasValue)

{

command.Parameters.AddWithValue(“@MaxPrice”, maxPrice.Value);

}

if (inStock.HasValue)

{

command.Parameters.AddWithValue(“@InStock”, inStock.Value);

}

connection.Open();

SqlDataReader reader = command.ExecuteReader();

while (reader.Read())

{

Product product = new Product

{

ProductID = (int)reader[“ProductID”],

ProductName = (string)reader[“ProductName”],

Category = (string)reader[“Category”],

Price = (decimal)reader[“Price”],

InStock = (bool)reader[“InStock”]

};

products.Add(product);

}

}

return products;

}

“`

## 三、业务逻辑层(BLL)

1. 创建业务逻辑接口

“`csharp

public interface IProductService

List SearchProducts(string category, string productName, decimal? minPrice, decimal? maxPrice, bool? inStock);

“`

2. 实现业务逻辑接口

“`csharp

public class ProductService : IProductService

private readonly IProductRepository _productRepository;

public ProductService(IProductRepository productRepository)

{

_productRepository = productRepository;

}

public List SearchProducts(string category, string productName, decimal? minPrice, decimal? maxPrice, bool? inStock)

{

return _productRepository.GetProducts(category, productName, minPrice, maxPrice, inStock);

}

“`

## 四、表示层(UI)

在表示层(MVC 控制器或 Web Forms 页面),我们可以调用业务逻辑层的方法来获取满足条件的产品列表,并将其展示给用户,以下是一个简单的示例(以 MVC 为例):

1. 控制器代码

“`csharp

public class ProductController : Controller

private readonly IProductService _productService;

public ProductController(IProductService productService)

{

_productService = productService;

}

public ActionResult Search(string category, string productName, decimal? minPrice, decimal? maxPrice, bool? inStock)

{

List products = _productService.SearchProducts(category, productName, minPrice, maxPrice, inStock);

return View(products);

}

“`

2. 视图代码(Search.cshtml)

“`html

@model IEnumerable

搜索结果

@foreach (var product in Model)

{

}

产品编号 产品名称 类别 价格 是否库存
@product.ProductID @product.ProductName @product.Category @product.Price @product.InStock

“`

通过以上三层架构的实现,我们可以方便地进行多条件检索,并且各层职责明确,易于维护和扩展,如果需要更改数据库访问方式(如从 SQL Server 改为 MySQL),只需修改数据访问层的实现即可,业务逻辑层和表示层无需做太多改动,这种架构也便于进行单元测试,可以在不依赖数据库的情况下对业务逻辑层和数据访问层进行测试。

0