Wednesday, January 28, 2009

How to bind a List with custom objects to a Gridview using an ObjectDataSource

First of all it's important to understand how the ObjectDataSource works.

Here is a quote from MSDN..

The ObjectDataSource is an ASP.NET data source control that represents a data-aware middle-tier object or a data interface object to data-bound controls. You can use the ObjectDataSource control in conjunction with a data-bound control to display, edit, and sort data on a Web page with little or no code.

A very common application design practice is to separate the presentation layer from business logic and to encapsulate the business logic in business objects. These business objects form a distinct layer between the presentation layer and the data tier, resulting in a three-tier application architecture. The ObjectDataSource control enables developers to use an ASP.NET data source control while retaining their three-tier application architecture.

The ObjectDataSource control uses reflection to create instances of business objects and to call methods on them to retrieve, update, insert, and delete data. The TypeName property identifies the name of the class that the ObjectDataSource works with. The ObjectDataSource control creates and destroys an instance of the class for each method call; it does not hold the object in memory for the lifetime of the Web request. This is a serious consideration if the business object that you use requires many resources or is otherwise expensive to create and destroy. Using an expensive object might not be an optimal design choice, but you can control the life cycle of the object by using the ObjectCreating, ObjectCreated, and ObjectDisposing events.


In this article I'm going to build further on what I did in this post.

Firstly I commented the code in the Page_Load of Default.aspx. After that, I dragged an ObjectDataSource to my Default.aspx using the designer.

I right-click on the ObjectDataSource and choose to configure it, which opens a wizard.

First I choose a Business Object. In this case it will be the Persons Class.



Step 1 of the wizard

After choosing my Business Object, I need to select a method which returns data. This method needs to return a datataype which implements IENumerable!

After selecting my select method, I finish the wizard.



Step 2 of the wizard

Now I configure my GridView by using the smarttag on the GridView. I set the ObjectDataSource of the GridView and enable Paging.



Configuring the GridView

And voila, as easy as that.



Voila!

I really hope Inserting, Deleting and Updating data is going to be as easy as this!

I think this is a great way to build a robust application where you have full control of what you are doing. You can debug right into your Business Objects. The only downside is that you have to write a little more code, but this does not outweigh the advantage of full control.

Monday, January 26, 2009

How to bind a List with custom objects to a Gridview

Curt Christianson told me in his interview that he uses Lists of custom objects as a datasource for his controls.

I wrote a little demo to try it myself. I binded a List of custom objects to a GridView. This shows again what a powerful control the GridView is. You can bind anything to it, as long as it implements IEnumerable. Here is the tutorial.

First I added one folder called BLL (Business Logic Layer). It would be better to put this layer into a different project, but this isn't necessary and would only complicate the tutorial.

In the BLL I added a class Person and I added a class Persons with one method which returns a dummy list of Persons.



Person.cs



Persons.cs

In the code-behind of the Default.aspx, I simple referenced to the namespace ListDemoCsharp.BLL, hooked up the list of Persons to a GridView, and databinded that GridView.



Default.aspx

And that's it! I'm going to experiment with it a little more, because you need to handle sorting/paging yourself.

Monday, January 12, 2009

Interview with Curt Christianson (Part II)

This is part II of the interview with Curt Christianson. For Part I go here.

Part 2: Some techtalk on mainly ASP.NET webproject-architecture mixed with a bit of AJAX and WPF



Me: Let me ask you some general questions about building a webapplication. What is your advise on setting up the layers of a webapplication. In the webapplication I'm currently working on we chose a not so standard architecture I guess. We have multiple "modules", which are folders in practice. In the root of these modules we hold our aspx pages. For each module we have three folders: BLL, DAL and Controls. In our BLL we hold our objects, some logic, ... In our DAL we stock our typed datasets based on stored procedures. We divided our pages into Controls, because our pages are pretty big. We access all of our datamethods in the controls, using ObjectDataSources based on the typed datasets. We try to make as much use as possible from the ObjectDataSources using the designer, some custom things are done in code-behind. But we found out that this architecture is a hassle. It doesn't give a much as control as we'd like and debugging/logging really is a pain in the ass. A problem is that we can't use the designer as much as we want, because we needed to add more functionality/business logic than the designer allows us. With the result that some of the logic is in the aspx page and some in the code behind. What is in your opinion a better architecture?
Curt: From the sound of it you are running into some of the same circumstances I did. I found that using the pre-build datasources were WAY too much limitation for me. I tend to toss them out right away and switch to a more manual approach. This does make more work (you have to do sorting and paging yourself) but other than that I found it a lot easier to work with.
I used to be a fairly strong believer in the 3 tier approach but have come to the conclusion that for 99% of work that's done you are best off with a simple 2 tier approach. There is no real need to seperate the Business and Data layers. Combining the object definitions with the calls to the database and mixing in the validation from the business perspective all at once seemed the best bet. I still break the pieces out into seperate methods but I've combined them all into one set of classes instead of 2 distinctive ones.
Now, as far as controls go... my rule is simple... if I use it more then once it's a control...if not, then it's coded into the page. I never make a code block into a control unless there is a reason to...otherwise it's extra work for yourself. That said... i was VERY guilty of the opposite. I used to take every Asp.Net control and make a custom control that inherited from the native one. Then I would only use the custom one...just in case some day I wanted to enhance it (I almost never did) so eventually I stopped that nonsense.

Me:What is your opinion on using stored procedures? Some say using stored procedures is oldschool.
Curt: I've really shifted away from it. In the past, with previous versions of MS SQL you really wanted to use them as much as you could for perf reasons, but now that's really not the case. What I've done is actually make all my SQL statements simply constants in my application. They are all in one class file so they are easily found and tweaked when needed. I do parameterize everything though...this is a MUST in my book.

Me: About using the sql queries in your classes.. Are you simply returning datasets from your functions and binding these to your controls? Are you planning on using LinqToSql in your future projects for this? What is your opinion on Linq? I also wonder if you have any idea what is Microsoft's opinion on the issue of not being able to use the datasources for more complex things, because basically that's what they are designed for, to reduce development time. What is your opinion on this. Should Microsoft do something about it?
Curt: I used to use datasources a lot but since vs2005 I discovered Generic.List(of ). This has been my favorite thing. Everything now is bound to a List(Of myObject), hence the custom sorting and paging. I've used LinqToSql in the past to play around but the limitations were too great. My objects are often complex and it didn't really handle them well. The new Entity Framework stuff looks more promising though, and it looks like LinqToSql is already "obsoleted" in the framework anyways.
I don't think these datasources were really meant for anything other than small-midsized applications (complexity wise). They were always meant (in my opinion only of course) to be for those times when you need it fast and easy and you have a one-to-one relationship with your datamodel and application classes.

Me: You also co-edited a book about AJAX? AJAX is an awesome technology. "Javascript that works". I really think I am not using AJAX to it's full potential though. What I am using is the UpdatePanel (which is the best control ever) and the AjaxControlToolKit. That's it. What am I missing here?

Curt: I wouldn't say edited.. but reviewed. It's different, but same basic concept.
Personally, the UpdatePanel is THE heart of Ajax. Beyond that it's all secondary. So, with that I'd say you are actually right on track with what you need for Ajax.

Me: The other book you reviewed was on WPF, right?
What are your thoughts on WPF? Is it gonna take of soon? Is this the end for WinForms?

Curt: I've actually reviewed at least a dozen different books over the last few years.
The WPF one was a while ago but it's just coming out in published copy.
For me WPF is something that's interesting but not something that I will probably ever do much with. Is *it* the end of WinForms... absolutely not. Is it *part* of the begining of the end... sure.

Now...if I was to take a stab at the prediction of development here's what I see.
WinForms and WebForms will both really "die off" and what you'll see come out of the ashes will be a conglomeration of the two. More like a child of the two rather than one taking over the other. The differentiation between two negligable. The user will never really know, or care, where they are opperating. It's not really "cloud" computing (that's just poor PR in my opinion) but rather a melding of the two. With things like Ajax, WPF, WCF and all the other acromyms, we are on the way. I think in the next 2 years you will see some real distinct breakthroughs, especially with hand-held platforms, that will be the hybrid we're after. Take those and extend them to the larger CPU capacity of the home PC and you get some idea of what could be done.

Me: Are you thinking of more Surface like interactivity?
Curt: No really... surface is cool, and definitely part of it. The multiple-input, touch-sensitive piece will definitely find it's place with the new scheme but I don't think we're really gotten at the core of the new stuff yet, it's still on the verge of being discovered (or tucked in a lab somewhere). Think of a meld between the PC, Laptop/Tablet and SmartPhone, but all as one device with all the best features of them all and integrated into a form factor yet to be determined (I have ideas but still to early to say).

Me: I think that's a good line to end this interview! Thank a lot.

Thursday, January 8, 2009

Interview with Curt Christianson (Part I)

Curt Christianson is one of the administrators of forums.asp.net. Last month I stumbled across his blog, and felt like I had to do an interview with him. And here we are!

I have divided his interview in two parts:

  • His career and his life

  • Some techtalk on mainly ASP.NET webproject-architecture mixed with a bit of AJAX and WPF



Part 1: His career and his life



Me: Who are you? Where do you live? What do you do for a living?
Curt: The question of who I am is a toughie... I'm really at a transition point in my life. For years now (too many to count) I would have simply answered "geek" but with this last year coming to a close and the start of a new one I feel like I've also hit a changing point. I've refocused my energies on my life and family (just got engaged, just bought a new house) and shifted away from a lot of the day to day "geekdom" that ruled me (I closed up my consulting business). I live, and grew up, in Central Wisconsin. The weather is nice (excluding the heavy winter months), the cost of living is great and the education systems are really quite great (thinking about starting a family soon). I pay the bills with a good job as a high level programmer with Asp.Net at a good sized insurance company. It's not the glamorous lifestyle but it's definitley a solid position with a good future. For a number of years I supplemented my daily work with private applications development with my consulting business but eventually the time comes where you need to pick priorities and, as it should, family and friends won out.

Me: How old are you? How long have you been into software development? Can you describe your experience as a junior developer?
Curt: I'm currently 36 and have been in the computer industry (professionally) for about 12 years but I've really been a geek since I got my first Atari 2600. My career is actually one that's a little different (I think) from most of the Sr. level developers out there. First off I actually went to school for something completely different. I was an English Lit. major in college (I attended the Univeristy of Wisconsin) but after 5 years I called it quits and left when I couldn't afford it anymore. When I returned from school I took a job at a software duplication company working the line. It was a 100% non-technical position and simply an assembly line type thing. While working there the equipment would break a lot... so I tended to take on the initiative of fixing the stuff myself. Eventually they hired me into their tech support area but that was pretty boring to me. I left there and went to work for an off-shoot of the state where I traveled around and worked at distance learning labs all around the State. I also was contracted out to help out a couple school districts in the area part of the time. My focus was all on the networking and server side of things. That lead me to another company that was an actual software focused business but my role was more of one of server setups and networking support again. After a few years I got pretty bored with it. Eventually I got a job as an Asp developer (not Asp.Net but the old stuff) for a construction company. I pumped out some great work while I was there and actually got noticed by Microsoft and was awarded the MVP award for the first time. That was October 2002 I believe. The company I was at was has a very small I.T. area and I started to get the itch to do more so I took the opportunity to go to an insurance company specializing in travel related products. I stayed there for about 3 years and really honed my .Net skills. Like most of us I got the itch again and moved on to a larger company. This time it was another insurance business but with a huge I.T. area and some incredible projects underway. That was about 3 years ago now and things are still going strong..

For me to describe my time as a Junior developer it would really only be one word...."short". When I started development the programming department at the company I was at consisted of ME..... There were a few others but for the stuff that I was doing there wasn't anyone but me really (and the newsgroups...they saved me). With this, I really had to immediately step up and take on a more controlling role. So in reality my first programming job was as a Sr. developer and from there I continued to take on new roles at new companies but always in a lead/Sr. type position. I think there are some developers out there that are destined to always be a Jr. developer. Some lack passion, others skill, but most of the ones out there that have at least one of those two things (or better yet, both) will quickly find themselves climbing the ranks. People who love what they do will get noticed and that will usually be the key to getting the next step up the ladder.

Me: When did you start up your consulting business? What are your thoughts afterwards on having a consulting business together with a fulltime-job? Is it worth it?
Curt:As far as my consulting business goes it's really a 2 parter. First off, back in the late 90's I had a computer business that I ran. Mostly I built custom PCs for people and would come out to their house to fix problems they had. This wasn't too bad but the profit margins (thanks to all the online retailers starting out) were tanking and there was little money left to be made. I closed up that business but took the pieces I learned and in 2000 started up df-Softare. I was writting custom apps (mostly web portals) for the small and mid-sized businesses out there. It was something I really enjoyed doing and managed to make a few bucks although it was never significant enough to let me quit my day job. I ran df-Software and had a pretty good time but eventually the time impact was a little too much and I decided to close shop. I still do the occasional site but these days it's mostly for fun. Was it all worth it? Sure... besides really learning a lot about way things can go wrong it also led me down a path where I had the MVP award for 6 years. The MVP title really was a huge perk when job hunting too. Besides those things it also let me see what it really takes to survive in the business world. It's a lot harder than it looks! Those things really helped me become a better person outside of work too I think. It's easier to see outside of your own "box" and look in from the other perspective now and then.

Me: Amazing career! In your current job.. What is it exactly thatyou do? Are you developing 100% of the time? Or are you more of a teamguru/systemarchitect who instructs the other developers, tells them what to do and how to do it?
Curt: Currently my role is as a Sr. Programmer. The company I'm at has it's own Architecture area as well as segregating the Middleware and Data teams out from the developers. Unofficially I'm a "goto guy" for anything .Net or even just general programming. With the projects I'm currenly on I spend about 60% of my time coding and the remaining 40% doing coordination and assisting others. I've also acted as a liason with the University locally and worked as the lead for their Comp. Sci. 480 students who come to our company under that program. It's like an internship.

Me: In general, what are the things you try to teach these students?
Curt: Mostly we aren't there to "teach" them so much as let them get a feel for a real-world project. What it's like to design, document, code and test an application. With only 1 semester it's tough to do but it's still a good experience for all.

Me: Which part of the ASP.NET framework would you enjoy talking about?
Kurt: I have the advantage of being with Asp.Net since it's pre-beta days. At an early MVP summit the Asp MVPs (there was no Asp.Net at the time) were taken in to a lab at Microsoft and allowed to play with the "bits" that later became Asp.Net 1.0. It blew us all away. Prebuilt controls, compiled code, object oriented programming and an intellisence development environment... WOW. It was like the computer Gods took a liking to us and blessed us with their favor. Most of us at the time were dealing with pure scripting and using tools like notepad for our development environment so there really didn't have to be a huge leap for us to feel like we really gained ground but this was really a lot more than we hoped for.
Now that I gave you my background in Asp.Net... you can see it would be hard to limit myself on what to talk about. I've really worked at it over the years, staying up to date as much as I can, but I have to be honest and say that a lot of the developments/enhancements that have come out in the last year have really passed right by me. LINQ, MVC, DynamicData just to name a few... these things I can't really talk on with any strong knowledge. I've messed around with them, and used them a little and can see how they could really help but I'm an old-school developer. Taking away too much control is something that's a little scary. Anyway... feel free to ask on anything you would like. If it's a topic I haven't gotten to know well then I'll say so.
Oh, and I'm not sure if I've mentioned but I've done a ton of manuscript reviews for publishers. I'm quoted on a few covers from Manning press (Asp.Net Ajax in Action and WPF In Action) as well as being listed in the acknowledgements sections of quite a few books related to programming.

To be continued..

The tech-talk will continue in part II of this interview which will be published next week tuesday I think.

Tuesday, January 6, 2009

Creative advertising

I came across this advertisment for donor transplants. I lolled!



Becoming a donor is probably your only chance to get inside her.

Monday, January 5, 2009

In the list of Top Answerers!

I try to participate in forums.asp.net as much as possible and I found out today that I'm in the list of Top Answerers. That's motivating! :)

Friday, January 2, 2009

The weakest link

I've had some hardware problems the last couple of days. My brand new SATA harddisk decided to give up. My system was making random noises and I got a boot failure error twice, so I decided to run a S.M.A.R.T. tool to make sure it was my harddisk that was broken and I could get a new one for free. The tool I used was SeaTools for Windows, which is straight-forward and easy to use. As I expected the test failed.

I removed the broken harddrive and replaced it with my older IDE harddrive. Once XP was installed I installed my copy of GTAIV. I noticed that the performance was a tad worse. Loading the levels, maps, ... was a lot slower all of a sudden! SATA is four times faster then IDE, but I really had no clue that this would affect playing games.. Most game codes are helt in memory, while the graphics card handles rendering. The game only accesses the harddrive to load textures, new opponents, saved games.. I think it's really crazy that playing a game can give such a hard time to a harddrive.

Out of curiosity I decided to test how the temperature of the harddrive is affected by playing GTAIV. I downloaded a tool called HWMonitor. While scrolling to the documentation I saw that they have made a System Monitoring Development Kit for .NET, which is awesome, but costs more then 1000€. After installing, this UI gave me a overview of the temperature of my hardware. As you can see my harddrive was runnin at 42°, which is not great but ok I guess. I couldn't find any temperature references on the Maxtor site, and Google couldn't help me either.



Onto testing.. Let's play a bit!



20 minutes later..



The temperature of my harddrive only went up by 3°, although I'm almost using 100% of it's resources. Interesting to know that the temperature of my harddrive and CPU (!) are pretty stable. The temperature of my graphics card increased with 50%, which is normal due to all the rendering it has to do.

It's funny how one slow(er) component, can downgrade the performance of your system. To the conclusion.. The chain is only as strong as it's weakest link!

All by all I'm satisfied with my system again. My new SATA harddrive should arrive by Saturday. Time to start thinking of a back-up strategy!