From:http://www.mertner.com/confluence/display/Gentle/3+-+Using+Gentle.NET
1 保存
User user = new User( 42, "Ford Prefect" );
Broker.Insert( user ); // save the user to the database
Key key = new Key( typeof(User), true, "Id", 42 ); // create a key with a single selection criteria value
user = Broker.RetrieveInstance( typeof(User), key ) as User; // load the specified user from the database
Broker.Insert( user ); // save the user to the database
Key key = new Key( typeof(User), true, "Id", 42 ); // create a key with a single selection criteria value
user = Broker.RetrieveInstance( typeof(User), key ) as User; // load the specified user from the database
如果继承自Persistent,可以这样
User ford = new User( "Ford Prefect" );
ford.Persist(); // save the new user and assign an id value
User prefect = User.Retrieve( ford.Id ); // retrieve the existing user
ford.Persist(); // save the new user and assign an id value
User prefect = User.Retrieve( ford.Id ); // retrieve the existing user
2 query
static public IList ListByNameStartsWith( string partialName )
{
SqlBuilder sb = new SqlBuilder( StatementType.Select, typeof(User) );
// note: the partialName parameter must also contain the %'s for the LIKE query!
sb.AddConstraint( Operator.Like, "Name", partialName );
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement( true );
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection( typeof(User), stmt.Execute() );
}
{
SqlBuilder sb = new SqlBuilder( StatementType.Select, typeof(User) );
// note: the partialName parameter must also contain the %'s for the LIKE query!
sb.AddConstraint( Operator.Like, "Name", partialName );
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement( true );
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection( typeof(User), stmt.Execute() );
}
如果返回ArrayList需要显式转换..
方法:Broker.Execute,ObjectFactory.GetCollection ,都是在transaction下进行
分页:
Basically you can use SetLimit and SetOffset on SqlBuilder
SqlResult has methods to get Next and Previous page, adjusting the limit/offset values as necessary
3 GentleList
The supported types are StandAlone, OneToMany and ManyToMany
The list type is decided implicitly from the constructor being called
If you add an object already in the list, GentleList returns the index of the existing entry (and does not add an object to the list).
3.1 StandAlone
Note that persisted objects will not be persisted again when added, even if updates have been made to them.
GentleList list = new GentleList( typeof(User) );
3.2 OneToMany
This mode is used when the objects being managed have a parent-child relationship to another object (the parent).
The parent itself must have been persisted before the GentleList is created
GentleList list = new GentleList( typeof(User), parentInstance );
3.3 ManyToMany
GentleList list = new GentleList( typeof(User), role, typeof(UserRole) );
4 Relations
数据库设计要规范
5
NullOption.Min for DateTime (which translates into DateTime.MinValue)
Can be applied to DateTime, decimal or Guid properties
6
use the TypedArrayList in companionship with the TypedArrayItemBase to customize display and behavior of your objects when data binding.
TypedArrayList is only for WinForms only(not sure)
ObjectView solves the visibility and formatting issues(for control like datagrid)
Joined result sets are most easily obtained by (a) creating a database view and mapping a class to it or (b) by creating a wrapper class that exposes various bits of the contained objects.
GentleList有可能具有类似功能.
[TableName("V_Customer")]
public class Customer : Persistent
{
[TableColumn("CustomerID", NotNull=true), PrimaryKey()]
[Caption("Customer Id"), ReadOnly(true)]
protected string mCustomerid = String.Empty;
[TableColumn("CompanyName", NotNull=true)]
[Caption("CompanyName Id"), AllowSort(false)]
protected string mCompanyname = String.Empty;
[TableColumn("Address")]
[Caption("Address"), Visible(false)]
protected string mAddress = String.Empty;
}
TypedArrayList customerList = new TypedArrayList(typeof(Customer));
SqlBuilder sql = new SqlBuilder(StatementType.Select, typeof(Customer));
SqlStatement stmt = sql.GetStatement(StatementType.Select, typeof(Customer), true);
SqlResult result = Broker.Execute(stmt);
customerList = (TypedArrayList)ObjectFactory.GetCollection(typeof(Customer), result, customerList);
public class Customer : Persistent
{
[TableColumn("CustomerID", NotNull=true), PrimaryKey()]
[Caption("Customer Id"), ReadOnly(true)]
protected string mCustomerid = String.Empty;
[TableColumn("CompanyName", NotNull=true)]
[Caption("CompanyName Id"), AllowSort(false)]
protected string mCompanyname = String.Empty;
[TableColumn("Address")]
[Caption("Address"), Visible(false)]
protected string mAddress = String.Empty;
}
TypedArrayList customerList = new TypedArrayList(typeof(Customer));
SqlBuilder sql = new SqlBuilder(StatementType.Select, typeof(Customer));
SqlStatement stmt = sql.GetStatement(StatementType.Select, typeof(Customer), true);
SqlResult result = Broker.Execute(stmt);
customerList = (TypedArrayList)ObjectFactory.GetCollection(typeof(Customer), result, customerList);
7 -- Concurrency(to do check)
8 Cache
Gentle automatically ensures that the default provider is never garbage collected
8.1 Statement Caching
Once the insert query has been created (using the SqlBuilder class) Gentle inserts the statement into the cache.
8.2 Object Caching
需要注意transaction回滚时内存数据与Cache数据不同的问题,需要手动更新cache
8.3 Query Skipping
能绕过数据库进而提高性能,作者推荐将其设为true..
8.4Object Uniqing
Usually you would use Thread scope for ASP.NET applications