CSLA 3.0.2: Filtered NVL in C#

CSLA 3.0.2: Filtered NVL in C#

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


cjherasmus posted on Tuesday, March 18, 2008

Hi,

I'm trying to create a NVL that can retrieve a full set or a filtered set of records. My thinking is to have two factory methods as in:

public static MyList GetList()

{

   if (_list == null)

{

   _list = DataPortal.Fetch<MyList>(new Criteria(typeof(MyList)));

}

return _list;

}

public static MyList GetList(Guid id)

{

if (_list == null)

{

_list = DataPortal.Fetch<MyList>(new Criteria(id));

}

return _list;

}

Then a dataportal_fetch as in:

private void DataPortal_Fetch(Criteria criteria)

{

RaiseListChangedEvents = false;

using (SqlConnection cn = new SqlConnection(DatabaseConnection.MyDB))

{

cn.Open();

using (SqlCommand cm = cn.CreateCommand())

{

cm.CommandType = CommandType.StoredProcedure;

cm.CommandText = "spGetMyList";

cm.Parameters.AddWithValue("@prog_id", criteria.Id);

using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))

{

IsReadOnly = false;

while (dr.Read())

{

Add(new NameValuePair(dr.GetGuid("net_id"), dr.GetString("net_name")));

}

IsReadOnly = true;

}

}

}

RaiseListChangedEvents = true;

}

I'm retrieving data from a parent-child table relationship. Sometimes I would like to have all children, the other times I would like to have children for a specific parent.

Should I use a single stored procedure or one for each list?. Should I have a second data_portal fetch?

Thanks

Regards

triplea replied on Tuesday, March 18, 2008

What about 2 seperate classes? Not sure what you mean by having a second dp fetch but in any case it will probably be clearer and more readable to split in 2 classes since the overhead of writing the extra factory methods is not that great...

ajj3085 replied on Tuesday, March 18, 2008

Well, it seems like you may have a bug.  If you're previously asked for a filtered list, then the full list, you'll get the first filtered list because _list has a value now.  Same thing for different filtered lists as well; you'll always get the last one, because you're not caching the list by Guid.

That said, you can have two dataportal fetches.  I would change your first call to DataPortal.Fetch<MyList>();

Then override DataPortal_Fetch() that doesn't take parameters.  In this DP_F, fetch the whole list.  If your other DP_F( Criteria ) fetch the list filtered by criteria.

HTH
Andy

cjherasmus replied on Thursday, March 20, 2008

Thanks Andy,

I've added a second factory method with a parameter and added an override to the fetch method, everything works like a dream. The bug you've mentioned did appear. My application is somewhat unique so to combat that issue I call the public InvalidateCache method before loading the data.

Regards,

Copyright (c) Marimer LLC