473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Polymorphism using DLL Declares?

Hello,

I have a set of DLLs that implement different configurations of the
same API. Consider them as first.dll, second.dll, and third.dll. They
all share a common API, calling pattern, and have the same logic to
handle the calls. Calls in the VB.net code does not call the DLLs
directly, but rather a custom dll wrapper class, so that the logic can
be made for the VB code in the abstraction class.

Initially, my idea was to create a superclass that contained
MustOverride prototypes for the DLL calls, and the VB public apis
implemented in this class. Then have 3 concrete classes FirstDll,
SecondDll, and ThirdDll, and have the Declares override the DLL api
calls, and just inherit the VB public API. This did not work however
as
you can not specify Declares as overrides.

Second, I attempted to implement the Strategy pattern, by creating an
interface called DllAPI which had the DLL prototype Declares as
methods, then create 3 concrete classes implementing the interface
with
their respective declares. Then create GlobalAPI class that contained
all the public VB APIs, and contains a reference to a DllAPI object,
which polymorphically is the proper DLL type, and delegate DLL calls
to
that object. This did not work either however as you cannot use
Declares to implement an interface.

What is the right way to do this, as to not have to maintain duplicate
VB public API information in multiple classes that are 95% the same?
Normally I would not seek asking such questions, as they are design
issues, but everything I try seems to be constrained by VB limitations
(with or without good reason), so I am hoping someone else has solved
this issue.

Thank you,
Vincent Castellano

Feb 2 '07 #1
4 1532
"Vince Castellano" <su*****@gmail.comwrote in
news:11**********************@m58g2000cwm.googlegr oups.com:
What is the right way to do this, as to not have to maintain duplicate
VB public API information in multiple classes that are 95% the same?
Normally I would not seek asking such questions, as they are design
issues, but everything I try seems to be constrained by VB limitations
(with or without good reason), so I am hoping someone else has solved
this issue.
Are you trying to build a Plug-In system?
Feb 3 '07 #2
On Feb 2, 4:33 pm, "Vince Castellano" <sury...@gmail.comwrote:
Hello,

I have a set of DLLs that implement different configurations of the
same API. Consider them as first.dll, second.dll, and third.dll. They
all share a common API, calling pattern, and have the same logic to
handle the calls. Calls in the VB.net code does not call the DLLs
directly, but rather a custom dll wrapper class, so that the logic can
be made for the VB code in the abstraction class.

Initially, my idea was to create a superclass that contained
MustOverride prototypes for the DLL calls, and the VB public apis
implemented in this class. Then have 3 concrete classes FirstDll,
SecondDll, and ThirdDll, and have the Declares override the DLL api
calls, and just inherit the VB public API. This did not work however
as
you can not specify Declares as overrides.

Second, I attempted to implement the Strategy pattern, by creating an
interface called DllAPI which had the DLL prototype Declares as
methods, then create 3 concrete classes implementing the interface
with
their respective declares. Then create GlobalAPI class that contained
all the public VB APIs, and contains a reference to a DllAPI object,
which polymorphically is the proper DLL type, and delegate DLL calls
to
that object. This did not work either however as you cannot use
Declares to implement an interface.

What is the right way to do this, as to not have to maintain duplicate
VB public API information in multiple classes that are 95% the same?
Normally I would not seek asking such questions, as they are design
issues, but everything I try seems to be constrained by VB limitations
(with or without good reason), so I am hoping someone else has solved
this issue.

Thank you,
Vincent Castellano
Vincent...

If I understand what your trying to do, then there are a couple of
ways to go about this. If your only talking the 3 dll's, then I would
probably declare a public MustInherit (abstract) class in a class
library that exposed the api to your client application. In side, I
would create three concrete classes that contained the declares for
each dll, but had an internal constructor. These classes could be
created then by either a factory method, or you might use something
like an abstract factory pattern.

If all the calling sequences are the same, then something like the
template method pattern would be perfect for you. Simply put the
calling sequences in the public api of the MustOverride class, and
then put some protected MustOverride (abstract) methods to actually
call the api calls. Each concrete class would then call the correct
api. It would look some thing like:

Public MustInherit Class PublicApi
Public Sub DoCoolStuff1 (ByVal aParameter As String)
' do stuff
' do more stuff
Dim somevalue As Integer = DoAnApiCall(aParameter)
' do more stuff
Return
End Sub

Protected MustOverride Function DoAnAPICall (ByVal aParameter As
String) As Integer

End Class

Public Class ConcreteImplementation1
Inherits PublicAPI

Private Declare blah lib "dll1.dll"....

Friend Sub New ()
End Sub

Protected Override Function DoAnAPICall (ByVal aParameter As
String) As Integer
return blah (aParameter)
End function
End Class

Public Class ConcreteImplementation2
Inherits PublicAPI

Private Declare blah lib "dll2.dll"....

Friend Sub New ()
End Sub

Protected Override Function DoAnAPICall (ByVal aParameter As
String) As Integer
return blah (aParameter)
End function
End Class

The client would do this:
Dim api As PublicApi = PublicApi.GetImplementation ()
api.DoCoolStuff1("Hi")

The other method I can think of is to dyanmically load the dll
(loadlibrary api call) and then to generate the calling class on the
fly using System.Reflection.Emit. Though, unless you are planning to
have a bunch of implementations or there could be more in the future,
I probably wouldn't go this way. I've seen examples around, so I'm
sure you could google and find some code on this topic... Anyway,
good luck.

--
Tom Shelton

Feb 3 '07 #3
On Feb 2, 9:00 pm, "Tom Shelton" <tom_shel...@comcast.netwrote:
On Feb 2, 4:33 pm, "Vince Castellano" <sury...@gmail.comwrote:

[...cut...]

Vincent...

If I understand what your trying to do, then there are a couple of
ways to go about this. If your only talking the 3 dll's, then I would
probably declare a public MustInherit (abstract) class in a class
library that exposed the api to your client application. In side, I
would create three concrete classes that contained the declares for
each dll, but had an internal constructor. These classes could be
created then by either a factory method, or you might use something
like an abstract factory pattern.

If all the calling sequences are the same, then something like the
template method pattern would be perfect for you. Simply put the
calling sequences in the public api of the MustOverride class, and
then put some protected MustOverride (abstract) methods to actually
call the api calls. Each concrete class would then call the correct
api. It would look some thing like:

Public MustInherit Class PublicApi
Public Sub DoCoolStuff1 (ByVal aParameter As String)
' do stuff
' do more stuff
Dim somevalue As Integer = DoAnApiCall(aParameter)
' do more stuff
Return
End Sub

Protected MustOverride Function DoAnAPICall (ByVal aParameter As
String) As Integer

End Class

Public Class ConcreteImplementation1
Inherits PublicAPI

Private Declare blah lib "dll1.dll"....

Friend Sub New ()
End Sub

Protected Override Function DoAnAPICall (ByVal aParameter As
String) As Integer
return blah (aParameter)
End function
End Class

Public Class ConcreteImplementation2
Inherits PublicAPI

Private Declare blah lib "dll2.dll"....

Friend Sub New ()
End Sub

Protected Override Function DoAnAPICall (ByVal aParameter As
String) As Integer
return blah (aParameter)
End function
End Class

The client would do this:
Dim api As PublicApi = PublicApi.GetImplementation ()
api.DoCoolStuff1("Hi")

The other method I can think of is to dyanmically load the dll
(loadlibrary api call) and then to generate the calling class on the
fly using System.Reflection.Emit. Though, unless you are planning to
have a bunch of implementations or there could be more in the future,
I probably wouldn't go this way. I've seen examples around, so I'm
sure you could google and find some code on this topic... Anyway,
good luck.

--
Tom Shelton
Hi Tom,

Thank you for the thorough response, it worked great for my situation.
It causes some duplication between different DLL classes, but no logic
code, which is what I wanted to avoid. Between what you said, and my
trusty Gang of Four book, was able to implement the template method
pattern as it fit for this situation well.

Hope this thread sheds light for anyone else in the same situation.

-Vince

Feb 5 '07 #4
On Feb 5, 4:43 pm, "Vince Castellano" <sury...@gmail.comwrote:
On Feb 2, 9:00 pm, "Tom Shelton" <tom_shel...@comcast.netwrote:


On Feb 2, 4:33 pm, "Vince Castellano" <sury...@gmail.comwrote:
[...cut...]
Vincent...
If I understand what your trying to do, then there are a couple of
ways to go about this. If your only talking the 3 dll's, then I would
probably declare a public MustInherit (abstract) class in a class
library that exposed the api to your client application. In side, I
would create three concrete classes that contained the declares for
each dll, but had an internal constructor. These classes could be
created then by either a factory method, or you might use something
like an abstract factory pattern.
If all the calling sequences are the same, then something like the
template method pattern would be perfect for you. Simply put the
calling sequences in the public api of the MustOverride class, and
then put some protected MustOverride (abstract) methods to actually
call the api calls. Each concrete class would then call the correct
api. It would look some thing like:
Public MustInherit Class PublicApi
Public Sub DoCoolStuff1 (ByVal aParameter As String)
' do stuff
' do more stuff
Dim somevalue As Integer = DoAnApiCall(aParameter)
' do more stuff
Return
End Sub
Protected MustOverride Function DoAnAPICall (ByVal aParameter As
String) As Integer
End Class
Public Class ConcreteImplementation1
Inherits PublicAPI
Private Declare blah lib "dll1.dll"....
Friend Sub New ()
End Sub
Protected Override Function DoAnAPICall (ByVal aParameter As
String) As Integer
return blah (aParameter)
End function
End Class
Public Class ConcreteImplementation2
Inherits PublicAPI
Private Declare blah lib "dll2.dll"....
Friend Sub New ()
End Sub
Protected Override Function DoAnAPICall (ByVal aParameter As
String) As Integer
return blah (aParameter)
End function
End Class
The client would do this:
Dim api As PublicApi = PublicApi.GetImplementation ()
api.DoCoolStuff1("Hi")
The other method I can think of is to dyanmically load the dll
(loadlibrary api call) and then to generate the calling class on the
fly using System.Reflection.Emit. Though, unless you are planning to
have a bunch of implementations or there could be more in the future,
I probably wouldn't go this way. I've seen examples around, so I'm
sure you could google and find some code on this topic... Anyway,
good luck.
--
Tom Shelton

Hi Tom,

Thank you for the thorough response, it worked great for my situation.
It causes some duplication between different DLL classes, but no logic
code, which is what I wanted to avoid. Between what you said, and my
trusty Gang of Four book, was able to implement the template method
pattern as it fit for this situation well.

Hope this thread sheds light for anyone else in the same situation.

-Vince- Hide quoted text -

- Show quoted text -
Glad it worked for you.

--
Tom Shelton

Feb 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
0
by: Vince Castellano | last post by:
Hello, I have a set of DLLs that implement different configurations of the same API. Consider them as first.dll, second.dll, and third.dll. They all share a common API, calling pattern, and have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.