1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
.Tests
.Model
.CompositeModel
18 using System
.Collections
;
19 using Castle
.ActiveRecord
.Framework
;
22 [ActiveRecord("Groups")]
23 public class Group
: ActiveRecordBase
27 private IList _agents
;
28 private IList _groups
;
29 private bool onSaveCalled
, onUpdateCalled
, onDeleteCalled
, onLoadCalled
;
33 _agents
= new ArrayList();
34 _groups
= new ArrayList();
37 [PrimaryKey(PrimaryKeyType
.Native
)]
48 set { _name = value; }
51 [HasAndBelongsToMany(typeof(Agent
), Table
= "GroupAgents", CompositeKeyColumnRefs
= new String
[] {"OrgId", "Name"}
,
52 ColumnKey
= "GroupId", Lazy
= true, Inverse
=true, Cascade
= ManyRelationCascadeEnum
.SaveUpdate
)]
55 get { return _agents; }
56 set { _agents = value; }
59 [HasAndBelongsToMany(typeof(Group
), Table
= "GroupOrgs", ColumnRef
= "GroupId", ColumnKey
= "OrgId", Lazy
= true,
60 Inverse
=true, Cascade
= ManyRelationCascadeEnum
.SaveUpdate
)]
63 get { return _groups; }
64 set { _groups = value; }
67 public static void DeleteAll()
69 ActiveRecordMediator
.DeleteAll(typeof(Group
));
72 public static void DeleteAll(string where
)
74 ActiveRecordMediator
.DeleteAll(typeof(Group
), where
);
77 public static Group
[] FindAll()
79 return (Group
[]) ActiveRecordMediator
.FindAll(typeof(Group
));
82 public static Group
Find(int id
)
84 return (Group
) ActiveRecordMediator
.FindByPrimaryKey(typeof(Group
), id
);
87 public static int FetchCount()
89 return Count(typeof(Group
));
92 public static int FetchCount(string filter
, params object[] args
)
94 return Count(typeof(Group
), filter
, args
);
97 public static bool Exists()
99 return Exists(typeof(Group
));
102 public static bool Exists(string filter
, params object[] args
)
104 return Exists(typeof(Group
), filter
, args
);
108 /// Lifecycle method invoked during Save of the entity
110 protected override void OnSave()
116 /// Lifecycle method invoked during Update of the entity
118 protected override void OnUpdate()
120 onUpdateCalled
= true;
124 /// Lifecycle method invoked during Delete of the entity
126 protected override void OnDelete()
128 onDeleteCalled
= true;
132 /// Lifecycle method invoked during Load of the entity
134 protected override void OnLoad(object id
)
139 public bool OnSaveCalled
141 get { return onSaveCalled; }
144 public bool OnUpdateCalled
146 get { return onUpdateCalled; }
149 public bool OnDeleteCalled
151 get { return onDeleteCalled; }
154 public bool OnLoadCalled
156 get { return onLoadCalled; }
159 public ISession CurrentSession
161 get { return (ISession) ActiveRecordMediator.Execute(typeof(Group), new NHibernateDelegate(GrabSession), this); }
164 private object GrabSession(ISession session
, object instance
)
169 public void CustomAction()
171 ActiveRecordMediator
.Execute(typeof(Group
), new NHibernateDelegate(MyCustomMethod
), this);
174 private object MyCustomMethod(ISession session
, object blogInstance
)
176 session
.Delete(blogInstance
);
182 internal static ISessionFactoryHolder Holder
184 get { return ActiveRecordMediator.GetSessionFactoryHolder(); }