Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# polymorphism issue

I have a BL class named:A , DTO class named:DTO . Now assume I want to add some more properties in my DTO. So I derive a new DTO class from my existing DTO and add properties to it.Below is the code:

namespace TestConsole
{
    class test
    {
        static void Main(string[] args)
        {

            B b = new B();
            b.D.ID = 1;
            b.D.Name = "4";
            MyBLMethod(b);

        }
        static void MyBLMethod(A b)
        {
            MyDALMethod(b.D);
        }

        static void MyDALMethod(DTO dto)
        {
           int i = dto.ID;
           string name = ((MyDTO)dto).Name;//I could not do this 
            //because i will get object cast error as i can't cast from 
            //parent to child
        }

    }
    public class DTO
    {
        public int ID = 99;
        public DTO()
        {
        }

        public DTO(DTO source)
        {
            ID = source.ID;
        }
    }

    public class MyDTO : DTO
    {
        public string Name = "";
        public MyDTO() { }
        public MyDTO(MyDTO source)
            : base(source)
        {
            Name = source.Name;

        }
    }
    public class A
    {
        private DTO _d;
        public A()
        {
            D = new DTO();

        }

        public DTO D
        {
            get { return _d; }
            set { _d = value; }
        }
    }

    public class B : A
    {
        private MyDTO _md;
        public B()
        {
            _md = new MyDTO();

        }

        public MyDTO D
        {
            get { return _md; }
            set { _md = value; }
        }
    }

}

From Main (you can think it as UI) i am calling MyBLMethod (present in BL repository) and passing class object to it , and from BL repository i am calling my DAL. In DAL i have written this:

static void MyDALMethod(DTO dto)
{
    int i = dto.ID;
    string name = ((MyDTO)dto).Name;//I could not do this 
       //because i will get object cast error as i can't cast from 
       //parent to child
}

Could you suggest me how could i get the newly extended property (name in the example) in my DAL.

like image 985
Raghav Avatar asked Aug 25 '26 10:08

Raghav


1 Answers

When B inherits A, it already owns a DTO attribute. So the problem is really that you are hidding this inheritance. You don't need a new property inside B class, just set it in your class constructor.

public class B : A
    {
        public B()
        {
            this.D = new MyDTO();
        }

    }

But, in your main class you will need a explicit cast in your property, just like below, since DTO does not have a "Name" property.

static void Main(string[] args)
        {
            B b = new B();
            b.D.ID = 1;
            ((MyDTO)b.D).Name = "4";
            MyBLMethod(b);
        }
like image 138
nandokakimoto Avatar answered Aug 27 '26 01:08

nandokakimoto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!