Home   Demos   Sample Applications   Download   Forums   Articles   About

Lollygag Demos — Object Access

Accessing Business Objects

A Lollygag application can access a business object just as easily as it can access database data. To call a server-side business object, you declare a <objectCommand> tag in your DataServices.config file.

For example, the following C# class includes a method named GetMovies that returns a collection of Movie objects.

Movie.cs

using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; /// <summary> /// Returns movies /// </summary> public class Movie { const string connectionString = @"data source=.\SqlExpress;Integrated Security=true;AttachDbFileName=|DataDirectory|MoviesDB.mdf;User Instance=true"; private string _title; private string _description; public string Title { get { return _title; } set { _title = value; } } public string Description { get { return _description; } set { _description = value; } } public static List<Movie> getMovies() { List<Movie> results = new List<Movie>(); SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("SELECT Title, Description FROM Movies", con); using (con) { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) results.Add(new Movie(reader)); } return results; } public Movie(SqlDataReader reader) { _title = (string)reader["title"]; _description = (string)reader["description"]; } public Movie() { } }

The following DataServices.config file exposes the Movie object. Notice that the file contains a <objectCommand> tag. This tag has two attributes that represent the object name and the object method to call.

DataServices.config

<?xml version="1.0" encoding="utf-8" ?> <dataServices xmlns="http://lollygagframework.com/dataservices" defaultConnectionStringName="conMovies"> <dataService id="svcMovieObject"> <objectCommand type="select" objectName="Movie" methodName="getMovies" allowAnonymous="true" /> </dataService> </dataServices>

After you expose a business object, you can bind to the object from your client-side widgets in exactly the same way that you bind to database data. The following Lollygag application includes a dataview widget that displays the data returned by calling the Movie.GetMovies() method.

ShowMovies.xml Try It!

<?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet type="text/xsl" href="/lollygagSite/lollygagframework/0_1/lollygag.xsl"?> <lolly:application title="Hello World" xmlns="http://www.w3.org/1999/xhtml" xmlns:data="http://lollygagframework.com/data" xmlns:lolly="http://lollygagframework.com/core"> <data:dataview dataServiceId="svcMovieObject"> <data:template> <h1>{{Title}}</h1> <p> {{Description}} </p> </data:template> </data:dataview> </lolly:application>

More Demos