0

A am trying to create a series of wrapper classes to encapsulate some classes derived directly from the database.

I want to simply hand in an instance of the database object into the constructor of the wrapper class and not have to manually set each property.

I cannot change the db class B, its not my code or department and Id have to go up 2 levels of management just to talk to someone who could consider changing it.

I know it is possible to simply brute force the solution. by taking an instance of the base class in the constructor and setting each property of the true base class with that instance. like so:

class A :B{
   public A(B instance){
      this.Prop1 = B.Prop1
      //...
      this.Prop87 = B.Prop87
   }

   public string doRandomWrapperFunction(){
       return Prop36 + Prop49 + DateTime.Now(); 
   }
} 

But some of these objects have over 100 props, and I have about 100 objects I would like to create a wrapper for. So that means I need to write over 1000 lines of just this.Prop59 = B.Prop59. AND they might change in the future.

what I want to have is something like:

class A : B{
    public A(B instance){
        base = B;//This is the line that I want to compile but can't
    }

    public string doRandomWrapperFunction(){
       return Prop36 + Prop49 + DateTime.Now(); 
    }
}

I really don't want to have to do something like

class A{
    public B BaseInstance;
    public A(B instance){
        this.baseInstance = B;
    }

    public string doRandomWrapperFunction(){
       return BaseInstance.Prop36 + BaseInstance.Prop49 + DateTime.Now(); 
    }
}

because I use class B everywhere

string id = "1234"
B dbObject = getBFromDatabase(id); //I cant change this method.
existingCodeFunctionCall(dbObject.Prop1) //<==this times 1000

and I would have to change all of them to:

A dbObject = new A(getBFromDatabase(id));
existingCodeFunctionCall(dbObject.BaseInstance.Prop1) //<==this times 1000

I simply want to change its declaration from B to A like this:

   A dbObject = new A(getBFromDatabase(id));
   existingCodeFunctionCall(dbObject.Prop1)//<== this doesnt need to change
//because it automatically has all the properties of the base class.

I understand that I might be way off, and that the solution might have nothing to do with inheritance or the base class, this is just where I am stuck.

Thanks for any help.

Doopdon
  • 125
  • 2
  • 10
  • 2
    You can just do new A(params), and in constructor of A, call base(params), like public A(int params) : base(params) {//rest of constructor logic} – Victor Mukherjee Aug 01 '19 at 16:45
  • The issue is I get all these objects fully formed from a repository. I never create them, they are handed to me already complete. Ill change my description to make that more clear. – Doopdon Aug 01 '19 at 16:55
  • 1
    You may be able to do this using reflection: https://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class – David Zemens Aug 01 '19 at 17:21
  • Your question is quite broad. You can't do exactly what you're asking to do, and the number of alternatives is large. See marked duplicate for several suggestions. That said, I don't see what the problem is. You still need to actually write the properties, which is trouble enough. So what if you also have to write one more statement for each to initialize it? But if you don't want to do that, even better just retain the reference to the base class and delegate all of the properties to the base class. Then you do only have to deal with the property implementation itself, not initialization. – Peter Duniho Aug 01 '19 at 20:47

1 Answers1

1

I thought a had a solution based on casting but while it compiled it did not run.

So I went the reflection rout and created a function that takes a from object a to object and a type.

This feels like a hack and would like something better.

public void copyAll(dynamic objFrom, dynamic objTo, Type t) {
    System.Reflection.PropertyInfo[] props = t.GetProperties();
    foreach (System.Reflection.PropertyInfo prop in props)
    {
         if (!prop.CanWrite) continue;
         dynamic value = prop.GetValue(objFrom);
         prop.SetValue(objTo, value);
    }
}

but now I can make my constructor like this:

class A{
   public A(B baseObj){
       Type t = typeof(B);
       CopyAll(user,this,t);
   }
}

That was annoying, but we are in business.

Doopdon
  • 125
  • 2
  • 10