Use of delegates for Data Portal??

Use of delegates for Data Portal??

Old forum URL: forums.lhotka.net/forums/t/3908.aspx


vdhant posted on Friday, November 16, 2007

Hi guys

I am relatively new to csla so please excuse me if this is a stupid question, but I was just wondering why delegates could not be used for determining which data layer method to execute from a given business layer method. Now Rocky/you might cover this reasoning in his/your book but i have not had a chance to read through it all yet, so if he/you does/do could you please point me in the direction as to why.

To qualify, when i say use delegates what I mean is as follows. It seems like if I am roughly following things correctly, at the moment when I want to fetch some data, the business method calls the fetch client data portal stub which then calls the fetch server data portal stub which then calls the data fetch method for the class in question. This requires that the all of the methods involved need to have a fetch method so that they can call the next required method. This makes it difficult if I want to add alternative list methods or any other methods to a class that have not been explicitly catered for by the framework.

So what I was wondering is, would it be possible to and if so why was this not done, to change the data portals to accept delegate references so that any methods could be called through the data portal and not just the ones that have been catered for? Now of course, if the system is setup to execute the data layer methods on a separate server, the client data portal would need to be able to pass the local delegate reference across to the server data portal and it would need to know how to hook it up so that the everything works (which might be easy said than done), but this seems like something that could be worthwhile. If this path was taken you might still need a couple different data portal methods to cater for different types of calls that can be made (like maybe one that excepts a criteria object, one that returns objects, I don’t know what else some thought might need to go into that side of it but one would think that it would be achievable).

Now there might be a really good reason for not doing this or the idea might be really stupid as i said but the thought did occur to me as I was looking through the data portal code and how things connect and I thought it was worth asking.

Thanks
Anthony

vdhant replied on Saturday, November 17, 2007

I know this is fairly simplistic and probably not very efficient but in theory it would look something like this:
class Program
{

static void Main(string[] args)
{
Client client = Client.FetchClient(12);
client.UpdateClient();
Console.ReadLine();
}

}

public class Client
{

public int ClientId = -1;
public string ClientName = "";

//Business Method
public static Client FetchClient(int clientId)
{
Client client = new Client();
ClientDataPortal.ExecuteRetrieveMethod(new ClientDataPortal.RetrieveMethod(client.FetchClient_Data), new Criteria(clientId));
return client;
}

//Business Method
public void UpdateClient()
{
ClientDataPortal.ExecutePersistMethod(new ClientDataPortal.PersistMethod(this.UpdateClient_Data));
}

//Data Method
public void FetchClient_Data(object criteria)
{
Criteria newCriteria = (Criteria)criteria;
//Call to database
this.ClientId = 12;
this.ClientName = "Hello World";
}

//Data Method
public void UpdateClient_Data()
{
//Call to database
this.ClientName = "Hello World -- Update";
}

public class Criteria
{
public Criteria(int clientId)
{
this.ClientId = clientId;
}
public int ClientId = -1;
}
}

public class ClientDataPortal
{

public delegate void PersistMethod();
public delegate void RetrieveMethod(object criteria);
private static bool _IsUsingRemoteDataAccess = true;

public
static void ExecuteRetrieveMethod(RetrieveMethod retrieveMethod, object currentCriteria)
{
if (ClientDataPortal._IsUsingRemoteDataAccess)
ServerDataPortal.ExecuteRetrieveMethod(retrieveMethod.Target, retrieveMethod.Method.Name, currentCriteria);
else
ServerDataPortal.ExecuteRetrieveMethod(retrieveMethod, currentCriteria);
}

public static void ExecutePersistMethod(PersistMethod persistMethod)
{
if (ClientDataPortal._IsUsingRemoteDataAccess)
ServerDataPortal.ExecutePersistMethod(persistMethod.Target, persistMethod.Method.Name);
else
ServerDataPortal.ExecutePersistMethod(persistMethod);
}
}

public class ServerDataPortal
{

public static void ExecuteRetrieveMethod(object currentObject, string retrieveMethod, object currentCriteria)
{
currentObject.GetType().InvokeMember(retrieveMethod, System.Reflection.
BindingFlags.InvokeMethod, null, currentObject, new object[1]{ currentCriteria });
}

public static void ExecuteRetrieveMethod(ClientDataPortal.RetrieveMethod retrieveMethod, object currentCriteria)
{
retrieveMethod(currentCriteria);
}

public static void ExecutePersistMethod(object currentObject, string persistMethod)
{
currentObject.GetType().InvokeMember(persistMethod, System.Reflection.
BindingFlags.InvokeMethod, null, currentObject, null);
}

public static void ExecutePersistMethod(ClientDataPortal.PersistMethod persistMethod)
{
persistMethod();
}
}

vdhant replied on Sunday, November 18, 2007

Any feedback on this one??
Thanks
Anthony

RockfordLhotka replied on Monday, November 19, 2007

vdhant:

So what I was wondering is, would it be possible to and if so why was this not done, to change the data portals to accept delegate references so that any methods could be called through the data portal and not just the ones that have been catered for?

What you are describing is a broader concept called an object portal. The data portal is a very specific implementation of this concept, designed to support data access.

CSLA is intententionally a prescriptive architecture, and CSLA .NET is a framework designed to assist and guide developers in following that architecture.

The object portal concept is very broad and very powerful, but is not very prescriptive. While I love the pattern and think it is very cool, I don't think the broader implementation has a place in CSLA .NET, because it would allow too much flexibility in how objects are created and that would defeat the benefits of a prescriptive architecture and of a highly consistent pattern for object creation and usage.

vdhant replied on Monday, November 19, 2007

Thanks a lot for your reply.

I have been thinking about this concept for a while but never knew what name to put to it. Although there doesn’t seem to be much on this topic, does this mean that there isn’t many implementations?? It makes sense now that you explain it that CSLA would not necessarily go down this track. I think that a CSLA or like framework that supported this would be very interesting but i agree that broader implementation would mean that there are too many ways that you could "shoot yourself in the foot" and not release it.

I appreciate the response and on another topic I just wanted to thank you for your book it really made me think about things in a different way and is one of the only books that i have found that talks about how to create a framework for the ground up and the challenges that one faces.

Keep up the great work…

Anthony

JoeFallon1 replied on Monday, November 19, 2007

In addition to Rocky's response - many people have asked in the past if they could add other "DataPortal" methods. I believe this is one of the reasons Rocky came up with the Command Object and DataPortal_Execute. This new method allows you to run arbitrary code against the DB while making sure the BO that contains the code moves throught the DataPortal when necessary. So now you have Create, Fetch, Insert, Update and Execute. Not sure why you would need something outside of these 5.

Joe

 

Copyright (c) Marimer LLC