Suggestion for upcoming version

Developer
Jun 8, 2012 at 2:50 PM
Edited Jun 11, 2012 at 12:14 PM

Hi Josip and all,

I'd like to modify the BeforeFetch partial method in order to allow the developer to override the default fetch completely. This would be useful in cases where the developer needs to have finer control over what is fetched from the database.

Right now we have:

 

 

		private void DataPortal_Fetch(Key key)
		{
			using (var ctx = Csla.Data.ObjectContextManager<CSLAExtensionTest.Data.Vittoria_GVCEntities>.GetManager(CSLAExtensionTest.Data.Vittoria_GVCEntitiesDatabase.Name))
			{
				var data = ctx.ObjectContext.Customers.Single(e => e.ID == key.ID);
				DataPortal_Fetch(data);
			}		
		}

		private void DataPortal_Fetch(CSLAExtensionTest.Data.Customer data)
		{
			BeforeFetch(data);
			if (Fetching != null)
            	Fetching(this, new DataEventArgs(data));

			ReadData(data);
			LoadEntityKey(data);
			AfterFetch(data);				
			if (Fetched != null)
				Fetched(this, new DataEventArgs(data));
		}			

 

What I'd like to see is something more like this:

 

		private void DataPortal_Fetch(Key key)
		{
			using (var ctx = Csla.Data.ObjectContextManager<CSLAExtensionTest.Data.Vittoria_GVCEntities>.GetManager(CSLAExtensionTest.Data.Vittoria_GVCEntitiesDatabase.Name))
			{
                CSLAExtensionTest.Data.Customer data = null;
                BeforeFetch(ref data, ctx, key);
                if (data == null)
                {
                    if (Fetching != null)
                        Fetching(this, new DataEventArgs(data));

                    data = ctx.ObjectContext.Customers.Single(e => e.ID == key.ID);
                    DataPortal_Fetch(data);
                }
			}		
		}

		private void DataPortal_Fetch(CSLAExtensionTest.Data.Customer data)
		{
			ReadData(data);
			LoadEntityKey(data);
			AfterFetch(data);				
			if (Fetched != null)
				Fetched(this, new DataEventArgs(data));
		}			
		partial void BeforeFetch(ref CSLAExtensionTest.Data.Customer data, Csla.Data.ObjectContextManager<CSLAExtensionTest.Data.Vittoria_GVCEntities> ctx, Key key);
		partial void AfterFetch(CSLAExtensionTest.Data.Customer data);	
		internal event EventHandler<DataEventArgs> Fetching;
		internal event EventHandler<DataEventArgs> Fetched;

 

This way, the developer has full control over what will be fetched from the database in the partial class implementation. Sometimes we need to refine the data that is fetched, and the current way doesn't allow us to customize this in the partial class without risking seeing our changes being overwritten by the T4 generator.

Let me know what you think.

 

Luc Morin, T.P.

http://www.stlm.ca

Coordinator
Jun 12, 2012 at 1:39 PM

Hi Luc,

I agree with you about the need to perform some custom logic when fetching data, just not sure about the implementation.

Let me think about this a little bit.

Nice to hear from you again :)

Developer
Jun 12, 2012 at 1:48 PM

Josip,

I've been very busy with automation projects for the last year, so I didn't have a lot of time for CSLA related projects :-)

I have already implemented the changes in my local copy of the code. I could submit to the Dev branch for you to review if you want.

Let me know.