在 MVC 应用程序中放置国家/地区列表的最佳实践位置在哪里?

发布于 2024-12-28 20:38:00 字数 1118 浏览 2 评论 0原文

在复杂的环境应用程序中,我们有用户、位置、等等等等,并且在许多情况下我们使用对服务的调用来检索国家/地区列表。

实现这一点的“最佳实践”或“正确方法”在哪里?此方法在多个地方被调用,并且许多对象都有 List 属性。

特别考虑使用 Razor 视图,通常必须将此属性添加到 ModelViews

解决方案是使用 DAL / BLL / SERVICE / UI[s] 架构。

真实示例:

public class User {
...
...
public List<DeliveryZoneVO> DeliveryZones {get;set;}
    public User() {
    ...
        DeliveryZones = service.GetDeliveryZones().ToList();
    }
}

DeliveryZoneVO 类来自 Web 服务,因此一个属性是

int IdCountry

User 类有一个在类上呈现的 DeliveryZoneVO 列表,这里的“问题”是因为它从 Web 服务检索数据,所以我只有国家的身份证。

当我在控制器中准备数据以发送到视图时:

UserModelView userMV = new UserModelView();
userMV.user = service.GetUserById(1);
ViewData.Model = userMV;

但是,在 userMV.user 内部,我有 DeliveryZones,其中包含带有 IdCountries 的 DeliveryZoneVO 对象列表。

在视图中,当我这样做时(例如):

@DisplayFor(m => m.user.DeliveryZones)

我想显示国家/地区名称,只有 ID。所以我需要在某个地方提供参考......问题在于应该将数据放置在哪里,这被认为是最佳实践。

在所有模型视图中(在本例中, UserModelView() 是否具有带有 List 的属性 Country ?

In a heavy enviroment application, we have Users, Locations, bla bla bla... and we use in many situations a call to a service where we retrieve the list of countries.

Where is the 'best practice' or 'proper way' to implement this. This method is called in several places and many objects has a List<CountryVO> property.

Specially considering using Razor views an often having to add this property to ModelViews

The solution is using DAL / BLL / SERVICE / UI[s] architecture.

Real Example:

public class User {
...
...
public List<DeliveryZoneVO> DeliveryZones {get;set;}
    public User() {
    ...
        DeliveryZones = service.GetDeliveryZones().ToList();
    }
}

The class DeliveryZoneVO comes from a webservice, so one property is

int IdCountry

The class User have a list of DeliveryZoneVO as presented on the class, the 'problem' here, is since it retrieves the data from a web service, I only have the ID of the country.

When I prepare the data in the controller to send to the View:

UserModelView userMV = new UserModelView();
userMV.user = service.GetUserById(1);
ViewData.Model = userMV;

BUT, inside userMV.user, I have DeliveryZones with a list of DeliveryZoneVO objects with IdCountries.

In the view, when I do (for example) :

@DisplayFor(m => m.user.DeliveryZones)

I want to show the Country Name, only have the ID. So i need a reference somewhere.. the question lies in where should that data needs to be placed that is considered BEST PRACTICES.

Is having in all modelview (in the case of the example, the UserModelView() the property Countries with a List ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朕就是辣么酷 2025-01-04 20:38:00

一件好事,因为此类问题是有一个从 controller 派生的 BaseController 类,以及从它派生的所有其他控制器。

BaseController 中放置一个仅带有 getter 的静态 List 属性,这样它将被初始化一次,并且可以被您的所有控制器和视图访问(如果您使用 ViewModelViewBag 传递它)。


例子:

public class BaseController : Controller
{
    private static List<CountryVO> _allCountries;

    public static List<CountryVO> AllCountries
    {
        get{ return _allCountries ?? _GetCountriesFromSomeWhere();}
    }       
}

public class HomeController : BaseController 
{
    public ActionResult Index()
    {
        ViewBag.AllCountries  = this.AllCountries;          
        return View();
    }
}

A good thing because this kind of issues is to have a BaseController class that derived from controller, and all the other controllers you have derived from it.

in the BaseController put a static List<CountryVO> property with getter only, this way it will be initialized once and will be accessible to all of your's controllers and views(If you pass it with the ViewModel or ViewBag).


Example:

public class BaseController : Controller
{
    private static List<CountryVO> _allCountries;

    public static List<CountryVO> AllCountries
    {
        get{ return _allCountries ?? _GetCountriesFromSomeWhere();}
    }       
}

public class HomeController : BaseController 
{
    public ActionResult Index()
    {
        ViewBag.AllCountries  = this.AllCountries;          
        return View();
    }
}
划一舟意中人 2025-01-04 20:38:00

我将创建一个部分视图,负责渲染国家/地区列表。然后,对列表呈现方式的任何更改都可以在一处进行。我将创建一个模型类,它封装调用服务来获取国家/地区。假设国家/地区列表相当静态,您可以在模型类中处理信息缓存,以减少对服务的调用并获得更好的性能。下面是模型中从服务器缓存获取国家/地区列表(如果可用)的方法示例。

    const string cacheId = "deliveryZones";

    public List<DeliveryZoneVO> GetDeliveryZones()
    {
        List<DeliveryZoneVO> deliveryZones = (List<DeliveryZoneVO>)HttpRuntime.Cache.Get(cacheId);
        if (deliveryZones == null)
        {
            deliveryZones = service.GetDeliveryZones().ToList();
            System.Web.HttpContext.Current.Cache.Insert(cacheId, deliveryZones);
        }
        return deliveryZones;

    }

I would create a partial view that is responsible for just rendering the country list. Then any changes to how the list is rendered is can be made in just one place. I would create a model class that encapsulates calling the service to get the countries. Assuming that the country list is fairly static you could handle caching of the information in the model class for less calls to the service and better performance. Below is an example of a method in the model that gets the country list from the server cache if it is available.

    const string cacheId = "deliveryZones";

    public List<DeliveryZoneVO> GetDeliveryZones()
    {
        List<DeliveryZoneVO> deliveryZones = (List<DeliveryZoneVO>)HttpRuntime.Cache.Get(cacheId);
        if (deliveryZones == null)
        {
            deliveryZones = service.GetDeliveryZones().ToList();
            System.Web.HttpContext.Current.Cache.Insert(cacheId, deliveryZones);
        }
        return deliveryZones;

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文