将 M 添加到 MVC?从 Web 窗体/数据表转换

本文关键字:窗体 Web 数据表 转换 添加 MVC | 更新日期: 2023-09-27 18:37:09

在编写了多年的网络表单之后,我正在尝试过渡到 MVC。 我在摆脱数据表和了解如何正确创建模型类时遇到麻烦。 最大的问题是我的数据源是一个返回XML的宁静Web服务。

目前,我强烈输入视图的模型作为数据表。 我是否因为不使用模型类而放弃了任何东西? 我是否在控制器中包含大量信息?

以下只是一个简单的应用程序,它采用查询字符串值,使用 restful Web 服务执行搜索,并在页面上吐出结果。 它可以工作,但我觉得我没有创建一个实际的 MVC 应用程序。

我的控制器:

using System.Web.Mvc;
using System.Data;
using System.Configuration;
namespace mvc1.Controllers
{
    public class HomeController : Controller
    {
        dhRestful _datahandler = new dhRestful(ConfigurationManager.ConnectionStrings["service"].ConnectionString);
        //
        // GET: /Home/
        public ActionResult Index(string search = "")
        {
            ViewBag.Title = "You searched for: " + search;
            using (DataTable dt = _datahandler.get("tv_show?name=" + search))
            {
                return View(dt);
            }
        }
    }
}

dhRestful是我从以前的项目移植的一个帮助类。 它进行 restful Web 服务调用,并将响应 XML 序列化为数据表。 如果我应该继续使用这个类,我应该把它放在项目文件中的什么位置?

using System.Data;
using System.Xml;
namespace mvc1
{
    public class dhRestful
    {
        string _hostPath = "";
        public dhRestful(string hostPath)
        {
            _hostPath = hostPath;
        }
        public DataTable get(string partialUrl)
        {
            using (XmlTextReader xr = new XmlTextReader(_hostPath + partialUrl))
            {
                using (DataSet ds = new DataSet())
                {
                    ds.ReadXml(xr);
                    if (ds.Tables.Count > 0)
                    {
                        return ds.Tables[0];
                    }
                    else
                    {
                        return new DataTable();
                    }
                }
            }
        }
    }
}

使用剃须刀的视图:

@model System.Data.DataTable
<h2>@ViewBag.Title</h2>
<table border="1">
    <thead>
        <tr>
            <th>ID</th>
            <th>Show Name</th>
        </tr>
    </thead>
    <tbody>
        @foreach (System.Data.DataRow row in Model.Rows)
        {
            <tr>
                <td>@row["id"].ToString()</td>
                <td>@row["name"].ToString()</td>
            </tr>
        }       
    </tbody>
</table>

搜索名称"星际之门"的示例 XML:

<myService>
  <tv_show>
    <id>72449</id>
    <name>Stargate SG-1 </name>
  </tv_show>
  <tv_show>
    <id>83237</id>
    <name>Stargate Universe </name>
  </tv_show>
  <tv_show>
    <id>70852</id>
    <name>Stargate: Infinity </name>
  </tv_show>
  <tv_show>
    <id>70851</id>
    <name>Stargate Atlantis </name>
  </tv_show>
</myService>

将 M 添加到 MVC?从 Web 窗体/数据表转换

我建议您通过依赖注入在组合根目录编写此数据访问层。因此,稍后您可以注入满足合约的不同客户端,而不是休息客户端。

另一个注意事项是创建视图模型。如果您是直接在应用程序中使用的 VO 或 DTO,则最好将它们包装起来。我想这就是你的答案。您可以在模型文件夹中查看模型,也可以创建子文件夹等。

像这样:

   // you need to adjust your dependency accordingly
   Interface IRepository<T> : where t has a value
   {
     void Set(T item);
     T Get(some value that you indicate in T);
   }
    namespace mvc1.Controllers
    {
        public class HomeController : Controller
        {
            IRepository<SomeObject> _repo;
            public HomeController(IRepository<SomeObject> repository)
            {
               _repo = repository;
            }             

            //
            // GET: /Home/
            public ActionResult Index(string search = "")
            {
                ViewBag.Title = "You searched for: " + search;
                using (SomeObject obj = _repo.get("tv_show?name=" + search))
                {
                    // here you can use automapper to map a DTO or VO to View model.
                    FooViewModel model = someobject;
                    return View(model);
                }
            }
        }
    }

在组合根:global.asax,你可以引导容器。有很多方法可以做到这一点,看看结构图和如何使用容器。