The final class, LoginDataControl.cs
, provides CRUD methods for data retrieval, storage, and removal. The session variable is an NHibernate session (you can find out more about session management in Chapter 5, The Session Procession).
This class defines a few simple CRUD methods that are used quite often when manipulating database records. The GetById(int id)
function allows the user to pass in an integer and retrieve the record with that ID. The GetAll()
method returns all of the records in a given table. GetCountOfAll()
returns a count of the records in the table, while allowing controls that handle pagination and record navigation to function.
public class LoginDataControl { public LoginDataControl() { } ISession session; public Login GetById(int id) { Login retVal = session.Get<Login>(id); return retVal; } public IList<Login> GetAll() { ICriteria criteria = session.CreateCriteria<Login>(); IList<Login> retVal = criteria.List<Login>(); return retVal; } public int GetCountOfAll() { return GetAll().Count; } }