none
Partial classes of Entity Object RRS feed

  • Question

  • Hi

    Generally when we design model layer we are adding Entity Object and then if we want to make any improvements in entity class then we create partial class of that entity. Below have tried to explain that improvement by example:

    Lets say we have TinyInt type Status field in Claim table to store Claim status 0- Open, 1- Processed and 2 – Closed.

    Now to make programming easy we create common Enum class where we define this enum:

    Now to make programming easy we create common Enum class where we define this enum:

    public class Enums
        {
            public enum Status { Open = 0, Processed, Closed };
        }
    

    And we create partial class of Claim to make this enum work:

    partial class tblClaim
        {
            private Enums.Status _iStatus;
            public Enums.Status iStatus
            {
                get { return (Enums.Status)Status; }
                set
                {
                    _iStatus = value;
                    Status = (byte)value;
                }
            }
        }
    

    So now whenever I want to refer status field I use iStatus.

    Would like to ask is it ok if we create Partial classes this way? Or is there any better method exists?

    Please share your expert suggestions.

    Thanks in advance.

    Regards

    Somnath



    Tuesday, January 5, 2016 9:18 AM

Answers

  • A partial class is used to extend a class's functionality without affecting the class's original code, like an auto generated class. So you can do anything you want in the extended class file,  and it will not affect the auto generated code EF is using.
    Tuesday, January 5, 2016 3:20 PM
  • >>Generally when we design model layer we are adding Entity Object and then if we want to make any improvements in entity class then we create partial class of that entity

    The main reason behind the use of partial classes in this case is that you don't risk losing your custom code if and when the auto-generated entity classes are re-generated. If the entity classes are not auto-generated, there is no real point of creating partial class definitions just for the sake of it.

    For large classes it might be convenient to split the definition of the class over two or more source files and there is nothing wrong with doing this. Remember that the partial classes must reside in the namespace of the same assembly though: https://msdn.microsoft.com/en-us/library/wa80x488.aspx

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Tuesday, January 5, 2016 7:38 PM

All replies

  • A partial class is used to extend a class's functionality without affecting the class's original code, like an auto generated class. So you can do anything you want in the extended class file,  and it will not affect the auto generated code EF is using.
    Tuesday, January 5, 2016 3:20 PM
  • >>Generally when we design model layer we are adding Entity Object and then if we want to make any improvements in entity class then we create partial class of that entity

    The main reason behind the use of partial classes in this case is that you don't risk losing your custom code if and when the auto-generated entity classes are re-generated. If the entity classes are not auto-generated, there is no real point of creating partial class definitions just for the sake of it.

    For large classes it might be convenient to split the definition of the class over two or more source files and there is nothing wrong with doing this. Remember that the partial classes must reside in the namespace of the same assembly though: https://msdn.microsoft.com/en-us/library/wa80x488.aspx

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Tuesday, January 5, 2016 7:38 PM