gpt4 book ai didi

c# - 对许多表使用与 Controller 相同的类

转载 作者:行者123 更新时间:2023-11-30 21:55:36 25 4
gpt4 key购买 nike

我是 ASP.NET 和 Azure 移动服务的新手。

有一些问题:

1)我使用TodoItemController从azure表存储中查询数据(刚刚使用了他们的示例类,如下所示)我如何修改它,以便它充当所有表的通用类,而不仅仅是一个表。例如:如果我有另一个名为 person 的表,除了 Todo我希望它对两个表使用相同的类

2)我的方法是否建议采用糟糕的设计模式?如果是,为什么?

3)我也不明白这个类是如何被调用的。在某处看到../tables/Todo映射到这个类。如果是这样的话。映射是在哪里完成的?

4)ApiController 能实现我的目的 1吗?如果是的话,请举个例子

using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.OData;
using Microsoft.WindowsAzure.Mobile.Service;
using TempService.DataObjects;
using TempService.Models;
using System.Web.Http.OData.Query;
using System.Collections.Generic;

namespace TempService.Controllers
{
public class TodoItemController : TableController<TodoItem>
{

protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);

// Create a new Azure Storage domain manager using the stored
// connection string and the name of the table exposed by the controller.
string connectionStringName = "StorageConnectionString";
var tableName = controllerContext.ControllerDescriptor.ControllerName.ToLowerInvariant();
DomainManager = new StorageDomainManager<TodoItem>(connectionStringName,
tableName, Request, Services);
}

public Task<IEnumerable<TodoItem>> GetAllTodoItems(ODataQueryOptions options)
{
// Call QueryAsync, passing the supplied query options.
return DomainManager.QueryAsync(options);
}

// GET tables/TodoItem/1777
public SingleResult<TodoItem> GetTodoItem(string id)
{
return Lookup(id);
}

// PATCH tables/TodoItem/1456
public Task<TodoItem> PatchTodoItem(string id, Delta<TodoItem> patch)
{
return UpdateAsync(id, patch);
}

// POST tables/TodoItem
public async Task<IHttpActionResult> PostTodoItem(TodoItem item)
{
TodoItem current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}

// DELETE tables/TodoItem/1234
public Task DeleteTodoItem(string id)
{
return DeleteAsync(id);
}
}

}

最佳答案

所以我会尝试逐点解决您的问题:

  1. 是也不是。您要做的就是遵循存储库模式。所以是的,您可以创建一个 BaseRepository 来使用通用数据类型完成大部分工作。不,您仍然会有继承基类但指定通用数据类型的类。

  2. 不,这不是一个糟糕的设计模式。

  3. 所以TableController是一个专门针对数据表的ApiController。它是通过转换 URL“/tables/TodoItem/Id”的路由配置调用的

  4. 同样,TableController 是一个 ApiController。不确定它是否有帮助,但有许多 Azure 移动服务的“存储库模式”示例。您可以看看这里来了解一下: http://www.codeproject.com/Articles/574357/Repository-Pattern-with-Windows-Azure-Mobile-Servi

关于c# - 对许多表使用与 Controller 相同的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32157737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com