gpt4 book ai didi

c# - MVC 业务逻辑访问模型

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

我目前在 ASP.NET MVC 4 中工作,同时使用 Entity Framework 。我以前从未使用过 MVC,并且构建了一个包含以下层的项目:

  • GUI(模型、 View 、 Controller 、...)
  • DAL(实体数据库和存储库 + IRepository)
  • BL(与我的查询)
  • 常见
  • WCF
  • 测试

现在,我正在尝试在我的 BL 中编写一个查询,它使用来 self 的 GUI 层的模型。由于您不能简单地引用 GUI,所以我真的不知道该怎么做。

这是我正在尝试的代码:

在我的 BL 中:

public static List<PSNAdres> GetAdres(IZoekRepository repo)
{
List<PSNAdres> lijstTypes = (from t in repo.PSNAdres
select new PSNAdres {t.Gemeente, t.Straat, t.Postcode}).ToList();

return lijstTypes;
}

正如我所说:我是 MVC 的新手。这种分层布局是我们的 MVC 专家告诉我们使用的,因为他就是这样工作的。

最佳答案

我不确定你的要求是什么,但这是我的答案。呈现地址列表的简单场景:

查看模型:

public class AddressListViewModel 
{
public List<AddressViewModel> AddressList { get; set; }
public bool CanAdd { get; set; }
}

public class AddressViewModel
{
public string Country { get; set; }
public string City { get; set; }
public string Street { get; set; }
public bool CanEdit { get; set; }
public bool CanDelete { get; set; }
}

BL:

public static List<Address> GetAddressList()//Doesn't take repository here, repositories are injected in constructor
{
return adresRepo.PSNAdres.ToList();
}

Controller

public class AddressController : BaseController
{
public ActionResult List()
{
var addressList = blObject.GetAddressList();
var model = new AddressListViewModel();
model.AddressList = addressList.Select(a =>
new AddressViewModel
{
Country = a.Country.Name,
City = a.City,
Street = a.Street,
CanDelete = ...check user access here...,
CanEdit = ...check user access here...
});

model.CanAdd = ...check user access here...

return View(model);
}
}

关于c# - MVC 业务逻辑访问模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10137441/

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