shdr
shdr

Reputation: 966

Entity Framework on ASP .NET MVC – Extend Entity for Attributes, Methods and Properties

The issue: Entity object has it properties related to databases on its own, but the needs in the programming area is differ, sometimes we want to add it some more:

  1. Properties – that is for temporary logic.
  2. Methods – for clean code and for programming necessaries.
  3. Finally yet importantly – Attribute for authorization, display, filters etc.

However, obviously we do not want our program to be maintainability without needs to rewrite code just after we update the model.

Upvotes: 0

Views: 794

Answers (2)

shdr
shdr

Reputation: 966

For properties and methods, the Entity Framework platform generated all the object from model as partial classes and the .NET environment allow us to extend them as we wish:

Remember to check that our partial sit in same namespaces (Notice that when we create them in model directory or in them own directory Visual Studio create addition namespace).

public partial class ErrorLog
{
    public long pk { get; set; }
    public int lineNumber { get; set; }
    public Nullable<int> error { get; set; }
}

Our partial:

public partial class ErrorLog
{
    public  string getErrorDescription()
    {
        return d[(int)error];
    }

    private static Dictionary<int, string> d = new Dictionary<int, string>()
    {
        {1,"desc1" },
        {2,"desc2" },
        {3,"desc3" },
        {4,"desc4" }
    };

}

For attributes:

We can add new interface

public interface IErrorLogsMetaData
{

    [Display(Name = "Id")]
    long pk { get; set; }

    [Display(Name = "The line Number")]
    int lineNumber { get; set; }

    [Display(Name = "The Error")]
    Nullable<int> error { get; set; }


}

Implement them on our Entity (even extended) object. For that we need to reflect and book it in global.asax by using:

TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(ErrorLog), typeof(IErrorLogsMetaData)), typeof(ErrorLog));

TypeDescriptor – familiar for us from reflection, its get information about type. AddProviderTransparent – is the method called from my partially trusted code and get metadata from associated class.

The first parameter is the provider and it TypeDescriptionProvider from the type we want to decorate and the attributed interface, the second parameter is the target type for decription.

Another Option Make your partial view to implement the IErrorLogsMetaData and then you don't need to associate at Global.asax

Upvotes: 2

SᴇM
SᴇM

Reputation: 7213

As you can see, the database first entity model classes are partial, so you can create your own partial class, for example if you have:

public partial class SomeClass
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

You can do something like this:

Add some class to your project, name it SomeClassPartial:

//SomeClassPartial.cs
namespace YourNamespace
{
    [MetadataType(typeof(SomeClassMetadata))]
    public partial class SomeClass
    {
        //add your new properties/some_logic here

        public string NewPropX { get; set; }
        public string NewPropY { get; set; }
    }

    public partial class SomeClassMetadata
    {
        //metadata for your existing model properties

        [Display(Name = "Property 1")]
        public string Prop1 { get; set; }

        [Display(Name = "Property 2")]
        public string Prop2 { get; set; }
    }
}

In your SomeClassMetadata class you can add data annotation attributes to your existing properties with MetadataType attribute, which will specify the metadata class to associate with a data model class, and with that you can tell you partial SomeClass class to get that attributes from SomeClassMetadata class. To add new custom properties, you can use SomeClass partial class.

MSDN Link: MetadataTypeAttribute Class

Upvotes: 0

Related Questions