Implementation issue in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am facing some implementation issues in C#, I have a UI class which is implementing an Interface and i also have factory class in that interface which will instantiate someother classes

basically I want to hide all the implementation details that is the reason i am having a interface... UI is in one assembly interface is in another assembly(factory class is implementing that interface is in the same assembly

When i say ITest.GetApplication() in UI, i know that it will create a compile time error....(An object reference is required for the nonstatic field, method, or property 'ClassLibrary2.ITest.GetApplication(string)


SO I need to instaintiate the like this Itest iTest = new factoryclass(), i dont want to do this bcoz if i do like this i dont require the interface

My idea is Implement the interface in UI and make that factory class has internal so that the interface hides the implementation...

can you please tell me how can i acheive this...
 
Hey Mark,
First, I would suggest building a very simplistic testcase to verify and
validate your approach. Eliminate every non-essential detail. Once you get
the basic version working, then you can add complexity gradually.

in regards to your particular case,
ITest.Getapp() is apparently an interface method. You need an instance of a
class that implements ITest.Getapp() in order to call ITest.GetApp().

Eg, suppose I have class C1 and it exposes method M1. I cannot call C1.M1()
unless I have an instance of C1. (assuming M1 is not static). But you
knew that.
Suppose I have interface I1 which exposes Method M2. Again I need an
instance of a class that implements I1, in order to call M2.

I don't know exactly what UI is, when you say UI, I think of user interface,
which I think is irrelevant. let's just say you have a class, C1.

C1 apparently implements ITest, according to your description.

Suppose that ITest consists of a single method, GetApplication(). This
means that C1 provides an implementation of GetApplication() (matching
signatures). like so:

public class C1: ITest {
public Something GetApplication() { ... } // implementation here
}

When a user of C1 wants to call GetApp, he does this:

C1 c = new C1();
Something s= c.GetApplication();

Do you have something like this? Can you show us?

--
Dino Chiesa
Microsoft Developer Division
d i n o c h @ ElideThis . m i c r o s o f t . c o m



Mark said:
Hi,

I am facing some implementation issues in C#, I have a UI class which is
implementing an Interface and i also have factory class in that interface
which will instantiate someother classes.
basically I want to hide all the implementation details that is the reason
i am having a interface... UI is in one assembly interface is in another
assembly(factory class is implementing that interface is in the same
assembly)
When i say ITest.GetApplication() in UI, i know that it will create a
compile time error....(An object reference is required for the nonstatic
field, method, or property 'ClassLibrary2.ITest.GetApplication(string)'
)

SO I need to instaintiate the like this Itest iTest = new factoryclass(),
i dont want to do this bcoz if i do like this i dont require the interface.
My idea is Implement the interface in UI and make that factory class has
internal so that the interface hides the implementation....
 
Hello Dino Chiesa [Microsoft],
C1 apparently implements ITest, according to your description.

Suppose that ITest consists of a single method, GetApplication().
This means that C1 provides an implementation of GetApplication()
(matching signatures). like so:

public class C1: ITest {
public Something GetApplication() { ... } // implementation here
}
When a user of C1 wants to call GetApp, he does this:

C1 c = new C1();
Something s= c.GetApplication();

To get the polymorphic behaviour he desires though, the code would actually be:

ITest c = new C1();
Something s = c.GetApplication();

This would partially hide the implementation. Taking this a step farther by introducing a factory class would completely hide it.

public class TestFactory
{
public static ITest CreateTest()
{
return new C1();
}
}

At this point, the code would be modified to this:

ITest c = TestFactory.CreateTest();
Something s = c.GetApplication();

From here, he can add parameters to the CreateTest method to return different implementations of ITest without changing the client implementation.
 
Back
Top