Thursday, November 20, 2008

Entity Framework, first impressions...

The Entity Relational framework for .Net is a way to relate normalized data from a database store to the objects that represent it within an application. Currently in development I have to create a mapper class that knows the SQL statements to store and retrieve data. This class takes and returns domain or business objects that represent the data and interacts with the database.

The Enitity framework creates an XML mapping file that is used at runtime to represent the database. It allows the developer to hide the specific database by using the Entity provider that will hide the underlying communication with the database. This introduces a new SQL type language that the user of the framework must understand. The following is and example of the eSQL language:

SELECT sp.FirstName, sp.LastName, sp.HireDateFROM AdventureWorks.AdventureWorksDB.SalesPeople AS spWHERE sp.HireDate > @date

The framwork allows the developer to navigate the relationships within data without the more complicated SQL such as INNER JOINS. The following is an example of the inner join syntax and the related eSQL:
SELECT SalesPersonID, FirstName, LastName, HireDateFROM SalesPerson spINNER JOIN Employee e ON sp.SalesPersonID = e.EmployeeIDINNER JOIN Contact c ON e.EmployeeID = c.ContactIDINNER JOIN SalesOrder o ON sp.SalesPersonID = o.SalesPersonIDWHERE e.SalariedFlag = 1 AND o.TotalDue > 200000
Same Query in eSQL:
SELECT VALUE sp
FROM AdventureWorks.AdventureWorksDB.SalesPeople AS sp
WHERE EXISTS(
SELECT VALUE o
FROM NAVIGATE(p, AdventureWorks.SalesPerson_Order) AS o
WHERE o.TotalDue > 200000)
A valuable key word here is the eSQL word "NAVIGATE" which represent inheritance within the entity framework. This is another helpful concept introduced, that foreign key relationships are now represented through inheritance. This will allow me to see a table as the sum of all of its relationships.
Object Representation
Notice in the following snippet that the query is returning a List of type SalesPerson:


Query newSalesPeople = ctx.GetQuery(
"SELECT VALUE sp "

+ "FROM AdventureWorks.AdventureWorksDB.SalesPeople AS sp "
+ "WHERE sp.HireDate > @date",

new QueryParameter("@date", hireDate));

foreach(SalesPerson p in newSalesPeople)
{ Console.WriteLine("{0}\t{1}", p.FirstName, p.LastName);




The entity framework knows how to represent partial class objects of the data that it is representing. This allows qualified objects to be used from the framework and give the developer a way to get rid of database specific code such as connection, command and adapter objects.
My first impression of the framework is apprehensive excitement. I have tried to write code that is database independent and acts as if the database is only a place to persist data. This has been hard and time consuming. The framework allows this to happen more quickly and with less time trying to relate data to the application.
My worries come from dealing with datasets in the past and the numerous issues related to their bulkiness and non-persistence in a web environment. So if the entity framework comes with little to no speed issues and allows for it's use in a disconnected environment then I think it will be useful. This remains to be seen.

Wednesday, January 16, 2008

Create Web Controls

Creating Composite Controls
Introduction
So far in this series you developed controls by emitting raw HTML markup. As you will agree with me this can be tedious at times. Won't it be nice if you can assemble existing controls of ASP.NET and render them as a part of your control? In fact this is a common requirement in many cases and ASP.NET provides an easy way out. In this part you will develop a custom control that assembles existing server controls such as Label and TextBox.
The CompositeControl class
In the examples so far you inherited your custom controls from Control or WebControl base class. If you wish to develop a composite control then you need also need to do the following additional tasks:
Implement INamingContainer interface. This interface ensures that all the child controls of your control get a unique ID. This way your custom control can keep track of its constituent controls.
Call EnsureChildControl() method of Control class to ensure that the child controls are fully loaded before accessing their properties.
One can of course code the above steps manually. However, if you inherit from CompositeControl class instead of Control or WebControl class then these steps are automatically done for you.
Once you create a class that inherits from CompositeControl class then you need to override the CreateChildControls() method of the Control base class (CompositeControl class inherits from WebControl class that in turn inherits from Control class). Inside the CreateChildControls() method you will instantiate the child controls and add them to the Controls collection.
Example - Address control
In order to see how a composite control can be developed you will develop an Address control. The Address control renders various controls for accepting street address, city, state, country and postal code. You can then use the Address control in any web form. The following figure shows how the Address control looks like:
As you can see it renders field captions (Street, City etc.) along with entry controls (TextBox, DropDownList etc.)
To begin your development, create a new web site using Visual Studio. Add App_Code folder to your web site with one class named AddressControl.namespace
BinaryIntellect
{
public class AddressControl : CompositeControl
{
...
}
}

Remember to place the custom control class inside a namespace.
Now declare the following class level variables. These are the child controls of the Address control:
private Label lblStreet;
private Label lblCity;
private Label lblState;
private Label lblCountry;
private Label lblPostalCode;
private TextBox txtStreet;
private TextBox txtCity;
private TextBox txtState;
private DropDownList ddlCountry;
private TextBox txtPostalCode;
private Table tblContainer;
Now add the following public properties that simply set and get the values for Address control. public string Street
{
get
{
return txtStreet.Text;
}
set
{
txtStreet.Text = value;
}
} public string City
{
get
{
return txtCity.Text;
}
set
{
txtCity.Text = value;
}
} public string State
{
get
{
return txtState.Text;
}
set
{
txtState.Text = value;
}
} public string Country
{
get
{
return ddlCountry.SelectedValue;
}
set
{
ListItem li = ddlCountry.
Items.FindByValue(value);
if (li != null)
{
li.Selected=true;
}
}
} public string PostalCode
{
get
{
return txtPostalCode.Text;
}
set
{
txtPostalCode.Text = value;
}
}

For the sake of proper laying out the controls you use table as a container to them. To simply your job of adding controls to the table you develop a helper method as shown below:

private void AddControlsToTable
(Label lbl, Control ctrl)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();cell1.Controls.Add(lbl);
cell2.Controls.Add(ctrl);row.Cells.Add(cell1);
row.Cells.Add(cell2);tblContainer.Rows.Add(row);
}

The AddControlsToTable() method accepts two parameters - a Label that is acting as a field caption and a control to be added. Inside it simply creates a TableRow and TableCell objects and adds the supplied Label and Control to them. The TableRow is finally added to the container table.
Finally, override the CreateChildControls() method as shown below:

protected override void CreateChildControls()
{
tblContainer = new Table();lblStreet = new Label();
lblStreet.Text = "Street :";
lblCity = new Label();
lblCity.Text = "City :";
lblState = new Label();
lblState.Text = "State :";
lblCountry = new Label();
lblCountry.Text = "Country :";
lblPostalCode = new Label();
lblPostalCode.Text = "Postal Code :";txtStreet = new TextBox();
txtStreet.Text = Street;
txtCity = new TextBox();
txtCity.Text = City;
txtState = new TextBox();
txtStreet.Text = State;
ddlCountry = new DropDownList();
ddlCountry.Items.Add("India");
ddlCountry.Items.Add("USA");
ddlCountry.Items.Add("UK");
if (Country != null)
{
ddlCountry.Items.FindByValue(Country).Selected = true;
}
txtPostalCode = new TextBox();
txtPostalCode.Text = PostalCode;AddControlsToTable(lblStreet, txtStreet);
AddControlsToTable(lblCity, txtCity);
AddControlsToTable(lblState, txtState);
AddControlsToTable(lblCountry, ddlCountry);
AddControlsToTable(lblPostalCode, txtPostalCode);Controls.Add(tblContainer);
}

In the CreateChildControls() method you initialize various controls and set their properties. You then call AddControlsToTable() helper method to add the appropriate controls to the table.
That's it. You can now use the Address control on a test web form to see it in action.

Thursday, January 10, 2008

Implement a custom role provider in Asp.Net

Installing the Database using Aspnet_regsql.exe
ASP.NET includes a tool for installing the SQL Server database used by the SQL Server providers, named Aspnet_regsql.exe. The Aspnet_regsql.exe tool is located in the drive:\WINDOWS\Microsoft.NET\Framework\versionNumber folder on your Web server. Aspnet_regsql.exe is used to both create the SQL Server database and add or remove options from an existing database.

You can run Aspnet_regsql.exe without any command line arguments to run a wizard that will walk you through specifying connection information for the computer running SQL Server and installing or removing the database elements for all the supported features. You can also run Aspnet_regsql.exe as a command-line tool to specify database elements for individual features to add or remove.
http://msdn2.microsoft.com/en-us/library/x28wfk74.aspx
http://www.dotnetheaven.com/Uploadfile/amit_agrl/EnablingMembershipProviderforSQLServer200502242006014742AM/EnablingMembershipProviderforSQLServer2005.aspx


type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="LocalSqlServer" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/"
requiresUniqueEmail="false" passwordFormat="Hashed"
maxInvalidPasswordAttempts="5" passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
/>

If you observe the above, you will
notice that the membership provider above "AspNetSqlMembershipProvider" uses a
connection string LocalServer. If you search for LocalServer in the
machine.config file you can see that the same points to the local SQL Express
database.


connectionString="data source=.\SQLEXPRESS;Integrated
Security=SSPI;AttachDBFilename=DataDirectoryaspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
>

Tuesday, November 20, 2007

Virtual Earth

Currently I have been working on Virtual earth for a project at school, and I need real information on how to connect a database to the javascript for VE. I think I have found a great article here:

Virtual Earth tutorial

Sunday, November 18, 2007