C# Interface Implementation Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Soon

    #1

    C# Interface Implementation Question

    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 GetSavingAccoun tBalance(str_Ac ctNo as string, int_BankID as Integer) as double

    Dim objSavingAcct as IBankAccount

    if ( ID_BANK_1 = int_BankID ) then

    set objSavingAcct = CreateObject ("Bank1.SavingA ccount.1")

    elseif ( ID_BANK_2 = int_BankID ) then

    set objSavingAcct = CreateObject ("Bank2.SavingA ccount.1")

    end if

    GetSavingAccoun tBalance = objSavingAcct.B alance( str_AcctNo )

    set objSavingAcct = nothing

    End Sub

    In this case, both Bank1.SavingAcc ount.1 and Bank2.SavingAcc ount.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
  • Ignacio Machin

    #2
    Re: C# Interface Implementation Question

    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.LoadFi le( Config.DataTran sformatorLocati on);

    dataStrategic = (BaseTransforma tion) ass.CreateInsta nce( Config.DataTran sformatorFQN);



    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, DataTransformat orLocation return the path of the dll and DataTransformat orFQN return the Fully qualified name of the type.



    Hope this help,


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


    "Soon" <ysliang00@yaho o.com> wrote in message news:%23GX$yiBU DHA.2036@TK2MSF TNGP10.phx.gbl. ..
    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 GetSavingAccoun tBalance(str_Ac ctNo as string, int_BankID as Integer) as double

    Dim objSavingAcct as IBankAccount

    if ( ID_BANK_1 = int_BankID ) then

    set objSavingAcct = CreateObject ("Bank1.SavingA ccount.1")

    elseif ( ID_BANK_2 = int_BankID ) then

    set objSavingAcct = CreateObject ("Bank2.SavingA ccount.1")

    end if

    GetSavingAccoun tBalance = objSavingAcct.B alance( str_AcctNo )

    set objSavingAcct = nothing

    End Sub

    In this case, both Bank1.SavingAcc ount.1 and Bank2.SavingAcc ount.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

    Comment

    Working...