what 's the use of creating a metadata class and a partial class for the model class c#

ado.net-entity-data-model c# entity-framework-6 model-view-controller

Question

I just joined a new company and my manager just joined too, and he wants to change the way we program. basically do what he does. I'm wondering what's the difference, pros, cons, limitation and problems if there'll be any..here's the sample

namespace Models //this is the model part of from edmx
{
using System;
using System.Collections.Generic;


public partial class MyModelClass
{
    public int ID { get; set; }
    public Nullable<System.DateTime> PostDate { get; set; }
    public string MyContent { get; set; }
}

}

this is the metadata:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace Models
    {
         public class MyModelMetaData
         {
           //he wants all data annotation here
           public int ID { get; set; }
           public Nullable<System.DateTime> PostDate { get; set; }
           public string MyContent { get; set; }
         }
   }

this is the partial:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;

    namespace Models
   {
        [MetadataType(typeof(MyModelMetaData))]
        public partial class MyModelClass or MyModelClassPartial
        {

           //He wants the programming algorithm to go here
       }
  }

Please enlightened me. and he wants to create different metadata and partial classes per model class..way too many files involved.

thank you..i need an answer as to why..if you think his method is good..I will do this..but if you think this will cause problem in the future and more coding will be involve..i need to know

1
2
12/16/2016 2:03:02 PM

Accepted Answer

The first class you show, the entity classes, are generated from the database every time you save the EDMX (or when you execute the T4 Template).

This causes the file containing public partial class MyClass under the EDMX to be regenerated. So you cannot alter it, because the next time someone refreshes a table or adds one, your changes are gone.

That's why entity classes are generated as a partial: so you can create another partial to the same class to do your modifications in.

However, if you want to annotate your entity's properties with metadata, you cannot redefine the same property in the other partial class: the same name can only be used by one member of a type. So you can't do this:

// Entity class
public partial class FooEntity
{
    public string Name { get; set;} 
}

// User-created partial class
public partial class FooEntity
{
    [Required]
    public string Name { get; set;} 
}

Because that code expresses you want two properties named Name in the FooEntity class, which is not valid.

So you'll have to come up with another way to add metadata to the type. Enter the [MetadataType] attribute. This works by creating a new class with the same properties as the class to be annotated. Here, using reflection, the metadata is resolved based on member name.

So when you create a metadata class for the above annotation:

public class FooEntityMetadata
{
    [Required]
    public string Name { get; set;} 
}

You can apply it to the user-created partial:

// User-created partial class
[MetadataType(typeof(FooEntityMetadata))]
public partial class FooEntity
{
}

And also, in the latter partial, you can add members that add functionality to the entity model. New ([NotMapped]) properties and new methods for example.

7
12/16/2016 12:59:59 PM

Popular Answer

I think the one use could be to not pollute the main class.

For example if you have a lot of attribute for validation (using dataannotation) and you don't want to have them in the main class you could use the MetadataTypeAttribute for that.

Another use could be if your class is auto-generated and you need to add some decoration (more attributes) to your properties without changing the autogenerated code.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow