Google Search

Google
 

Sunday, August 31, 2008

Video: How Do I: Use the ObjectDataSource with a Data Access Class and Business Object

In this video Chris Pels will show how to use the ObjectDataSource as the intermediate layer between data bound user interface elements and a custom data access class for a business object. Uses of the ObjectDataSource in the GUI Design mode and declaratively in the source view of an .aspx page are discussed. In addition, the considerations for design of the data access class and business object for use with the ObjectDataSource are covered.

The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video


Minute 0

Will rule just those this will views this as objects with the object data source in ASP net with this survey committee website and in this website will we going to do is who can I have a business object which we can call customer and within a bind that customer object to the object data source in an ASP net webpage solicit up the default aspx page a bit before you get into the business object side of things when they do is place a couple controls on this webpage and it lets see the first one we re going to put on here is a review that could be the object would you use to display the data in that we also need an object data source will put that

Minute 1

Data on the form and we ll come back to configuring the object data source in the grid in a few minutes slowly want to do is have the data in this grid be bound to a set of custom business objects that we create subordinate to overhear the website and you could do this in a separate assembly for what they would hear the website just for demonstration sake is class which will can call customer and business can be our business object which contains our customer data and then we can create a companion class which will can call customer data so let s set a class customer data and this is to be the class that controls the data access to and from our customer objects so through our customer class and flesh that out first select the

Minute 2

Several properties in year of its greater private variable for the company name and it will make that a string and will create a corresponding property and that also a lot of sleepy type string and then will return from that the private rival contents and likewise will set a sense for a company and let s just put a couple more in here is the one for the city and it will creep across my property set to get and set it will

Minute 3

Turn_city and will set_city equal to the inbound value and thus create one less one wall call state and will create the property as a string in it that we have returned_state and full set_state so this is going to be our business object that stores our customer information we also need to put a constructor insider class so to emigrate to different constructors so let s create a public s of new and will choose to leave

Minute 4

As it is no part in dump routers to that constructor illustrate a second constructor and into this blood work and no allow a the inbound constructor to have parameters to set the different values in our company object so we ll see but as string and then we ll say the city to live for city string and will say us via a substring and out before our state and then named out in his constructor will disable me got company equals sealant bleeding down parameter value meet at sea will see T Y and me State 60 and LOL is the ease of the instantiate an instance of the customer object and set the different values of the properties

Minute 5

It don t have to have this secondary constructor as I ve been here to set the about properties is just a convenience factor to see when we do the data access by that just makes it a little easier to create instances of our business object now let s go back over to customer data with you as I said is our customer data access class first thing I want to do here is Ed to import statements just make does have a little as typing and the first one is system data and the second one is component model and you ll see how we can use that component model namespace in just a moment

Minute 6

This 80 shared class words should as they were to make the methods have shared inside this class classmate methods for major operations as a person who can make one for inserting tools a public shared insert arts are a set of insert customer and in down to the insert customer method work in a pass an instance of our customer class so we ll call that customer as type customer then let s do one for updating a customer at the very similar call this update customer and it ll pass the customer object and

Minute 7

Let s make one for deleting a customer and shared as well so will it delete customer end in this one are old past day and a customer ID as an integer and then we need a method that will essentially select our customers and let s be bad shared as well this would be a function because we re going to return our customers data and will call this get customers in over there that do here is return a data table now there is several ways you can approach making this get customers method well

Minute 8

One would be to return a data table so essentially what walked the database like the data and return it back as a data table you could potentially return it as a data set that clinic gets away from our custom business object approach and another thing that we can do instead of the data table is we could return a generic collection of customer objects and Israeli law to the basic operation of the object data source in relation to these custom does this object that there is a variety of ways that you could approach this in what you have to do is think about your own situation and determine which of those approaches might be the most efficient for you but the reason on a return a data table is that that s a very quick and efficient operation the alternative returning a generic collection of customer objects would mean that I have to instantiate an instance of

Minute 9

Customer object for sound every customer that I retrieve and then put them into the collection so there is a lot more overhead associated with that and there may be a reason that you want that collection of customer objects because it gives you instant instant access to the customer data in a customer object format but in this particular approach were saying okay we re just in a return a data table because they get customers method is purely for selecting data and displaying it on the screen and we re getting a little squiggly line on 21 because we aren t returning a data table so for the moment let s just return a new data table and that will suffice them to come back and fill the Senate in a moment when I start to show you the relationship between this customer data class and the object data source in effect before we start

Minute 10

Rested with the adapter data source when they put their second get customers method in here so you would see how that might work separatists to another public shared method click as customers however bold when you return from here would be a system collections generic I collection because the work for the object data source it does need to be a type are strongly typed collection and the type of question that we were to return is of type customer objects so what we do here is returned and this is just as this account of a stub at the moment and we go to system collections generic and let s just return a list of type cost

Minute 11

Are in me just comment out this second get customers method because such a suit in the squiggly line on 19 because this has the same signature as to get customers will comment that out now let s go back over to the webpage and take a look at the object data source for the first time to start see the relationship between the object data source and our customer data class swiftly to the smart tag for the object is worth and select configure data source and Thursday were prompted with is choosing a business object and if we look at the list you see there is several business objects listed here the customer and customer data is also this check box on the right to show all the data components and we ll come back and look at that it promoted in just a few minutes have been assured a bit about the connection between this check

Minute 12

Ox and how you code your business object data access class soared up a customer data because they know that s where our data access methods are located and will click next and now you can see where prompted to define what methods in our business object relate to selecting updating inserted and deleting our data and if we go down to the slums like to absolve it out to choose a method and you ll see that we have kicked customers and that s the only one that s being displayed because it s the only one that s a function that returns a set of data that might logically be used for the select statements of the some intelligence here to this wizard in determining what methods are business class or appropriate to did pick that and when we picked the dead customer he can see that it s now scientists are select method and

Minute 13

To do now is go back to the data access class or put a little bit more information in there and will return to this wizard and completed on just a click cancel it alone because I don t want out some of the food to get generated in the source view afar paid just quite yet so click tickets he clicked cancel and I can see that the relationship to the object data source and a data access class to know in a classless look at how we can make the data access class more intelligent or I should say puts him at use the data access class that made the new wizard look at the data access class in a more intelligent manner so will want to do is put some attributes here in our data access class and the first armored output is the key to object attribute as you can see it has two methods are gestures

Minute 14

To inbound parameters that relate to whether or not it s suitable for binding it with an object is source and where artists a true because it is and what this attribute does defines our customer data class as being a bindable class with the object data source and the mages compile our class

Minute 15

Be customer data class because it is marked with that data object attribute soon also back over to her data access class and we can put some similar attributes that are for the methods in the class so lets down and make this a data object method and then you can see there are some the new values to indicate what type of method if this and this is an insert method and then there is an a second signature for this and it s what whether or not this is the default insert method and will say is true that and now we re going to go down and I will put some attributes on the additional methods so say this is also a data access or a data object method and this is an update method and yes it is the

Minute 16

Default walk the same thing for delete data object method of type delete the default and get customers in this one is of type select and it is also the fault insurgency here if we had multiple methods that selected data we might mark the primary one would be to attribute as we ve done here forget customers and the other was my also be marked as a select data object methods button may not be the default method flex compiler class

Minute 17

And then there we are little back over to default is the next configure our object data source and will uncheck this just so we can see everything there is both are we ll pick methods or classes will pay customer data for the next his finger right what it s done now is it s automatically picked our get customers method because it knows that is our default to select method and lets forward to the update path it has also selected the update customer method by default because we have the attribute on it let s just do the two of those at the moment click finish and let s go back into the source to which we haven t seen yet and take a look at some of the things that are inherently just reformat things a little bit so we can see it better

Minute 18

On the screen and you really have a couple or ad views here the hand limit is Rudy ordered these a little bit cousin and read a little more logically so what we have as attributes object data source are what is the type name that is the object for our as our data source in this case is our customer data class and then what is the type of object that is used within this data access class for insert update and that is the customer object and that we have the corresponding names of our different lead insert select an update methods and then you can also see in our we have some delete primer is here because for members of the delete method takes an age term containing the customer ID so we re able to specify what primary is in this

Minute 19

These four are not delete method we also could have separate parameters defined for save the insert method because in that case we might not be passing the customer object we might be passing individual values of like customer named city and state so we can define those insert parameters to be different than the default data object type which is customer source work with a select method first look back over customer data in a air we have in our select method is right here and we have a little stuff in there at the moment which is just returning a new data tables and would not return any data and what I ve done here is create a custom code to create a data cable and out of the sky taken from the toolbox and drag and drop it in here week

Minute 20

As this will save us from behind in that it would take a while for me to type all this while you watch is what this does is it simulates going to the database and getting some data about our customers to his critics are three columns companies city and state and returns them and after putting some data into those column so let s go back over to our default aspx and just do a couple things here and I m just in a set the auto generate columns property of our grid to true and then are data source IP is going to be equal to the object data source one is a copy and paste that somebody make sure we get that name correctly and we could do the same thing

Minute 21

Up in the design view we could select the object data source one of just doing it down here in the source so you can see that there s both approaches now if we go into the design view let s fire up our website and see what happens so before we do that let s just go over into customer data and market customer that the disparate breakpoint here at line 26 and we can see that we do hit this method to retrieve our data for our data source to look back over to default aspx and it s fired up in debug mode s first time we started up sources can add debug most of the web config and page starts slowed and here we are in the method get customers getting our data is just to get out in three decades table populated and will hit F5 to let it run from here and here we have our data coming up in

Minute 22

It really sickens me that is bound for data table from that s coming from our business object smells come back and just take a slightly different approach and one can I do this time is broken up flush out the code here in our data access class for this alternative way of getting that customers as a collection of customer objects and we ll see if we can mind back to work great as well slipped on comment or other method here and we ll just can you give this a different signature organ call this by object is to get customers by object and rather than just return this job like it did with the data table approach I have some code over here that I d made up ahead of time and there it is focused copy that it is pathetic

Minute 23

And tormented and this is simulating it as if we went to the database got the data for our customers and then create a new instance of the customer object and edited to this generic collection of customers which I have called customers in this case to cyclical customers the local rival for the data table off about 60 over using a constructor that is set up were passing in the company name city and state when we re creating each new instance of the customer object and we want to put a data object method attribute on this one as well and it will go down and create that can and will say this is of type select but this one were to say false this is not hard to fault the select method for the customers

Minute 24

Is now role as head is not limited to break the year line 4694 to default the SPX and the school I could do this here in the code but I want to go into the design view to show you something that happens to so you re aware of it Salieri are we need to change the method that s been used for selecting our customers less going to configure data source for the object is worth going to keep the customer data class as a business object and you can see we have the I get customers method and what we want is the get customers by object and you can now see that is available in our drop down it knows its select savings and only take that and let s click finish and see what happens is the grid refresh to intelligently know what properties are of our customer object and let s go back

Minute 25

Into the source you because here we can change it to get customers by object and that s all that changed here now let s fire this up and slowly get all fired up in a debug mode and as the page loads you can see who we are and are committed school lucky we are not get customers by object method just can create instances of the customer as well let it go from there and here they are on the page just like we had from the data table there is one slight change here and that is the order of the fields is toward the columns I should say is slightly different and that s just reflecting what s in our customer object so what we could do is go back here in Tuesday to fault a SPX page and or auto generating a

Minute 26

Columns we could say false here and we could specifically set up bound columns in fact let s do that sells a false go in here and will say columns and then within columns we want as a downfield syllables a data field equal to a company in the header text is also equaled company and it is put another one in a data field equal city header text is also city and in fact let s just leave it at that let s just leave to columns even though we know that the state is there it well as well and see if we get that s let s fire up there we are in our method and

Minute 27

Will continue on president five and up comes Art grid which this company and city so that shows you not only binding to the customer object but it also shows you that we can define the data fields which in essence are the properties of our customer object you re probably familiar with doing this when you have said it is said or did a table where you specify the column names but in this case were actually specifying the data field is a property of our customer object so looked at selecting let s look at how we might do us an insert update or delete to do that we disagree to start editing tool lets go into the grid edit columns going to add a command field for edit update cancel him to be the first column in our grid by default the show edit button is equal to true will just leave

Minute 28

To get an edit button in our grid and now when we had it and decrease goes to update it because it s bound to object data source age should call the update customer method in our customer data class lets go over there set a breakpoint in the update method right here and well we would typically do here in our update customer method is performed the operation such as calling a stored procedure in our database passing inbound parameters to that store procedure obtained from the instance of the customer object to actually update our customer data and we also might refresh the grid back on that page in the code behind default ASP ask to look good to go let s just doublecheck okay let s start up the debug mode here we get our Griffith

Minute 29

Said it say IBM will change the name to test that the update link can see here we are in balance to what the update method in our customer data class hears the instance of the customer object lets expand them down and you can see that the company name coming inbound inside of our customer object it is indeed test which reflects the change we made in the grid so we went on to update the data in the database refresh the grid then we would have that change to our customer data and would follow very similar approach to say the leading or inserting a customer data we hook up the user appropriate user interface element whether it s the created details view wore a custom made forum with text boxes etc we hook that up so that the appropriate method either insert for nuke

Minute 30

For delete for an existing customer was called in down we get the customer object and then we would make the appropriate changes in the database and is one part of this versus lobbyists shut down the webpage one of the back over to the default ASP next page in the source view and point out that I mentioned early on that you can have parameters in this case for the delete parameters we could also change things like this picket at select primer is here that passed in our justice like we did at insert parameters here that passed in the company name city and state as separate parameters say they re coming from eight series of text boxes we could mind to those appropriately passed in the individual values

Minute 31

To our insert customer method in a data access class and use those alternatively we could leave it as a customer object construct an instance of the customer object in the code behind on far page and passed that customer object in down to our insert customer method and it is this more accustomed to insert a different parameters to methods you would want to look at the events associated with the object is source specifically look at the inserting updating and deleting an offense and those events give you an opportunity to manipulate the parameters going in down to the corresponding methods in your object data source source can see this a number of ways that you can approach using the object is source we ve covered a few of them here but it should give you a good perspective on how the basic functionality works as well as the flight

Minute 32

Ability and using the object data source in different ways and then you can figure out in your own situation what the appropriate approach might be for your particular application to occur to download the sample source code that comes with the video is available in both see sharp and Visual Basic then you can use that as a starting point for figuring out the best approach to using the flexibility of the object is forcing your particular application is as crisp else thanks for watch this video on using the object data source with the custom business objects in ASP net or wall

Click here to go to original video page

No comments: