Monday, 12 March 2007

Validators & Multi-view

Validators don't (seem to) work in MultiView view.

May be due to Ajax v1 & validators see above.

Thursday, 8 March 2007

NHibernate Flush before Query

Was having a problem with following:
  1. Start Session
  2. Read object/record (presentation layer)
  3. Update record/object im memory (presentation layer)
  4. Run query to check changes valid (biz layer).
  5. If 4 ok commit changes inside transaction (biz layer)
  6. End session
Step 4 was identifying a problem and so steps 5 was not completed (i.e. no explicit Commit was called), the problem was that the record changes were still being written to the database.

This is caused by the FlushMode of the session be set to Auto. When set to auto NHibernate will sometimes flush the persistent obejct prior to performing a query.

There are a few solutions to this:
  • Perform updates to object after query (not applicable in this case due to layers)
  • Disconnect object prior to updating (Evict)
  • Change FlushMode to Commit
The solution decided upon was to change FlushMode to Commit and to also clear the session if a transaction fails and rollback is called.

Tuesday, 6 March 2007

Error on DataBind() of DropDownList:, invalid SelectedValue

Getting error:

has a SelectedValue which is invalid because it does not exist in the list of item


The SelectedValue before the databind is set to "" (i.e. blank string)
The error occurs because the data being bound does not include a value of "" (blank string).

To fix it:
  1. Set AppendDataBoundItems=true for control
  2. Clear the list prior to databind; .Items.Clear();
  3. Add a blank item: .Items.Add(new ListItem("", ""));
  4. Call .DataBind();

Monday, 5 March 2007

How to map to a Generic IDictionary

Say you have a standard nHibernate Bag that maps to a IList

C# Definition

public virtual IList<UserGroup> Groups;


nHibernate Mapping

<bag name="UserGroups" table="UserGroup">
<key column="UserID"/>
<one-to-many class="UserGroup"/>
</bag>


You can change this to a IDictionary by changing the IList to a IDictionary in the Business Entity, then change the bag to a map,and adding a index setting to the nHibernate mapping.

So the above would be changed to

C# Definition

public virtual IDictionary<int,UserGroup> Groups;


nHibernate Mapping

<map name="UserGroups" table="UserGroup">
<key column="UserID"/>
<index column="GroupID" type="int"/>
<one-to-many class="UserGroup"/>
</map>


Where of course the index column is the column that you will search on, and the type is the type of the column.

NOTE - The Key Column and the Index column cannot be the same, but then thinking about that it would be pointless because the key value would be the same on all the keys of the dictionary.

NHibernate error loading class

Got the error:

Could not load type '...Page, SalecoreContent', check that type and assembly names are correct


Was caused by an incorrect class="Page" reference in one of the object mapping files.

Friday, 2 March 2007

nHibernate Get or Load?

Session.Get and Session.Load do very similar things, the difference is.

.Get will return null if a record is not found.
.Save will return an exception if a record is not found, and if it can use a proxy, it will return a proxy, so you won't get the exception until you try to actually use the object.

If you are not sure that a record exists then use .Get and check that the object isn't null.

NHibernate:How do I know a field is dirty?

The problem arose that I needed to know if a field had changed just prior to saving:

  1. Presentation layer reads object/record
  2. Presentation layer changes object field value
  3. Presentation layer calls business layer to update the object/record
  4. Business layer needs to know if a certain field has changed to perform extra processing
When the record is read (in step 4) from the database it actually comes from the (session) cache and therefore holds the values of the changed (but not yet persisted) fields.

The solution seems to be either clear the cache or tell the business object that the field has changed. The clear-the-cache solution was not tried, would have been along the lines of Session.Evict(object).