Oldest known version of this page was edited on 2006-06-20 04:39:35 by AqD []
Page view:
HomePage -
Tutorials -
First Form
First Form
Step 1 -- The Model Class
A plain and simple user profile:
public class User {
private string _name;
private int _age;
public string Name {
set { _name = value; }
get { return _name; }
}
public string Age {
set { _age = value; }
get { return _age; }
}
}
Step 2 -- Mapping to Database
Map your class to database table(s) using
NHibernate:
using NHMA = NHibernate.Mapping.Attributes;
// Map your class to the [User] table
[NHMA.Class]
// The class AqDWeb.Utils.PersistentEntity includes basic entity properties such as PersistenceID
// (GUID, primary key) and a version field for nhibernate to track state.
public class User: AqDWeb.Utils.PersistentEntity {
private string _name;
private int _age;
// Map to [User].[Name] column, type NVARCHAR(200)
[NHMA.Property(NotNull=true, Length=200)]
public string Name {
set { _name = value; }
get { return _name; }
}
// Map to [User].[Age] column, type INT
[NHMA.Property(NotNull=true)]
public int Age {
set { _age = value; }
get { return _age; }
}
}
Now you can either create the schema by yourself, or just open
http://localhost/YourApp/NHibernateInfo.axd∞, paste & execute the schema creation SQL (at the bottom). You will also find the generated mapping xml there.
PS: Don't worry about table/column name conflict with reversed words in your db - all of them are quoted by default.
Step 3 -- Write your code-behind page class
A page that takes one query parameter "id" to update existing user profile, or create a new user if there is no id given:
public class MyPage: AqDWeb.Base.ManagedPage {
// UserID will be set to the ID in request query string (and the type is converted to Guid).
// If there isn't any ID given, its value will remain to be Guid.Empty or whatever you set below.
[QueryParameter("id")]
public Guid UserID = Guid.Empty;
public User User = null;
// OnDataBinding is the place where you prepare the model objects for the view layer to use.
// It could be called more than once - I'll explain the details later.
protected override void OnDataBinding(EventArgs e) {
if (UserID != Guid.Empty)
User = AqDWeb.Services.PersistentEntityBroker<User>.FindByID(UserID);
else
}
// This method will be invoked when "save" button is pressed. Its return value is an URL that this
// request will be redirected to, or null to stay in the same page.
public virtual string SaveUser() {
AqDWeb.Services.PersistentEntityBroker<User>.Persist(User);
return null;
}
}
Step 4 -- Write your aspx page!
Now the easist part comes :)
<%@ Page Inherits="MyPage" %>
<html>
<head></head>
<body>
<h1>You're editing user profile for ${User.Name}:</h1>
<form runat="server">
ID: <span runat="server" bind-innerText="'[' + User.ID + ']'"/>
Name: <input runat="server" type="text" bind-value="User.Name"/><br/>
Age: <input runat="server" type="text" bind-value="User.Age"/><br/>
<input runat="server" type="submit" value="Save" ext-serverClick="SaveUser()"/>
</form>
</body>
</html>
In real world the binding is much more complex than that and you will need to consider a bit more things (we haven't mentioned loops and state management yet). But for a simple form, that's all!
- Note that you can use C#-like syntax inside the bind-whatever attributes - it's OGNL, visit http://www.ognl.org/2.6.9/Documentation/html/LanguageGuide/index.html∞ if you want to learn more.
- The ${something} syntax, new in 0.9.1.0, it's for all literal cntrols. However there is no way to escape } yet ;)