C # polymorphism problem

I have a BL class with the name: A, a DTO class with the name: DTO. Now suppose I want to add some more properties to my DTO. Therefore, I get a new DTO class from an 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 of it as a user interface) I call MyBLMethod (present in the BL repository) and pass it a class object, and from the BL repository I call my DAL. In DAL, I wrote 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 I can get the recently extended property (name in the example) in my DAL.

+3
source share
5 answers

B A, DTO. , . B, .

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

    }

, , DTO "Name".

static void Main(string[] args)
        {
            B b = new B();
            b.D.ID = 1;
            ((MyDTO)b.D).Name = "4";
            MyBLMethod(b);
        }
+4

ACTUALLY , . , .

(). , - .

+3

, , , MyDTO , DTO. MyBLMethod DTO DAL, MyDTO.

D . , D A, DTO, A, a B, MyDTO.

D D , , .

B, MyDTO, DTO:

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

, B DTO, MyDTO, , .

+3

, "", - . , DAL.

, , , , , , "", null dal-. , . , , "" , , , , .

+2

B A a DTO A. protected. , B.D cast A.D.

public class A
{
    private DTO _d;

    // New constructor.
    protected A(DTO d)
    {
        _d = d;
    }

    // Old constructor calls new constructor.
    public A() : this(new DTO())
    {
    }

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

public class B : A
{
    // Old B constructor calls new A constructor.
    public B() : base(new MyDTO())
    {
    }

    new public MyDTO D
    {
        // Getting D casts A.D instead of using an object exclusive to B.
        get { return (MyDTO)base.D; }
        set { base.D = value; }
    }
}
+1
source

Source: https://habr.com/ru/post/1722169/


All Articles