Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a partial class property

I have a partial class and I want to do something like the following:

[MetadataType(typeof(UserMetaData))]
public partial class Person
{
    public override string PrivateData
    {
        get
        {
            return customDecrypt(base.PrivateData);
        }
        set
        {
            base.PrivateData = customEncrypt(value);
        }
    }
}

the above does not work however.

Is there a way to override the base entity framework classes its properties to allow for custom getter/setter?

like image 592
Mike Valstar Avatar asked Mar 04 '13 17:03

Mike Valstar


People also ask

Can we override a partial class in C#?

Ordinarily, this means that you cannot override them; you cannot override in one partial class the methods that are defined in another partial definition of the same class. Nevertheless, you can override these methods by setting the Generates Double Derived flag for the domain class.

Can a partial class inherit?

Inheritance cannot be applied to partial classes.

Do partial classes have to be in same namespace?

All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project. Each part of a partial class has the same accessibility.

Can a partial class implement an interface?

Partial class, interface and structure was introduced in C# 2.0. Now it is possible to split the definition of an class, interface and structure over more than one source files. Moreover the other parts of the class, struct, or interface should be defined in the same namespace or assembly.


2 Answers

Partial classes have nothing to do with inheritance, and override is entirely about inheritance.

The partial keyword on a class just means that it can be declared multiple times in the same assembly. It's exactly the same as if you copied every part of every partial class into the same file and removed the partial keyword. Since you can't define the same property/function/etc twice in the same class, you can't define it twice in two separate parts of the same class, even with the partial keyword.

override, on the other hand, is used in derived classes to indicate that they're replacing the functionality of the base class they inherit from. If it doesn't explicitly inherit, it inherits from object, which lets you override ToString() (among others).

Your best options to do what you want are either to use a custom T4 template to generate the encrypt/decrypt logic, or to set the encrypted properties to protected or private in the designer and manually add public versions which do the decryption.

like image 116
Bobson Avatar answered Oct 14 '22 23:10

Bobson


While you can't override the Entity Framework base classes, there is a work around. In the .edmx model (for DB first, direct in entities if code first) on the propery you want to 'override' change the Getter / Setter to private and rename the property. Then create the partial class with the property using the public name, which will no longer conflict.

In the public partial class property you will be able to access the private renamed property if needed.

like image 26
Jon Roberts Avatar answered Oct 14 '22 21:10

Jon Roberts