Jonas Stawski

Everything .NET and More

Make Your Business Layer a Data Component

One of the cool features of ASP.NET 2.0 is the new Object Data Source, which lets you bind your business entities/objects to your ASP.NET controls. What I hate about this control is imagethat when you open the dropdown you get a list of all the classes that are referenced in your project, which in some cases in can be a lot of them. If you check the Show only data components box then most of the time the dropdown empties out.

So how do you get your business layer classes to show as Data Components? All you have to do is add the attribute DataObject to the class. According to the MSDN documentation: "Identifies a type as an object suitable for binding to an ObjectDataSource object."




public class manager
  //methods go here
<DataObject()> _
Public Class Manager
   'Methods go here
End Class

If your Business Layer is in a separate layer (project) from your UI then you will need to recompile. Now when you check image"Show only data components" you get a smaller list with your Business Layer class.

Furthermore, you can decorate your methods with the DataObjectMethod attribute which "Identifies a data operation method exposed by a type, what type of operation the method performs, and whether the method is the default data method."



[DataObjectMethod(DataObjectMethodType.Select, false)]
public static List<User> GetUsers()
    //Implementation goes here
<DataObjectMethod(DataObjectMethodType.Select, false)> _
Public Shared Function GetUsers() as List(Of User)
    'Implementation goes here
End Function

Happy Data Binding!

Comments (4) -

Hey Now Jonas,

Great content & concept displayed well in this post. As always..

Thx 4 the info,


Great tip - will definitely add this to my code generation...


Victor E. Diaz
Victor E. Diaz

Gran aporte!


Sérgio Douglas Lopes Dias
Sérgio Douglas Lopes Dias

Simple and complete.

Thank you


Add comment