|
-
Oct 22nd, 2007, 11:44 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] Interface question
Hello,
I have never created my own interface before. I am pretty confident that I understand it pretty good, but I have two issues that I am hoping that someone can help me out with.
I have created a transactions interface that will be implemented by classes such as contact, or any other that I choose to add it to. It will provide the methods used to add a contact or another record to a database through a webservice (a third party).
My first issue is what data type of parameter should I use. In my add method I could be adding a contact, or some other type. These are my own custom types (classes). Should I use System.Type as the parameter data type? Also, in my search method I want to pass a list of my own custom types. I use my own List(of Customer) for this one, but how can I make it so that it will accept a list of any types?
The second issue is about my search method as well. I want to pass back a list of results that are custom types as well. In the one I am working on right now, it is a customer type, but it could be another type. How can I make this interface abstract enough to allow my method to return a list of any types?
I hope I have made my questions clear. If not, just ask and I will clarify anything. Thank you for your help.
BTW, I am using customer and contact interchangeably here.
Ben
Using Visual Basic 2005/2008
-
Oct 23rd, 2007, 02:22 AM
#2
Hyperactive Member
Re: [2005] Interface question
Hi, the awnser is quite simple.
Compile your custom types and interfaces in a seperate DLL, and add them to both other projects as references...
That way, all code knows about all objects and you can reference the objects by their interface name.
-
Oct 23rd, 2007, 02:02 PM
#3
Thread Starter
Hyperactive Member
Re: [2005] Interface question
Thank you for your reply.
I think I wasn't clear. I don't have a problem referencing the objects, what I am trying to do is make my interface abstract enough so that I don't have to write an interface for each type. Here is some code, maybe it will help me clarify what I am trying to do.
Code:
Friend Interface IPosTransactions
''' <summary>
''' Adds new object to persistent data.
''' </summary>
''' <param name="objectRecord">Object record to be added to the persistent data.</param>
Function Add(ByRef recordObject As System.Type) As Boolean
''' <summary>
''' Deletes an object from the persistent data.
''' </summary>
''' <param name="recordObject">Object to be deleted from the persistent data.</param>
Function Delete(ByRef recordObject As Type) As Boolean
''' <summary>
''' Finds a specific object in persistent data.
''' </summary>
''' <param name="recordObject">Object to be found.</param>
Function Find(ByRef recordObject As Type) As Object
''' <summary>
''' Searches for objects in persistant data.
''' </summary>
''' <param name="objectList">List of objects for searching.</param>
Function Search(ByRef objectList As Type) As IList
''' <summary>
''' Updates object in persistant data.
''' </summary>
''' <param name="recordObject">Object to be updated in persistant data.</param>
Function Update(ByRef recordObject As Type) As Boolean
End Interface
OK, I made bold the area in my code that I am referring to. For the parameter, I want to accept a class that is my own type, it could be a customer type, or some other type that I create. I don't want it to be able to accept strings, or other types like that, and I don't want to use object and then try to cast. I am trying to avoid boxing and unboxing.
I hope that helps clarify what I am asking.
Ben
Using Visual Basic 2005/2008
-
Oct 23rd, 2007, 05:55 PM
#4
Hyperactive Member
Re: [2005] Interface question
So if I understand correctly, you have created objects which encapsulate a data record. eg: Contact. among others, and you want your interface to be able to handle all of these objects and return the results.
eg:
function Add(ByRef recordObject as type) As Boolean
Where the recordObject may be either a Contact, Customer, or some other object which encapsulates a record.
I think this is a matter of weather or not you can encapsulate any record in one object, and I think the awnser is yes, so you dont have to create an interface.
Here is one idea i have...
Create a structure which can hold any data along with a feild name and data type, (you may want to consider putting validation variables in there, like maximum number, string length, date range ect.)
Then create a class which can construct an SQL insert, update and delete statements, your interface should reference this recordObject as its own type RecordObject.
Code:
Structure FeildValue
Sub new(Byval strFeild as string, Byval objValue as object,Byval ValueType as type)
Feild = strFeild
Value = objValue
FeildType = ValueType
End Sub
Dim Feild as string
Dim Value as object
Dim FeildType
End Structure
Now create the class which can construct SQL Statements
Code:
Class RecordObject
private _tableName as string
private _data as List(Of FeildValue)
private function SQLinsert as string
dim strSQL as string
strSQL = "INSERT INTO " & _tableName & " VALUES ("
for each fv as FeildValue in _data
select Case fv.FeildType
case Integer
strSQL &= Ctype(fv.Value, Integer) & ", "
case String
strSQL &= "'" & Ctype(fv.Value, String) & "',"
'Do other possible types here, eg Decimal, date
end Select
next
'Remove the last , and replace with a )
return strSQL
end function
function SQLupdate
'im sure you get the gist
end function
End Class
IF that class was complete im sure it could handle any datatype, and table row, this class could allow you to use your interface with any table row what so ever and could be used like so.
Code:
Public Function Add(Byval ro as recordObject) As Boolean Implements IposTransactions.Add
Dim sqlStatement as string = ro.SQLInsert
'Execute statement.
end function
Public Function Update(Byval ro as recordObject) As Boolean Implements IposTransactions.Update
Dim sqlStatement as string = ro.SQLUpdate
'Execute statement.
end function
Hopefully this gives you some direction, obviously if you want this object to be able to be sent to the find and search mehtods it will need some work and a bit of thaught, but I think it showes that you can use any one object to handle more than one table, also you wouldnt want to throw away the objects you have made to encapsulate the records, just create a conversion function, which will convert a RecordObject into or from your other objects.
Hope this helps.
-
Oct 23rd, 2007, 10:12 PM
#5
Re: [2005] Interface question
Firstly, all your parameters should NOT be type Type, but rather type Object. That said, you'd probably be better off making this a generic interface:
vb.net Code:
Friend Interface IPosTransactions(Of T)
''' <summary>
''' Adds new object to persistent data.
''' </summary>
''' <param name="objectRecord">Object record to be added to the persistent data.</param>
Function Add(ByRef recordObject As T) As Boolean
''' <summary>
''' Deletes an object from the persistent data.
''' </summary>
''' <param name="recordObject">Object to be deleted from the persistent data.</param>
Function Delete(ByRef recordObject As T) As Boolean
''' <summary>
''' Finds a specific object in persistent data.
''' </summary>
''' <param name="recordObject">Object to be found.</param>
Function Find(ByRef recordObject As T) As T
''' <summary>
''' Searches for objects in persistant data.
''' </summary>
''' <param name="objectList">List of objects for searching.</param>
Function Search(ByRef objectList As T) As IList(Of T)
''' <summary>
''' Updates object in persistant data.
''' </summary>
''' <param name="recordObject">Object to be updated in persistant data.</param>
Function Update(ByRef recordObject As T) As Boolean
End Interface
Also, why are all your parameters being passed by reference?
-
Oct 24th, 2007, 01:51 PM
#6
Thread Starter
Hyperactive Member
Re: [2005] Interface question
Thank you both for your replies. jmcilhinney, you seem to understand better where I am trying to go with this. I do want to use generics. I am just at my limit of what I know, and I am not sure the right questions to ask to get what I need to know.
I use byref so that I am not creating copies. The interface will be in ASP .NET so I want to try to keep memory usage down.
Using generics for my interface is where I am kind of stuck. If I do what you did, won't that limit me to just one type. Like in the example, if I use
Friend Interface IPosTransactions(Of Customer)
Then that interface will only accept the Customer type, right? Maybe, I am just thinking crazy. I just didn't want to write a new interface to allow for each type (Customer, SalesOrder,...), but I want to reuse those four methods, and they will have different block code so I didn't think inheritance would be right. I think if I showed my class diagram it would help, but I am not able to get it today.
BTW, objectRecord and objectList are classes that have properties that contain information that will be used by the methods. It seemed as though maybe there was the thought that they are a dataset or contain a dataset, but they are not.
The high level view of what I am trying to do is to take data from Quickbooks POS, load the data in my custom types, pass it to this class and it will integrate that data into salesforce.com. Turns out designing this is taking more thought than just writing the code, but I want to design it right in case I need to add more functionality later. Plus, I want this to be using OOD properly.
That is probably too much information, but I thought it might help you understand where I am and where I am trying to get.
Thank you for your help.
Ben
Using Visual Basic 2005/2008
-
Oct 24th, 2007, 06:30 PM
#7
Re: [2005] Interface question
I use byref so that I am not creating copies. The interface will be in ASP .NET so I want to try to keep memory usage down.
That is misguided. Passing by value makes a copy of your variable. If you create an instance of a class and that object occupies 200 MB of memory, the variable referring to it still only occupies 32 bits. It is the 32 bits you are copying, NOT the 200 MB.
The point of generics is exactly what you are trying to do. You specify a generic type argument in various places and then you specify the type of that argument at run time. Everywhere that that generic type is used is then forced to that type. Here's a simple example of a generic interface:
vb.net Code:
Public Interface ISomeInterface(Of T)
Function GetSomething() As T
End Sub
So, you have a generic interface with a GetSomething method that returns an object of type T, whatever that may be. If you implement that interface like so:
vb.net Code:
Public Class Class1
Implements ISomeInterface(Of FirstType)
Then you're saying that everywhere in that class T is replaced by FirstType, so the GetSomething method of Class1 will return a FirstType object. If you do this:
vb.net Code:
Public Class Class2
Implements ISomeInterface(Of SecondType)
Then the GetSomething method of Class2 will return a SecondType object, because in this case T is replaced with SecondType.
In your case, you specify your Add and Delete methods as taking a T object, so you can only add and delete objects of type T, whatever T may be for any particular type that implements that interface. If T is Customer then you can only add and delete Customer objects, etc.
-
Oct 24th, 2007, 11:05 PM
#8
Thread Starter
Hyperactive Member
Re: [2005] Interface question
That is misguided. Passing by value makes a copy of your variable. If you create an instance of a class and that object occupies 200 MB of memory, the variable referring to it still only occupies 32 bits. It is the 32 bits you are copying, NOT the 200 MB.
Your right! I misunderstood how that worked. According to this MSDN article...
http://msdn2.microsoft.com/en-us/lib...h4(VS.80).aspx
Byref just allows the underlying element type to be modified. Thank you for pointing that out.
Your information about generics makes sense. I created a sample to test it out.
Here is my interface.
Code:
Public Interface ITransaction(Of T)
Function Add(ByVal recordObject As T) As Boolean
Function Search(ByVal recordObject As T) As IList(Of T)
End Interface
Here is my class.
Code:
Public Class Customer
Implements ITransaction(Of String)
Public Function Add(ByVal recordObject As String) As Boolean Implements ITransaction(Of String).Add
End Function
Public Function Search(ByVal recordObject As String) As System.Collections.Generic.IList(Of String) Implements ITransaction(Of String).Search
End Function
End Class
I just used strings as a test. So I just have one last question. On my search method, I have a list of my types that inherits the List(Of T) class. Like this.
Code:
Public Class CustomerList
Inherits List(Of Customer)
End Class
If I return CustomerList from my search method it will work, right? Because List(Of T) derives from the IList(Of T), right? Or should I just have it return List(Of T)?
Thank you very much for your help. This is exactly what I was trying to do.
Ben
Using Visual Basic 2005/2008
-
Oct 24th, 2007, 11:10 PM
#9
Re: [2005] Interface question
List(Of T) doesn't derive from IList(Of T); it implements it. A List(Of String) implements the IList(Of String) interface, so it will work. As long as the type you specify for T when you create the List matches the type you specify for T when you implement the interface then you're good to go.
-
Oct 24th, 2007, 11:22 PM
#10
Thread Starter
Hyperactive Member
Re: [2005] Interface question
List(Of T) doesn't derive from IList(Of T); it implements it.
Ok, that makes sense. List(Of T) uses the IList(Of T) Interface is what you are saying, right? Because I indicates it is an interface.
A List(Of String) implements the IList(Of String) interface, so it will work. As long as the type you specify for T when you create the List matches the type you specify for T when you implement the interface then you're good to go.
So in my above example, if I change the class to this:
Code:
Public Class Customer
Implements ITransaction(Of Customer)
Public Function Add(ByVal recordObject As Customer) As Boolean Implements ITransaction(Of Customer).Add
End Function
Public Function Search(ByVal recordObject As Customer) As System.Collections.Generic.IList(Of Customer) Implements ITransaction(Of Customer).Search
End Function
End Class
Then that would be consistent with what you are saying. Cool, thank you for your help. I tried to rate you, but it said I have to give it to someone else before I can give it to you. Seems you are the last person I rated.
Ben
Using Visual Basic 2005/2008
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|