谁能解释一下创建松散耦合视图模型的理论。
我在下面附上了一些示例代码来尝试解释我的意思。
我有 2 个示例类仅用于此示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Web.UI.Models
{
public class EmployerAddress
{
public string address { get; set; }
public string city { get; set; }
public string region { get; set; }
public string country { get; set; }
public string postZipCode { get; set; }
}
public class EmployerDetails
{
public string position { get; set; }
public string gender { get; set; }
public string dob { get; set; }
}
public class DisplayEmployerAddress : IDisplayEmployerAddress
{
public IEnumerable<EmployerAddress> employerAddr()
{
List<EmployerAddress> Data = new List<EmployerAddress>();
Data.Add(new EmployerAddress
{
address = "address1",
city = "city1",
region = "region1",
country = "country1",
postZipCode = "post zip1"
});
return Data;
}
}
public class DisplayEmployerDetails : IDisplayEmployerDetails
{
public IEnumerable<EmployerDetails> employerDetails()
{
List<EmployerDetails> Data = new List<EmployerDetails>();
Data.Add(new EmployerDetails
{
position = "trainee",
gender = "male",
dob = "22-08-1964"
});
Data.Add(new EmployerDetails
{
position = "trainee2",
gender = "male2",
dob = "22-08-1970"
});
return Data;
}
}
}
上面的代码有接口:
IEnumerable<EmployerAddress> employerAddr();
IEnumerable<EmployerDetails> employerDetails();
然后我使用 Ninject 来绑定上面的内容。
kernel.Bind<IDisplayEmployerAddress>().To<DisplayEmployerAddress>().InSingletonScope();
kernel.Bind<IDisplayEmployerDetails>().To<DisplayEmployerDetails>().InSingletonScope();
此时一切正常,我可以更改 DisplayEmployerAddress 等,只要所有方法等都匹配,代码仍然可以工作。
然后我创建一个视图模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Web.UI.Models
{
public class EmployerDetailsViewModel
{
public string age { get; set; }
public IEnumerable<EmployerAddress> EmployerAddress { get; set; }
public IEnumerable<EmployerDetails> EmployerDetails { get; set; }
}
}
但是现在这会导致一个问题,因为 EmployerAddress 现在是紧密耦合的,所以如果我更改代码,现在必须在 2 个地方更新它。
在我的控制器中,我有
public class HomeController : Controller
{
private readonly IDisplayEmployerAddress _address;
private readonly IDisplayEmployerDetails _details;
public HomeController(IDisplayEmployerAddress address,
IDisplayEmployerDetails details)
{
_address = address;
_details = details;
}
public ActionResult Index()
{
ViewBag.Title = "Title";
var Address = _address.employerAddr();
var Details = _details.employerDetails().AsEnumerable();
var Age = _details.employerDetails().FirstOrDefault().dob;
var employerModel = new EmployerDetailsViewModel
{
EmployerAddress = Address,
EmployerDetails = Details,
age = age.calAge(Age)
};
return View(employerModel);
}
我使控制器保持轻量级,因为我读过的所有书籍都说在控制器中保留尽可能少的代码,因此为了计算年龄,我使用了一个静态类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Web.UI.Models
{
public static class age
{
public static string calAge(string dob)
{
//Would cal age here
return "48";
}
}
}
所以我的问题是三个部分。
- 这个例子是正确的方法吗?
- 由于我的视图模型现在紧密耦合,我如何使其松散耦合。
如果我不想使用 foreach 循环,我如何才能从说 EmployerDetails 中获取每个项目
该雇主担任@Model.EmployerDetails.position 职位,性别为@Model.EmployerDetails.gender
<ul> @foreach (var d in Model.EmployerAddress) { <li>@d.address</li> <li>@d.city</li> <li>@d.country</li> <li>@d.region</li> <li>@d.postZipCode</li> } </ul> <ul> @foreach (var dd in Model.EmployerDetails) { <li>@dd.position</li> <li>@dd.gender</li> <li>@dd.dob</li> } </ul>
至今
该雇主担任@Model.EmployerDetails.position 职位,性别为@Model.EmployerDetails.gender
解决了问题 3,将代码更改为@Model.EmployerDetails.FirstOrDefault().position
希望上面的例子对我想要学习的东西有意义
谢谢
乔治