C# Interface Implementation Question

  • Thread starter Thread starter Soon
  • Start date Start date
S

Soon

Hi,

I am new to C# and have a question regarding interface implementation in C#. In the COM days, we can have multiple COM objects implementing the same interface and instantiating one of them depending on the business requirements. For instance:

public function GetSavingAccountBalance(str_AcctNo as string, int_BankID as Integer) as double

Dim objSavingAcct as IBankAccount

if ( ID_BANK_1 = int_BankID ) then

set objSavingAcct = CreateObject ("Bank1.SavingAccount.1")

elseif ( ID_BANK_2 = int_BankID ) then

set objSavingAcct = CreateObject ("Bank2.SavingAccount.1")

end if

GetSavingAccountBalance = objSavingAcct.Balance( str_AcctNo )

set objSavingAcct = nothing

End Sub

In this case, both Bank1.SavingAccount.1 and Bank2.SavingAccount.1 are ProgIDs for 2 COM objects, and they both implement IBankAccount interface. If there are changes in the business logic, all I need to do is to instantiate another object with a different ProgID (which can be retrieved from a configuration file). This way I do not have to recompile the client again and makes the architecture very flexible.

How do I do this in C#? Any help will be appreciated. Thanks in advance.

Cheers,
Soon
 
Hi,

You could define an interface, then have several classes to implement it, doing this you will get the same functionality.
But you can go a step further, though. You can declare in the app configuration file the class to be instantiate and the dll location and at runtime you can use it instead of using a multiple if construction ( in this way you do not have to hard code the different possibilities) , take a look at the code below:

Assembly ass = Assembly.LoadFile( Config.DataTransformatorLocation);

dataStrategic = (BaseTransformation) ass.CreateInstance( Config.DataTransformatorFQN);



Here I load an assembly and then instantiate a class declare in it that I know implement a defined interface. Config is a class that provide static properties to access configuration settings, DataTransformatorLocation return the path of the dll and DataTransformatorFQN return the Fully qualified name of the type.



Hope this help,


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Hi,

I am new to C# and have a question regarding interface implementation in C#. In the COM days, we can have multiple COM objects implementing the same interface and instantiating one of them depending on the business requirements. For instance:

public function GetSavingAccountBalance(str_AcctNo as string, int_BankID as Integer) as double

Dim objSavingAcct as IBankAccount

if ( ID_BANK_1 = int_BankID ) then

set objSavingAcct = CreateObject ("Bank1.SavingAccount.1")

elseif ( ID_BANK_2 = int_BankID ) then

set objSavingAcct = CreateObject ("Bank2.SavingAccount.1")

end if

GetSavingAccountBalance = objSavingAcct.Balance( str_AcctNo )

set objSavingAcct = nothing

End Sub

In this case, both Bank1.SavingAccount.1 and Bank2.SavingAccount.1 are ProgIDs for 2 COM objects, and they both implement IBankAccount interface. If there are changes in the business logic, all I need to do is to instantiate another object with a different ProgID (which can be retrieved from a configuration file). This way I do not have to recompile the client again and makes the architecture very flexible.

How do I do this in C#? Any help will be appreciated. Thanks in advance.

Cheers,
Soon
 
Back
Top