473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Of CollectionBases, Enumerations and DataBinding

I have an object model that exposes a class inheriting from collection base.

I would like to make this class available to an ASP.NET data grid, such as
in the following code:

grid.DataSource = MyObjectModel.MyCustomers.Filter("Smith%")

The problem is that MyCustomers is a lazy loading property of MyObjectModel
that, by default, returns ALL customers. So
"MyObjectModel.MyCustomers.Filter("Smith%")" first does the lazy load of ALL
customers THEN loads the customers filtered to the names beginning with
"Smith." The undesired initial lazy load is wasted.

How do I prevent this initial lazy load when a Filter method is called as in
above, but still retain the ability to make the very clean syntax of a call
such as "grid.DataSource = MyObjectModel.MyCustomers" when I want all
customers?
Nov 20 '05 #1
6 1272
Hi Good,

Just overload it,

Function() as dataset
Do your code without test
Function(byval search as string) as dataset
Do your code with test

I hope this helps?

Cor
Nov 20 '05 #2
if you are working w sql server make a stored proc that dous the filtering,
that way only the filtered data is sent. (you could do it as a sql string
from code but this gives troubles with ' ...

example of sp (i automized those things here would be to much work to do
this for 30 tables otherwise)
(and it's easyer to work w than it looks ;p )

/* Stored Procedure FilterColl tblDocument*/
CREATE PROCEDURE spFilterColltblDocument

-- FK tblDocuType.DOCTID
@docDOCTID int,
@blndocDOCTID bit,
-- FK tblDossier.DOSID
@docDOSID int,
@blndocDOSID bit,
@DocNr varchar(50),
@blnDocNr bit,
@docTotBedragExcl float,
@blndocTotBedragExcl bit,
@docTotBtw float,
@blndocTotBtw bit,
@docAlgOms varchar(2000),
@blndocAlgOms bit,
@docOref varchar(50),
@blndocOref bit,
@docUref varchar(50),
@blndocUref bit
As SELECT *
FROM tblDocument
WHERE (docDOCTID = @docDOCTID OR @blndocDOCTID = 0)
AND
(docDOSID = @docDOSID OR @blndocDOSID = 0)
AND
(DocNr like @DocNr OR @blnDocNr = 0)
AND
(docTotBedragExcl = @docTotBedragExcl OR @blndocTotBedragExcl = 0)
AND
(docTotBtw = @docTotBtw OR @blndocTotBtw = 0)
AND
(docAlgOms like @docAlgOms OR @blndocAlgOms = 0)
AND
(docOref like @docOref OR @blndocOref = 0)
AND
(docUref like @docUref OR @blndocUref = 0)

RETURN
GO

Public Function FilterDocument(Optional ByVal intdocDOCTID AS Integer = 0,
Optional Byval blnintdocDOCTID AS Boolean = False, Optional ByVal
intdocDOSID AS Integer = 0, Optional Byval blnintdocDOSID AS Boolean =
False, Optional ByVal strDocNr AS String = "", Optional Byval blnstrDocNr AS
Boolean = False, Optional ByVal dbldocTotBedragExcl AS Double = 0, Optional
Byval blndbldocTotBedragExcl AS Boolean = False, Optional ByVal dbldocTotBtw
AS Double = 0, Optional Byval blndbldocTotBtw AS Boolean = False, Optional
ByVal strdocAlgOms AS String = "", Optional Byval blnstrdocAlgOms AS Boolean
= False, Optional ByVal strdocOref AS String = "", Optional Byval
blnstrdocOref AS Boolean = False, Optional ByVal strdocUref AS String = "",
Optional Byval blnstrdocUref AS Boolean = False) as DataSet

Dim arParms() as SqlParameter = New SqlParameter(15) {}

arParms(0) = new system.Data.SqlClient.sqlParameter("@docDOCTID",
SqlDbType.int)
arParms(0).Value = intdocDOCTID
arParms(1) = new system.Data.SqlClient.sqlParameter("@blndocDOCTID" ,
SqlDbType.bit)
arParms(1).Value = blnintdocDOCTID

arParms(2) = new system.Data.SqlClient.sqlParameter("@docDOSID",
SqlDbType.int)
arParms(2).Value = intdocDOSID
arParms(3) = new system.Data.SqlClient.sqlParameter("@blndocDOSID",
SqlDbType.bit)
arParms(3).Value = blnintdocDOSID

arParms(4) = new system.Data.SqlClient.sqlParameter("@DocNr",
SqlDbType.varchar, 50)
arParms(4).Value = strDocNr
arParms(5) = new system.Data.SqlClient.sqlParameter("@blnDocNr",
SqlDbType.bit)
arParms(5).Value = blnstrDocNr

arParms(6) = new system.Data.SqlClient.sqlParameter("@docTotBedragE xcl",
SqlDbType.float)
arParms(6).Value = dbldocTotBedragExcl
arParms(7) = new system.Data.SqlClient.sqlParameter("@blndocTotBedr agExcl",
SqlDbType.bit)
arParms(7).Value = blndbldocTotBedragExcl

arParms(8) = new system.Data.SqlClient.sqlParameter("@docTotBtw",
SqlDbType.float)
arParms(8).Value = dbldocTotBtw
arParms(9) = new system.Data.SqlClient.sqlParameter("@blndocTotBtw" ,
SqlDbType.bit)
arParms(9).Value = blndbldocTotBtw

arParms(10) = new system.Data.SqlClient.sqlParameter("@docAlgOms",
SqlDbType.varchar, 2000)
arParms(10).Value = strdocAlgOms
arParms(11) = new system.Data.SqlClient.sqlParameter("@blndocAlgOms" ,
SqlDbType.bit)
arParms(11).Value = blnstrdocAlgOms

arParms(12) = new system.Data.SqlClient.sqlParameter("@docOref",
SqlDbType.varchar, 50)
arParms(12).Value = strdocOref
arParms(13) = new system.Data.SqlClient.sqlParameter("@blndocOref",
SqlDbType.bit)
arParms(13).Value = blnstrdocOref

arParms(14) = new system.Data.SqlClient.sqlParameter("@docUref",
SqlDbType.varchar, 50)
arParms(14).Value = strdocUref
arParms(15) = new system.Data.SqlClient.sqlParameter("@blndocUref",
SqlDbType.bit)
arParms(15).Value = blnstrdocUref

Return SqlHelper.ExecuteDataset(gstrCnn, CommandType.StoredProcedure,
"spFilterColltblDocument", arParms)

End Function
"Good Enchiladas" <go************@hotmail.com> wrote in message
news:U0*******************@fe2.columbus.rr.com...
I have an object model that exposes a class inheriting from collection base.
I would like to make this class available to an ASP.NET data grid, such as
in the following code:

grid.DataSource = MyObjectModel.MyCustomers.Filter("Smith%")

The problem is that MyCustomers is a lazy loading property of MyObjectModel that, by default, returns ALL customers. So
"MyObjectModel.MyCustomers.Filter("Smith%")" first does the lazy load of ALL customers THEN loads the customers filtered to the names beginning with
"Smith." The undesired initial lazy load is wasted.

How do I prevent this initial lazy load when a Filter method is called as in above, but still retain the ability to make the very clean syntax of a call such as "grid.DataSource = MyObjectModel.MyCustomers" when I want all
customers?

Nov 20 '05 #3

"EricJ" <er********@ThiSbitconsult.be.RE> wrote in message
news:40***********************@news.skynet.be...
if you are working w sql server make a stored proc that dous the filtering, that way only the filtered data is sent. (you could do it as a sql string
from code but this gives troubles with ' ...


I am already using stored procedures to do the filtering.

And the idea of automatically generating CRUD stored procedures using
sysobjects and syscolumns is a great idea that I've already suggested to my
team. You are right, it IS very easy for everyone to do.

Thank you.
Nov 20 '05 #4
> Just overload it,

Function() as dataset
Do your code without test
Function(byval search as string) as dataset
Do your code with test


Maybe code will help visualize what I'm trying to do.

Public Class MyObjectModel
Private _customers As MyCustomerCollectionBaseClass
'NOTE: MyCustomerCollectionBaseClass inherits from
CollectionBase

Public ReadOnly Property Customers()
Get
'Lazy load of all customers
If _customers Is Nothing Then
_customers.LoadAll()
End If
Return _customers
End Get
End Property

Public Function Filter(ByVal nameSearch As String) As Customers
'search login goes here -- uses stored proc, etc.
End Function

End Class

Maybe you are right. Maybe I should use a overloaded function instead of a
readonly property. I'll take a look at it.

I am just worried that I am going to lose the clean syntax of:
grid.DataSource = MyObjectModel.Customers() 'will the parentheses have
to be here?
but I guess:
grid.DataSource = MyObjectModel.Customers(CustomerFind.FindByName,
"Smith")
isn't too bad (CustomerFind.FindByName is an Enum value for a name-based
search). I guess I just preferred:
grid.DataSource = MyObjectModel.Customers.Find(CustomerFind.FindByNa me,
"Smith")
but this resulted in an initial load of all customers first then a filtered
load (using a stored proc).

Thanks.
Nov 20 '05 #5
not only sp's :p
also a complete base class calling the sp's containing elements of other
classes ..., a wrapper for the base class and a collection class based on
the wrapper :p. I was thinking on doing base manage forms 2 but that will be
for next time.

my programmers (and my boss) love it :)

"Good Enchiladas" <go************@hotmail.com> wrote in message
news:iL****************@fe2.columbus.rr.com...

"EricJ" <er********@ThiSbitconsult.be.RE> wrote in message
news:40***********************@news.skynet.be...
if you are working w sql server make a stored proc that dous the filtering,
that way only the filtered data is sent. (you could do it as a sql string from code but this gives troubles with ' ...


I am already using stored procedures to do the filtering.

And the idea of automatically generating CRUD stored procedures using
sysobjects and syscolumns is a great idea that I've already suggested to

my team. You are right, it IS very easy for everyone to do.

Thank you.

Nov 20 '05 #6
Good,
How do I prevent this initial lazy load when a Filter method is called as in above, but still retain the ability to make the very clean syntax of a call such as "grid.DataSource = MyObjectModel.MyCustomers" when I want all
customers? The only way I can think of off hand is to defer the lazy load within the
MyCustomers collection itself. Don't do it in the MyObjectModel.MyCustomers
property itself, but in each method of the MyCustomers class. I would
probably use a Strategy pattern (via delegates possible) to implement the
lazy load. The constructor of MyCustomers would set the delegate field to
the load routine, the last thing the load routine does is

The Filter property itself would not lazy load the collection itself.

Instead of (in addition to) the pattern you've started, I would consider
using the Finder Pattern as identified in Martin Fowler's book "Patterns of
Enterprise Application Architecture" from Addison Wesley.
http://www.martinfowler.com/books.html#eaa

The Finder pattern was identified as part of the Data Mapper Pattern
(http://www.martinfowler.com/eaaCatalog/dataMapper.html). I would implement
the Finder pattern as a Shared Property of the respective class.

Public Interface ICustomerFinder

' finds by primary key
Function FindByID(id As Integer)
' finds by filtering name
Function FindByName(name As String)

End Interface

Public Class Customer

Public Shared Readonly Property Finder As ICustomerFinder

End Class

The ICustomerFinder is setup for the Separated Interface pattern, where the
Data Mappers & Finders are in a separate assembly from the Domain (business)
classes.

The above allows you to:

' return a list of customers
grid.DataSource = Customer.Finder.FindByName("Smith%")

' return a single customer
aCustomer = Customer.Finder.FindByID(123)

Martin's book also covers a couple of Lazy Load options.

Hope this helps
Jay

"Good Enchiladas" <go************@hotmail.com> wrote in message
news:U0*******************@fe2.columbus.rr.com... I have an object model that exposes a class inheriting from collection base.
I would like to make this class available to an ASP.NET data grid, such as
in the following code:

grid.DataSource = MyObjectModel.MyCustomers.Filter("Smith%")

The problem is that MyCustomers is a lazy loading property of MyObjectModel that, by default, returns ALL customers. So
"MyObjectModel.MyCustomers.Filter("Smith%")" first does the lazy load of ALL customers THEN loads the customers filtered to the names beginning with
"Smith." The undesired initial lazy load is wasted.

How do I prevent this initial lazy load when a Filter method is called as in above, but still retain the ability to make the very clean syntax of a call such as "grid.DataSource = MyObjectModel.MyCustomers" when I want all
customers?

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joyce | last post by:
In my schema I have 2 enumerations, let's say, country description and country code, and I want to use them so I can map each country description to its precise country code (and no other). So far...
0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
21
by: Christopher Benson-Manica | last post by:
I'll try to explain what I want to do: I have foo.h and foo.cpp. Units that include foo.h will define an enumeration bar: enum bar { typeNone, typeBaz, typeQuux, ... , count }; A method...
3
by: JoeH | last post by:
Hi, I'm using a COM DLL (created in VB) in my javascript code and can successfully call its methods and get/set its properties. There are also some Public enumerations defined in the ActiveX...
5
by: Seamus M | last post by:
I can't find any info on enumerations in the PHP manual, so I assume there is no built in way to create them. Can anyone tell me the best way to build a simple enumeration, such as: Enum...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.