dotnet0824
asked on
Calling a .NET Interface at the client
Calling an interface at the Client
Simple procedure to to be called at the client used in the Interface.
ASP.NET Application
1) Interface class (I_Interface) (Class File)
2) Customer.Class1.cs (Class File)
3) Default.aspx.cs
1) Interface Class
namespace Thames.Interfaces
{
public interface I_Interface
{
int CustomerAdd(int ab,int bc);
}
2)
Customer.Class1.cs
namespace Customer
{
public class Class1:I_Interface
{
public int CustomerAdd(int ab, int bc)
{
return (ab + bc);
}
}
}
3) Client (ASP.NET Web Page :Default.aspx.cs)
Things done at the client : I added a Reference to the Interface class designed earlier.
( I DONT WANT THE CUSTOMER CLASS EXPOSED TO THE CLIENT / INSTEAD INTERFACE HAS TO BE USED TO INVOKE THE METHOD "CustomerAdd at the client")
I declare :
I_Interface _Interface ;
_Interface.CustomerAdd(2,3 ) 'It doesnot accept how do i use the method of the interface here.
Simple procedure to to be called at the client used in the Interface.
ASP.NET Application
1) Interface class (I_Interface) (Class File)
2) Customer.Class1.cs (Class File)
3) Default.aspx.cs
1) Interface Class
namespace Thames.Interfaces
{
public interface I_Interface
{
int CustomerAdd(int ab,int bc);
}
2)
Customer.Class1.cs
namespace Customer
{
public class Class1:I_Interface
{
public int CustomerAdd(int ab, int bc)
{
return (ab + bc);
}
}
}
3) Client (ASP.NET Web Page :Default.aspx.cs)
Things done at the client : I added a Reference to the Interface class designed earlier.
( I DONT WANT THE CUSTOMER CLASS EXPOSED TO THE CLIENT / INSTEAD INTERFACE HAS TO BE USED TO INVOKE THE METHOD "CustomerAdd at the client")
I declare :
I_Interface _Interface ;
_Interface.CustomerAdd(2,3
better yet use a factory pattern ...
Class1 MyClass = YourFactory.GetObject(); //allows injection etc
I_Interface _Interface = MyClass;
Class1 MyClass = YourFactory.GetObject(); //allows injection etc
I_Interface _Interface = MyClass;
ASKER
Darren,
Whats the use of using the Class at the client. I was in the impression that Interfaces would only be exposed to the client which would restrict the User at the client to view the Class data like properties / methods etc. but if the class is also referenced along with the interface, I dont find much use of implementing interfaces.
Whats the use of using the Class at the client. I was in the impression that Interfaces would only be exposed to the client which would restrict the User at the client to view the Class data like properties / methods etc. but if the class is also referenced along with the interface, I dont find much use of implementing interfaces.
ASKER
The bottom line is Class1 Has to be added to the client along with the Interface right!!!!!!!!!!!!
No you can create the class dynamically but you have to have an instance of the class.
But you don't have to add it to the solution.
Interfaces allow you to use different object / Classes through your interface.
Hence gregoryyoung said to use a class factory.
He may be able to shed somemore light on this but in essence you use them to create more than one type of class which can then be used by the implemented interface.
Darren
But you don't have to add it to the solution.
Interfaces allow you to use different object / Classes through your interface.
Hence gregoryyoung said to use a class factory.
He may be able to shed somemore light on this but in essence you use them to create more than one type of class which can then be used by the implemented interface.
Darren
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
In a real world example you could say have an interface which performs some database methods.
ie.
Interface
ExecuteSQL()
ExecuteSP()
Close()
Then you could have for example three components or one component with three classes which implement these methods.
Eg:
SQL Server implementation
OLEDB implementation for Oracle
MySQL implementation
Then you could create the class required dynamically depending on what database you wished to connect to and once you have implemented all the methods in the interface then you can call each class no matter which one through the interface without having to change your code...
Hope this helps.
Darren
PS: You should probably use the Microsoft Database Connection classes for this this operation but this is just an example of something you could do using interfaces.
ie.
Interface
ExecuteSQL()
ExecuteSP()
Close()
Then you could have for example three components or one component with three classes which implement these methods.
Eg:
SQL Server implementation
OLEDB implementation for Oracle
MySQL implementation
Then you could create the class required dynamically depending on what database you wished to connect to and once you have implemented all the methods in the interface then you can call each class no matter which one through the interface without having to change your code...
Hope this helps.
Darren
PS: You should probably use the Microsoft Database Connection classes for this this operation but this is just an example of something you could do using interfaces.
My last post has nothing to do with class factories.
Just an idea on how to explain my original post.
Definitely look at class factories.
Darren
Just an idea on how to explain my original post.
Definitely look at class factories.
Darren
ASKER
No you can create the class dynamically but you have to have an instance of the class.
But you don't have to add it to the solution.
Darren : Without adding Customer.Class1 Dll at the client, I am not able to create an instance of it.
for eg I can do this I_Interface I_Inter;
Class1 Obj = new Class1 '''' Am not able to do this as the client doesnot know as it has no reference to it
But you don't have to add it to the solution.
Darren : Without adding Customer.Class1 Dll at the client, I am not able to create an instance of it.
for eg I can do this I_Interface I_Inter;
Class1 Obj = new Class1 '''' Am not able to do this as the client doesnot know as it has no reference to it
ASKER
Lets take my Example which I wrote and show me how to use the Class Factory methodology to call my Interface (I_Interface) without a REFERENCE OF CLASS1 at client . That would be greatly appreciated
ASKER
I have increased the Points for this question
OK,
Firstly you need the DLL in which your classes reside.
What I ment by dynamically is:
If you have a DLL with the required classes then you can dynamically create and instance of the required class using the LoadAssembly and Activator.CreateInstance method. This means that your DLL does not have to be part of the solution but instead is loaded at runtime.
The fact is that you call your methods through your interface which keeps your type safety instead of using methods like Invoke which looses type safety.
I do work sometimes so I'm not able to put up a working version right now but I will do it asap if gregoryyoung doesn't have one that he could put up sooner. Could be tomorrow though.
Darren
Firstly you need the DLL in which your classes reside.
What I ment by dynamically is:
If you have a DLL with the required classes then you can dynamically create and instance of the required class using the LoadAssembly and Activator.CreateInstance method. This means that your DLL does not have to be part of the solution but instead is loaded at runtime.
The fact is that you call your methods through your interface which keeps your type safety instead of using methods like Invoke which looses type safety.
I do work sometimes so I'm not able to put up a working version right now but I will do it asap if gregoryyoung doesn't have one that he could put up sooner. Could be tomorrow though.
Darren
dotnet0824 you can use the same pattern as above ... the client code shown
IFoo foo = FooFactory.GetFoo();
foo.Bar();
has no dependency on the actual Foo object (only the factory does). you can further seperate this by putting those 3 classes into a dll together. Make Foo internal and the factory + interface public. You are completely assured then that your consumer cannot have a dependency on the Foo object.
Cheers,
Greg
IFoo foo = FooFactory.GetFoo();
foo.Bar();
has no dependency on the actual Foo object (only the factory does). you can further seperate this by putting those 3 classes into a dll together. Make Foo internal and the factory + interface public. You are completely assured then that your consumer cannot have a dependency on the Foo object.
Cheers,
Greg
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
The main question asked was answered and then a another question was asked.
So I don't know. Split the points...
So I don't know. Split the points...
ASKER
Sorry for the late reply. I was infact out of my country . Thanks again
Class1 MyClass = new Class1();
I_Interface _Interface = MyClass;
Then you can call the methods:
_Interface.CustomerAdd(2,3
Your interface only describes a contract between it and the component which implements it. You still need to be able to access the actual class to call the methods.
Hope this helps,
Darren