To use an OpenAccess persistent class in Sitefinity, first install OpenAccess and create a new Class project in Visual Studio. Next, create the objects you want OpenAccess to persist. Note: You cannot create your persistent objects in ~/App_Code.
Make your Persistent Class part of your Solution
Open your Sitefinity web site in Visual Studio by clicking File -> Open Web Site. Navigate to the directory of your Sitefinity web site and click Open.
Click File -> Add -> Existing Project to make your Persistent Class part of your current project. (Note: alternately you can add a reference to your persistent class' DLL or simply copy the DLL file to your web site's ~/bin folder.)
Navigate to the location of your persistent class' project file, select the file and click Open.
Your persistent class should now become part of the Solution Explorer.
Add a Reference to Telerik.OpenAccess and Telerik.OpenAccess.Query
Right-click on the root of your Sitefinity web-site and click Add Reference.
Locate Telerik.OpenAccess in the .NET tab, select it and click OK.
Repeat the step above and select Telerik.OpenAccess.Query as well.
Add a Reference to your Persistent Class
Right-click on the root of your Sitefinity web-site and click Add Reference.
Select your project in the Projects tab and click OK.
Making use of the Persistent Class
We're now ready to use our persistent class inside Sitefinity. Let's look at some code:
using System;
using System.Web;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyClass.Customer cust = new MyClass.Customer();
cust.FirstName = "Gabe";
cust.LastName = "Sumner";
Telerik.OpenAccess.IObjectScope scope = MyClass.ObjectScopeProvider1.GetNewObjectScope();
scope.Transaction.Begin();
scope.Add(cust);
scope.Transaction.Commit();
}
} That's it! With 7 lines of code OpenAccess will create the customer database table and generate/execute the SQL needed to store this new customer object. Check the database and you'll see a new customer table with 1 row.
We could then fetch and display all Customer objects using LINQ with the following code:
using System;
using System.Web;
using System.Linq;
using Telerik.OpenAccess.Query;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Telerik.OpenAccess.IObjectScope scope = MyClass.ObjectScopeProvider1.GetNewObjectScope();
var result = from c in scope.Extent<MyClass.Customer>() select c;
foreach (MyClass.Customer customer in result)
{
Response.Write(customer.FirstName + " " + customer.LastName + "<br />");
}
}
} When using LINQ, be sure to add "using Telerik.OpenAccess.Query".
In the examples above I've placed the code in an ASP.NET page code-behind file. However, I could also place this code in a UserControl. This control could then be dragged & dropped onto your Sitefinity pages.
Where to Now?
We've barely scratched the surface of what you can do with OpenAccess. I'll publish more articles in the future. For now, please check out the support page and the product forums.