简体   繁体   English

在模型和视图模型之间共享枚举

[英]Share enums between model and view model

Is it a good idea to share enums between model and viewmodel? 在模型和视图模型之间共享枚举是一个好主意吗?

I have pretty much common enums to share like: 我有很多常见的枚举,例如:

public enum Month { January, February, March, April, May, June, July, August, September, October, November, December }

or 要么

public enum WeekDay {
    [EnumDescription("working day")]
    WorkingDay,
    [EnumDescription("weekend")]
    WeekEnd,
    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}

I use these enums in viewmodel now, and I want to move them to my model's namespace. 我现在在viewmodel中使用这些枚举,并将它们移到模型的名称空间中。 But it will make my view dependent on the model: 但这会使我的观点取决于模型:

@using MyApp.Model

What's the best place to share enums in the project and why? 在项目中共享枚举的最佳位置是什么?为什么?

Absolutely it's a good idea. 绝对是个好主意。 Sometimes the easiest way is to build a separate assembly, maybe suffixed with .Common . 有时,最简单的方法是构建单独的程序集,可能后缀.Common Then, all assemblies can reference that without circular reference issues. 这样,所有程序集都可以引用该文件,而不会产生循环引用问题。

Such general-purpose enums are like any other cross-cutting concerns: They are not themselves part of any layer, but should be put into a separate namespace or project, since they are not specific to any architectural layer. 此类通用枚举与任何其他横切关注点一样:它们本身并不是任何层的一部分,但应放置在单独的命名空间或项目中,因为它们不特定于任何体系结构层。 This way, you also get rid of any unwanted dependencies. 这样,您还可以摆脱任何不必要的依赖关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在dotnet core中的视图组件之间共享模型 - Share a model between view components in dotnet core 自动映射将枚举的集合映射到视图模型集合中 - Automapper mapping collection of enums into view model collection 使用模型中的自定义枚举进行授权 - Authorize with custom enums in model 输入模型和视图模型有什么区别? - What is the difference between an input model and a view model? 在MVC和控制台应用程序之间共享模型和dbcontext - Share model and dbcontext between mvc and console application 如何在主视图 model 和用户控制视图 model 之间进行通信 - How to communicate between main view model and user control view model WPF MVVM:INPC和中介视图模型和模型之间的通信 - WPF MVVM: INPC and mediating communication between view model & model Itemselector项使用不同的数据模板,但应共享一个视图模型 - Itemselector items use different datatemplates but should share a view model 从模型创建UserControl-在UserControl之间共享ViewModel属性 - Create a UserControl from a Model - Share ViewModel Property between UserControl 如何在多个实体数据模型之间共享连接字符串 - How to share a connection string between multiple entity data model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM