Suraj U Gadhvi
Suraj U Gadhvi

Reputation: 147

Multiple metadata classes for entity

Suppose I have 2 entities one is UserMaster another is ProjectMaster, now there are few common properties in both the entities and I need to apply same data annotations and attributes on both set of common properties which I do using metadata classes, now my question is there any way that I have a common metadata class for all this common properties across entities,so that I don't need apply data annotation and attributes for common properties again and again and if yes then how to achieve this.

Upvotes: 1

Views: 1328

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

You can create a base metadata class and the other metadata class derive from the base and add some other properties.

Example:

In the below example I supposed you can't derive Class2 from Class1 so I have inheritance only between metadata classes, otherwise you can simply have inheritance between your model classes.

[MetadataType(typeof(Class1Metadata))]
public class Class1
{
    public string Id { get; set; }
    public string Name { get; set; }
}

[MetadataType(typeof(Class2Metadata))]
public class Class2
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class Class1Metadata
{
    [Display(Name="Id1")]
    public string Id { get; set; }
    [Display(Name = "Name1")]
    public string Name { get; set; }
}
public class Class2Metadata:Class1Metadata
{
    [Display(Name = "Description2")]
    public string Description { get; set; }
}

Upvotes: 2

Related Questions