Saturday, April 28, 2007

The esDataSource in this weekends beta can automatically handle sorting for all databases and paging for those databases which have built in paging support (SQL 2005, Oracle and MySQL). If you have been experimenting with our esDataSource we encourage you to upgrade this weekend when the new beta becomes available.

The sample ASP.NET web form below uses Northwind's Employees table and takes advantage of the built in paging capablities of EntitySpaces which in SQL 2005 uses the ROW_NUMBER function. Why Microsoft chose this syntax over the MySQL syntax for paging is hard to fathom. However, you wont have to worry about that as EntitySpaces takes care of this for you. The nice thing about the web form below is that it required almost no coding whatsoever and handles sorting and paging automatically. The TextBox is populated with the text from the EntitySpaces esDynamicQuery.es.LastQuery property each time data is fetched from the database so we can see the actual query.

The page above can also edit, delete, and save the data without writing any code as well. And more importantly, the GridView was setup entirely in design mode. The esDataSource runs just fine in Medium Trust mode as well, you'll find most DataSourceControls do not as they use heavy reflection unlike the EntitySpaces DataSource which uses NO REFLECTION.

So how did we build it?

Let's start from the beginning.

Add the esDataSource assemblies to your project

  1. EntitySpaces.Web.dll
  2. EntitySpaces.Web.Design.dll

Getting esDataSource in your Toolbox

After adding the two assemblies mentioned above (or projects if you have purchased the source code) you should be able to recompile and see the esDataSource show up on your toolbox. If you do not see it you might have to close and reopen Visual Studio. If you still do not see the esDataSource in your Toolbox you can always right mouse on your Toolbox and choose Add Tab to add a new tab. Once you have given your tab a name "right mouse" on the tab and select Choose Items  and browse to the EntitySpaces assemblies folder and click on the EntitySpaces.Web.dll. The assemblies can be found here:

The Trial Version Assemblies
C:\Program Files\EntitySpaces\EntitySpacesDemo\EntitySpacesDemo\Runtime

The EntitySpaces Developer Assemblies
C:\Program Files\EntitySpaces\Redistributables

Design Time Setup 

This is what our final page looks like in design time, notice we have our esDataSource control and a TextBox on the form. The entire page took less than 5 minutes of setup and design time.

Choosing columns

Notice in the image below that we have pointed to our class library named BusinessObjects.dll as the assembly that contains the EntitySpaces collection(s) we desire to use. You might not have to do this depending on how your project is setup. If you have a traditional .NET 1.1 type of "Project" based web solution then you will probably see your esEntityCollections without browsing to an assembly. However, if you have your esEntityCollection in a class library you will need to browse to that assembly or DLL. Reflection is used to populate this information however this reflection is in the EntitySpaces.Web.Design.dll which is not used at runtime. This brings up a tricky issue regarding new .NET 2.0 file based projects. "What assembly do we load?"  Well, for now our recommendation is for you to launch your web application without debugging it and browse to the app_code.dll. You will find it here: 

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\esdatabinding\8a81ff31\51684664\App_Code.fbpesify.dll

Kind of cumbersome we admit, in the above path esdatabinding is the name of the webapplication and you just kind of guess Angry on the other two, it's pretty easy though as there are only like one or two folders in those directories. It's well worth it however considering the time you can save.

Tying the GridView to the esDataSource

Right mouse on your GridView and choose "Show Smart Tag". Set the esDataSource and the DataSource and in this sample we enabled all of the other options. 

After this you can go into the normal GridView designer dialogs and reorder the columns and set other properties.

Implementing a few key esDataSource Properties and Events

If you select your esDataSource and look at the properties you can set the AutoPaging and AutoSorting properties to True. However, you will need SQL 2005, Oracle, or MySQL in order for AutoPaging to work.

Now let's hook up some events.

We only truly need two events, the esCreateEntity and esSelect. For a grid that doesn't allow editing then only the esSelect event is needed. The esPostSelect event is needed to populate the TextBox with the esDynamicQuery.es.LastQuery text which will be explained below.

First, since we have decided to use Paging we need to tell the esDataSource how many rows are in the data that we are browsing. Don't let the code below confuse you. All we are doing here is using our single entity, Employees to get the total row count and assign it the esDataSource.TotalRowCount property, we only have to do this one time. If we add or delete rows of course we should adjust it. We also set the default sorting on the grid which is important as well. Remember, using the built in paging of the database itself means we don't actually return all of the rows, we only return as many rows as we show on each page, this is why this type of paging is very fast even with mega resultsets.

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.Page.IsPostBack)
    {
        Employees emp = new Employees();
        emp.Query.es.CountAll = true;
        emp.Query.es.CountAllAlias = "Count"
        if (emp.Query.Load())
        {
            esDataSrc.TotalRowCount = (int)emp.GetColumn("Count");
        }

        gridView.Sort(EmployeesMetadata.PropertyNames.LastName, SortDirection.Ascending);
    }
}

esSelect

Okay, Let's look at what is required for our esDataSrc_esSelect event which is used to provide the data for the esDataSource.

protected void esDataSrc_esSelect(object sender, esDataSourceSelectEventArgs e)
{
    EmployeesCollection coll = new EmployeesCollection();

   // Assign the esDataSourcSelectEvenArgs Collection property
    e.Collection = coll;
}

Yes, it's that easy. You can have a fully sortable, pageable grid with no more than those few lines of code.

Notice that all had to do was create our collection and assign it to the esDataSourceEventArgs.Collection property. We don't use any Where condition in this example. However, if we did need to use them we would simply use the coll.Query.Where() method to assign them. However, and this is important. If you set the esDataSource to use either AutoPaging or AutoSort you DO NOT load the collection via Query.Load() or LoadAll(). The reason is that the esDataSource itself is going to apply the OrderBy properties for sorting  and or PageSize/PageNumber properties for paging and then call Query.Load() on your collection.

esCreateEntity

If you allow editing in your grid then you need to provide the esCreateEntity event, here is our esDataSrc_esCreateEntity event.

protected void esDataSrc_esCreateEntity(object sender, esDataSourceCreateEntityEventArgs e)
{
    Employees entity = new Employees();

    if (e.PrimaryKeys == null)
        entity.AddNew();
    else
   
    entity.LoadByPrimaryKey((int)e.PrimaryKeys[0]);

    // Assign the Entity
    e.Entity = entity;
}

esPostSelect

Finally, we want to display the actual query that is run each time the esDataSource populates the grid for curiousity reasons. Since we don't call Query.Load() ourselves we need to implement the esPostSelect event and gain access the loaded collection there.

protected void esDataSrc_esPostSelect(object sender, esDataSourceSelectEventArgs e)
{
    this.txtLastQuery.Text = e.Collection.es.Query.es.LastQuery;
}

That's it. Those four methods are all that is required to make our page handle sorting, paging, editing, modifying and save the data.

Don't worry, if you're not using a database that provides built in paging you can still do paging, but you will have to provide custom paging logic, however, the esDataSourceSelectEvenArgs does provide you with the PageNumber and PageSize prefilled in, you just have to fetch the data correctly.

esDataSource "State"

The esDataSource now makes it very easy for you to deal with postbacks. There is a property named "State" which is a Hashtable that you can use to store things in (not just strings either, anything that is serializable). The nice thing is the Hashtable (esDataSource.State) lives accross postbacks. This can be handy when you desire to trap the gridView_SelectedIndexChanged event and pass the id or id(s) to another esDataSource to use, an example would be a FormView or DetailsView control.

protected void gridView_SelectedIndexChanged(object sender, EventArgs e)
{
    this.esDataSrc.State["EmployeeID"] = this.gridView.SelectedDataKey.Value;
}

This is a glimpse of what is coming in this weekends ES 2007 beta. Also, for Windows.Forms we now fully support the INotifyPropertyChanged event which you can choose on the Generated Template's "Advanced Tab".

Upon release of the official EntitySpaces 2007 there will a very complete PDF manual documenting our esDataSource control.

 

The EntitySpaces Team
--

EntitySpaces LLC
Persistence Layer and Business Objects for Microsoft .NET
http://www.entityspaces.net



posted on Saturday, April 28, 2007 10:03:08 AM (Eastern Standard Time, UTC-05:00)  #   
 Thursday, April 19, 2007

Topic:        Rapid DNN Module Development with EntitySpaces
Date:         Thursday, April 19, 6:30 pm
Where:
Microsoft's Las Colinas Office
LC1 Building (Right Tower)
Check in at the security desk

See Dallas DNNUG


Presentation
Learn how to quickly build custom DNN modules using the EntitySpaces architecture.  This method significantly reduces development time by automatically generating the entire module data layer using your database schema. The entity spaces data layer is designed specifically to replace the standard DNN data layer for custom module development.

About EntitySpaces
The EntitySpaces architecture is a persistence layer and business object system for the .NET 2.0 Framework. You can easily have your object model created for you in about 15 minutes.


Speaker Bio

Will Ballard is the founder and president of Dallas New Media, a web design, development, a hosting firm in Grand Prairie.  Dallas New Media has been building websites using DNN since the release of version 2. Our site credits include DNN implementations for the Dallas Stars, Fellowship Technologies, and many large churches and non-profits across the country.

 


EntitySpaces LLC
Persistence Layer and Business Objects for Microsoft .NET
http://www.entityspaces.net/

posted on Wednesday, April 18, 2007 11:22:05 PM (Eastern Standard Time, UTC-05:00)  #   
 Saturday, April 14, 2007

Our next EntitySpaces 2007 beta is at the door. In this beta we will have Medium Trust support and a few nice tweaks concerning finding our assemblies in the Visual Studio references dialogs. The EntitySpaces assemblies can now be installed in the GAC if you so desire.


Assemblies

Before installing EntitySpaces 2007 Beta v0.0415 we want you to uninstall any prior beta and then completely delete your EntitySpaces folder if you can. We strongly advise this so that our new installation layout doesn't intermix with your current files as the folder layout has changed somewhat.

If you accept the default path during installation the EntitySpaces assemblies will be found here:

  • C:\Program Files\EntitySpaces\Redistributables
  • C:\Program Files\EntitySpaces\Redistributables\CE

The installer will also ad a few registry entries that cause the EntitySpaces assemblies to show up when you choose "Ad Reference" in Visual Studio, no need to browse. However, we do not install the EntitySpaces assemblies into the GAC during installation. You can do this of course for your installations as you roll out your application but there is no need, the ability is there of course.

Here is what you will see when you choose Add Reference in a non-Compact Framework application.


You might notice a few new assemblies, we'll get to those in a moment. The nice thing is there is no more browsing for these even though they are not installed in the GAC.

If you are working on a Compact Framework application here is what you will see when you choose "Add Reference" from within Visual Studio.
 


The ability to have your assemblies show up in the reference dialog requires that we make a registry entry. The installer has a checkbox for this that is "on" by default, you can uncheck it if you prefer to browse to your assemblies.

 

Medium Trust

You probably noticed a few new assemblies above, specifically the "loaders". The loaders enumerate through all of your registered connection entries loading the appropriate EntitySpaces Providers to support each connection. There are two forms of the loader. The non-medium trust and the medium trust version. No matter which type of loader you use you will only need to ship the data providers that you actually use. Here are the two loaders.

  • EntitySpaces.LoaderMT = medium trust (no reflection)
  • EntitySpaces.Loader     = (uses reflection, the way ES has always been up until this point)

It is our recommendation that you USE the medium trust loader. It is faster and will work in any environment. Your application will now require a one time call to assign the proper loader at program startup. Here is how we recommend that you do this in your code.

This is how our EntitySpaces Demo Application initializes the Loader (Windows Forms App)
 

namespace EntitySpacesDemo
{
   static class Program
   {
      [STAThread]
      static void Main()
      {
        
esProviderFactory.Factory =
            new EntitySpaces.LoaderMT.esDataProviderFactory();

         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Demo());
      }
   }
}

 

For an ASP.NET the best approach is probably to add a "Global Application Class" or Global.asax file. Here is an example" 

<%@ Application Language="C#" %>

   <script runat="server">

      void Application_Start(object sender, EventArgs e)
      {
         EntitySpaces.Interfaces.esProviderFactory.Factory =
            new EntitySpaces.LoaderMT.esDataProviderFactory();

      }

   </script>

If you are using the configless support (as do our DotNetNuke users) then you want to assign your loader after your connections are registered.

 

The EntitySpaces Team
--
EntitySpaces LLC
Persistence Layer and Business Objects for Microsoft .NET
http://www.entityspaces.net/

posted on Saturday, April 14, 2007 9:38:10 PM (Eastern Standard Time, UTC-05:00)  #   
 Friday, April 13, 2007

MyGeneration 1.2.0.6 now offers support for Microsoft SQL CE and VistaDB 3.0

If you want a 100% free, very nice Code Generator and ORM mapping tool try the new version of MyGeneration 

You can download it here ....

 Get it from CNET Download.com!

MyGeneration, Download.com's #1 Development Tool in the .NET Category.

posted on Friday, April 13, 2007 8:42:48 AM (Eastern Standard Time, UTC-05:00)  #   
 Sunday, April 08, 2007

Our next beta will have, among other things, paging built right into the EntitySpaces architecture. The EntitySpaces dynamic query API has two new properties, StartRow and PageSize. Here is a very simple dynamic query that uses these two new properties. This feature will be built into all providers for databases that support some sort of built in paging. The example below uses the EntitySpaces.SqlClientProvider and only works on SQL 2005 as it takes advantage of the new ROW_NUMBER() function in SQL 2005.  

EmployeesCollection coll = new EmployeesCollection();
coll.Query.Select(coll.Query.LastName, coll.Query.FirstName);
coll.Query.OrderBy(coll.Query.LastName, esOrderByDirection.Ascending);
coll.Query.es.StartRow = 25;
coll.Query.es.PageSize = 10;
if(coll.Query.Load())
{
     Console.WriteLine(coll.Query.es.LastQuery);

Notice that we print the Query's LastQuery property. EntitySpaces always provides the raw text back from a query for debugging purposes. Let's take a look at the SQL generated and executed by EntitySpaces. 

WITH [withStatement] AS
(
    SELECT [LastName],[FirstName] , ROW_NUMBER() OVER( ORDER BY [LastName] ASC) AS ESRN FROM [Northwind].[dbo].[Employees]
)
SELECT [LastName],[FirstName] FROM [withStatement] WHERE ESRN BETWEEN 25 AND 34

Now that we have paging built into the EntitySpaces architecture our new esDataSource control for ASP.NET will expose a PageSize property that you can set. The esDataSource control will then perform all of the paging logic for you automatically. You will be able to build sortable pages that can add, edit, and delete and page through data in a matter of minutes.

 

The EntitySpaces Team
--
EntitySpaces LLC
Persistence Layer and Business Objects for Microsoft .NET
http://www.entityspaces.net/

posted on Sunday, April 08, 2007 7:35:34 PM (Eastern Standard Time, UTC-05:00)  #