1

I have an application that needs to check whether to use a 32 bit or a 64 bit driver dll. return value is the driver of 32/64 bit dll. Think I need to do a generic class. Someone got an idea to do that ?

public Driver_64/Driver_32 Connect()
{
   if (Environment.Is64BitOperatingSystem)
   {
      Driver64bit.driver drv = new Driver64bit.driver(ip,parm)
      return drv;
   }
   else
   {
         Driver32bit.driver drv = new Driver32bit.driver(ip,parm)
         return drv;
   }
}
Jedhi
  • 73
  • 1
  • 9

1 Answers1

5

Basically, hide this implementation detail behind an interface:

public interface IDriver {
    void DoSomething(...);
    //...
}

If you control the types, then have Driver64bit.driver and Driver32bit.driver implement the interface; otherwise, add classes to encapsulate this:

internal sealed class Driver32 : IDriver {
    private readonly Driver32bit.driver driver;
    public Driver32(Driver32bit.driver driver) {
        this.driver = driver;
    }
    void IDriver.DoSomething(...) {
        driver.DoSomething(...);
    }
}

(and a Driver64, obviously)

now just return IDriver:

public IDriver Connect() {
   // ...
}

either returning drv or new Driver32(drv) / new Driver64(drv), depending on whether you could make the types implement the interface versus adding a wrapper type.

You can also use base-classes, but interfaces are generally more flexible.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Wouldn't it make more sense for the IDriver to offer an architecture neutral method GetDriverInstance() that's supposed to give back the appropriate version and Get64bitDriverInstance()/Get32bitDriverInstance for explicit access? – ASA Sep 16 '14 at 08:40
  • @Traubenfuchs why? what useful purpose would that address? – Marc Gravell Sep 16 '14 at 08:41
  • He could get an architecture appropriate driver-object with a single line. – ASA Sep 16 '14 at 08:52
  • @Traubenfuchs again, what useful purpose would that address? the entire point of encapsulation and abstraction is that the caller **doesn't need to worry about that** - they just work with the abstraction, and let the implementation worry about those details internally. – Marc Gravell Sep 16 '14 at 08:54
  • No it is first when I ask the dll which can either be a driver_32 bit or a driver_64 that I know which driver to use. Is it enough to use Environment.Is64BitOperatingSystem to find out the bit version of dll ? Is it possible to make a generic class or do I have to make two classes for 32 bit and 64 bit where I practice the driver instructions ? – Jedhi Sep 16 '14 at 09:27