I have a project called Model. All of my entities are in that project generated by EF Code first.
我有一个名为Model的项目。我的所有实体都在EF Code首先生成的项目中。
Public Partial Class Person
{
...
}
I don't want to touch my generated classes so I can create a partial class and add MetadataType
attribute to it.
我不想触摸我生成的类,所以我可以创建一个分部类并向其添加MetadataType属性。
[MetadataType(typeof(Person_Metadata))]
public partial class Person
{
}
And here is my buddy class.
这是我的好友课。
[Bind(Exclude="PersonID")]
public class Person_Metadata
{
[Display(Name:="First name")]
public string FirstName { get; set; }
[Display(Name:="Last name")]
public string LastName { get; set; }
}
The problem is, I want to move my buddy class to another assembly.
问题是,我想将我的好友类移动到另一个程序集。
Model project has no reference to it so [MetadataType(typeof(Person_Metadata))]
gives an error because it has no reference to Person_Metadata
class.
模型项目没有引用它,因此[MetadataType(typeof(Person_Metadata))]给出错误,因为它没有引用Person_Metadata类。
I can use FluentValidation for validation part (and it works great), but what about other Metadata like: Display
Attribute ?
我可以使用FluentValidation作为验证部分(并且效果很好),但是其他元数据如:显示属性呢?
I have also found this question: Adding DataAnnotation to class when using FluentValidation about managing MetaData with FluentValidation engine, but that looks like a long way to go and I prefer using data annotation attributes
我还发现了这个问题:在使用FluentValidation关于使用FluentValidation引擎管理MetaData时,将DataAnnotation添加到类中,但这看起来还有很长的路要走,我更喜欢使用数据注释属性
2 个解决方案
#1
2
The problem is, I want to move my buddy class to another assembly.
问题是,我想将我的好友类移动到另一个程序集。
That's impossible. Partial classes work only within the boundaries of the same assembly.
这不可能。部分类仅在同一程序集的边界内工作。
I can use FluentValidation for validation part (and it works great), but what about other Metadata like: Display Attribute ?
我可以使用FluentValidation作为验证部分(并且效果很好),但是其他元数据如:显示属性呢?
Nothing, they just could stay on your view model. Not on your domain model. Your view models are classes that you specifically design to meet the requirements of your views. It's on those view models that you would use the [DisplayFormat]
and similar attributes.
没什么,他们只是留在你的视图模型上。不在你的域名模型上。您的视图模型是您专门设计的类,以满足您的视图要求。在那些视图模型上,您将使用[DisplayFormat]和类似的属性。
#2
1
I know its too late to reply, but someone may get help from it.
我知道回复已太晚了,但有人可能会从中得到帮助。
I had similar situation where existing Model classes/Entities were created in an independent assembly and that was also in DotNet 3.5.
我有类似的情况,现有的模型类/实体是在一个独立的程序集中创建的,也是在DotNet 3.5中。
I Had to make reuse of the above assembly but this time in ASP.Net MVC 5. For that i figured out that the classes in Model assembly, if marked as public can be inherited in ASP.Net MVC project and there we can add buddy classes for data annotations.
我必须重复使用上面的程序集,但这次是在ASP.Net MVC 5中。为此我发现模型程序集中的类,如果标记为public,可以在ASP.Net MVC项目中继承,我们可以添加好友数据注释的类。
It worked perfectly....
它工作得很好......
#1
2
The problem is, I want to move my buddy class to another assembly.
问题是,我想将我的好友类移动到另一个程序集。
That's impossible. Partial classes work only within the boundaries of the same assembly.
这不可能。部分类仅在同一程序集的边界内工作。
I can use FluentValidation for validation part (and it works great), but what about other Metadata like: Display Attribute ?
我可以使用FluentValidation作为验证部分(并且效果很好),但是其他元数据如:显示属性呢?
Nothing, they just could stay on your view model. Not on your domain model. Your view models are classes that you specifically design to meet the requirements of your views. It's on those view models that you would use the [DisplayFormat]
and similar attributes.
没什么,他们只是留在你的视图模型上。不在你的域名模型上。您的视图模型是您专门设计的类,以满足您的视图要求。在那些视图模型上,您将使用[DisplayFormat]和类似的属性。
#2
1
I know its too late to reply, but someone may get help from it.
我知道回复已太晚了,但有人可能会从中得到帮助。
I had similar situation where existing Model classes/Entities were created in an independent assembly and that was also in DotNet 3.5.
我有类似的情况,现有的模型类/实体是在一个独立的程序集中创建的,也是在DotNet 3.5中。
I Had to make reuse of the above assembly but this time in ASP.Net MVC 5. For that i figured out that the classes in Model assembly, if marked as public can be inherited in ASP.Net MVC project and there we can add buddy classes for data annotations.
我必须重复使用上面的程序集,但这次是在ASP.Net MVC 5中。为此我发现模型程序集中的类,如果标记为public,可以在ASP.Net MVC项目中继承,我们可以添加好友数据注释的类。
It worked perfectly....
它工作得很好......