Example


From: http://www.mertner.com/confluence/display/Gentle/3+-+Using+Gentle.NET

1 保存

None.gif User user  =   new  User(  42 " Ford Prefect "  );
None.gifBroker.Insert( user ); 
//  save the user to the database
None.gif
Key key  =   new  Key(  typeof (User),  true " Id " 42  );  //  create a key with a single selection criteria value
None.gif
user  =  Broker.RetrieveInstance(  typeof (User), key )  as  User;  //  load the specified user from the database

如果继承自Persistent,可以这样
None.gif User ford  =   new  User(  " Ford Prefect "  );
None.gifford.Persist(); 
//  save the new user and assign an id value
None.gif
User prefect  =  User.Retrieve( ford.Id );  //  retrieve the existing user

2 query

None.gif static   public  IList ListByNameStartsWith(  string  partialName )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    SqlBuilder sb 
= new SqlBuilder( StatementType.Select, typeof(User) );
InBlock.gif    
InBlock.gif    
// note: the partialName parameter must also contain the %'s for the LIKE query!
InBlock.gif
    sb.AddConstraint( Operator.Like, "Name", partialName );
InBlock.gif    
InBlock.gif    
// passing true indicates that we'd like a list of elements, i.e. that no primary key
InBlock.gif    
// constraints from the type being retrieved should be added to the statement
InBlock.gif
    SqlStatement stmt = sb.GetStatement( true );
InBlock.gif    
InBlock.gif    
// execute the statement/query and create a collection of User instances from the result set
InBlock.gif
    return ObjectFactory.GetCollection( typeof(User), stmt.Execute() );
ExpandedBlockEnd.gif}

如果返回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.
None.gif 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
None.gif GentleList list  =   new  GentleList(  typeof (User), parentInstance );

3.3 ManyToMany
None.gif 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有可能具有类似功能.

None.gif [TableName( " V_Customer " )]
None.gif
public   class  Customer : Persistent
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  [TableColumn(
"CustomerID", NotNull=true), PrimaryKey()]
InBlock.gif  [Caption(
"Customer Id"), ReadOnly(true)]
InBlock.gif  
protected string mCustomerid   = String.Empty;
InBlock.gif  [TableColumn(
"CompanyName", NotNull=true)]
InBlock.gif  [Caption(
"CompanyName Id"), AllowSort(false)]
InBlock.gif  
protected string mCompanyname  = String.Empty;
InBlock.gif  [TableColumn(
"Address")]
InBlock.gif  [Caption(
"Address"), Visible(false)]
InBlock.gif  
protected string mAddress      = String.Empty;
InBlock.gif
InBlock.gif  dot.gif
ExpandedBlockEnd.gif}

None.gif
None.gifTypedArrayList customerList 
=   new  TypedArrayList( typeof (Customer));
None.gifSqlBuilder sql 
=   new  SqlBuilder(StatementType.Select,  typeof (Customer));
None.gifSqlStatement stmt 
=  sql.GetStatement(StatementType.Select,  typeof (Customer),  true );
None.gifSqlResult result 
=  Broker.Execute(stmt);
None.gifcustomerList 
=  (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


转载于:https://www.cnblogs.com/day/archive/2006/04/07/369526.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值