MVC用户格式输出
我有一个 C# MVC 项目,允许用户选择输出数据的方式(即以 kmh/mph 为单位的速度,以 CST、EST 等为单位的时间,以 KMs、英里、米、赫克特等为单位的距离) .)。登录时,我不会每次都跑回数据库来检索设置,而是将其填充到加密的 cookie 用户数据部分中。我还使用服务存储库模式,其中我的服务在 BLL 中实现,模型在 BOL 中定义。我还将数据作为 JsonResults 返回(使用 NewtonSoft 库)。将数据转换为用户指定类型而不将此单元首选项信息传递到业务逻辑层的最佳策略是什么(在何处以及如何)?我最初考虑使用 customAttributes 并编写自己的 CustomJsonResult actionResult,它将使用反射并生成 Json 字符串,但这需要我自己的对象遍历算法。我主要关心的是我想让我的应用程序无状态。
I have a C# MVC Project, and allow for users to select the way in which their data can be outputted (i.e. speed in kmh/mph, time in CST, ESt, etc., Distance in KMs, Miles, Meters, Hecters, etc.). Rather than running back to the db each time to retrieve the settings, on logon i stuff it into the encrypted cookies user data portion. I am also using the service-repository pattern where my services are implemented in the BLL and models defined in the BOL. I am also returning the data as JsonResults (using NewtonSoft Library). What is the best strategy (where and how) to convert the data to the user specified type without passing this unit preference information into the Business Logic Layers? I was initially thinking about using customAttributes and writing my own CustomJsonResult actionResult, that would use reflection and generate the Json string, but this would require my own object traversal algorithm. My primary concern is that i want to make my app stateless.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这种转换可以在映射层中执行,该映射层将业务对象转换为传递给视图的视图模型。因此,控制器将查询业务层以检索模型,然后查询存储在 cookie 中的用户首选项,并将这两个实体传递到映射层,映射层将根据用户区域设置执行必要的转换和格式化,并返回视图模型准备好传输到强类型视图,该视图将简单地输出信息。
另一种可能性是在视图中使用自定义 HTML 帮助程序,该帮助程序将采用模型值,并根据存储在 cookie 输出中的用户首选项执行转换并输出格式化字符串。
I think that this conversion could be performed in the mapping layer which converts your business objects into view models that are passed to the view. So the controller would query the business layer to retrieve a model then will query the user preference which was stored into a cookie and would pass those two entities to the mapping layer which will perform the necessary conversions and formatting based on the user locale and return a view model ready to be transmitted to the strongly typed view which would simply output the information.
Another possibility is to use custom HTML helpers inside your view which would take the model value and based on the user preferences stored into the cookie output perform the conversion and output the formatted string.