简体   繁体   中英

Multiple Classes With Same Methods

I have many classes in a project that need to all have a base set of the same constructors and 1 public method. Below is an example of this partial class:

public partial class SHIPMENT_LINE
{
     private OracleConnection _rp = null;
     private EntityConnection _rpe = null;
     private static string _schema = "";

     public SHIPMENT_LINE() { }

     public SHIPMENT_LINE(BHLibrary.Configuration.ConnectionOption Environment)
     {
         SetConnection(Environment);
     }

     public void SetConnection(BHLibrary.Configuration.ConnectionOption Environment)
     {
         this._rp = Configuration.RPConnection(Environment);
         this._rpe = Configuration.RPEntityConnection(Environment, out _schema);
     }
}

I need to implement the same private variables, constructors, and the SetConnection method on each of my classes that I create. After this all exists in each class, then each class will do something different, so the classes are not all necessarily related, aside from the fact that they all have this same "Beginning."

How should I go about building each of these classes so that I do not have to implement this SetConnection method in each of the classes that I create?

Keep this in mind:

  • Due to other restrictions, I cannot inherit from another class in any of these classes. I can , however, use Interfaces if necessary.

If you can't subclass then an abstract class is not a viable solution and interfaces are only going to give you the contract that your common classes conform to without any implementation.

I would suggest implementing the common functionality in a common class and using this as a private member in your other classes (IE composition rather than inheritance). Your other classes could all implement an interface to ensure they all have the same methods and they could just forward their calls onto the private classes implementation of the method.

EG

private MYClassWithCommonFunctionality xyz = new MYClassWithCommonFunctionality();

And then...

Private void MyCommonInterfaceMethod(object param)
{
    // Do derived class specific stuff here...
    xyz.MyCommonInterfaceMethod(param);
}

And as an added bonus and a bit of forward thinking....have the common class also share the same interface and pass an implementation of this into your other classes constructor. That way in the future you can swap the implementation for another.

I would suggest going for composition rather than inheritance...

Make each of the class implement an interface, then have another class (not related to these) which also implements the interface and has a concrete implementation of it. All the classes you've mentioned above should have an instance of this additional class and just call through to it.

Example

public partial class SHIPMENT_LINE : ISetConnection
{
   private ConnectionSetter connector = new ConnectionSetter();

   public void SetConnection(BHLibrary.Configuration.ConnectionOption Environment)
   {
      this.connector.SetConnection(Environment);
   }
}

public class ConnectionSetter : ISetConnection
{
    public void SetConnection(BHLibrary.Configuration.ConnectionOption Environment)
   {
      // Implementation
   }
}

如果您无法创建实现通用功能的基类(出于任何原因?),则可能无法使用T4模板通过通用方法生成部分类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM