Calling DataPortal_XYZ of unknown classCalling DataPortal_XYZ of unknown class
Old forum URL: forums.lhotka.net/forums/t/2671.aspx
burmajam posted on Tuesday, April 10, 2007
Hi all,
I have my base object Document<T>: BusinessBase<T> and some inherited specific document classes. I want in Document<T> to make factory method:
public Document<T> GetDocument(Guid id)
that will instanciate appropriate document and call its DataPortal_Fech method. I can determine type of document out of id, but problem is in calling DataPortal.Fetch<documentType>. Any ideas how to solve this?
Tnx,
Bayu replied on Tuesday, April 10, 2007
Hi,
In your base Document(Of T) class you could have this method:
Public Shared Function Fetch(ByVal id As Guid) As T
If Not CanGetObject() Then
Throw New System.Security.SecurityException("User not authorized to view this object")
End If
Return DataPortal.Fetch(Of T)(New GuidCriteria(id))
End Function
In the dataport_fetch methods of your concrete document classes (so subclasses of this base document) you recieve the GuidCriteria and populate the fields of the class.
You said that based on the ID you can determine what type of document should be returned, but since you are using Generics you don't have to perform such a check.
Bayu
burmajam replied on Tuesday, April 10, 2007
Hi Bayu,
I'm affraid it can't be done that way. That approach mean that when I'm calling procedure I know what type is it, ie. Document<Invoice>.Fetch would return Invoice (it is T in fetch method). Look at Document<T> just as a factory class. It could be some DocumentFactory class, thus not CSLA based. Then I would have:
public static Document<T> DocumentFactory.Fetch(Guid id)
{ ..... }
In this method I get type of object whos ID is provided id parameter. I can do it. But when I get that type, I should call:
return DataPorta.Fetch<ofThatType>(SomeCriteria criteria).
Problem is in red part. It just can't be called that way!
Bayu replied on Tuesday, April 10, 2007
Hi,
Is it perhaps a solution to define your Document class like this:
<Serializable()> _
Public MustInherit Class Document
Inherits BusinessBase(Of Document)
End Class
This way you end the chain of Generics at this class. This mean also that you would have to implement all dp_methods in this class, which seems to be exactly what you are after.
The subclasses of Document simply add properties, validation logic and authorization rules as necessary. In your Document base class you can provide overridable template methods that subclasses can use as a hook to get involved in loading, saving and updating the object.
Bayu
burmajam replied on Tuesday, April 10, 2007
Hi Bayu,
It makes sense. I will consider that approach.
Thanks
burmajam replied on Tuesday, April 17, 2007
I've done it via reflection. Problem was in reflecting all Fetch methods from DataPortal. If someone needs code, contact me.
Copyright (c) Marimer LLC