[英]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.