General Information

.netTiers is a set of open source code generation templates that simplify the tasks of creating customized Application Tiers for your Microsoft.Net applications in just a few minutes.

More information on NetTiers can also be found in this presentation.

Better yet, check out their documentation page

Set Up

This is just a little expose about my foray into playing with CodeSmith and NetTiers, for purposes of documenting my interactions with each.

Steps Taken To Set Up:

  1. First, downloaded a 30 day trial of CodeSmith from http://www.codesmithtools.com/
  2. Next, downloaded the free .NET Tiers project from http://code.google.com/p/nettiers/
  3. Referenced http://nettiers.com/GettingStarted.ashx in setting this up

Example Queries

Below are some example queries I’ve thrown together from auto-generated NetTiers objects (eg, ExamUsers/ExamFaculty).  You can learn more about the services layer of NetTiers at http://nettiers.com/ComponentLayer.ashx#_Service_API_Example:_3

 

protected void Page_Load(object sender, EventArgs e)
{
//get all
grid.DataSource = getAll();
grid.DataBind();
}

/// <summary>
/// gets all exam requests and returns a tlist (works with Types that implement IEntity)
/// for every table in your database, an entity will be generated for that table
/// </summary>
/// <returns></returns>
protected TList<ExamRequests> getAll()
{
//Get the list
TList<ExamRequests> allRequests = DataRepository.ExamRequestsProvider.GetAll();

//Deepload the list (ie, get related tables)
DataRepository.ExamRequestsProvider.DeepLoad(allRequests, true, DeepLoadType.IncludeChildren, typeof(ExamUsers));

return allRequests;
}

/// <summary>
/// gets a paged list of exam requests, by “hist”
/// </summary>
/// <returns></returns>
protected TList<ExamRequests> getPaged()
{
int count = 0;

TList<ExamRequests> pagedRequests = DataRepository.ExamRequestsProvider.GetPaged(null, “CourseNumber LIKE ‘Hist%'”, “CourseNumber ASC”, 0, 1, out count);

//Deepload the list
DataRepository.ExamRequestsProvider.DeepLoad(pagedRequests, true, DeepLoadType.IncludeChildren, typeof(ExamUsers));

return pagedRequests;

}

/// <summary>
/// get exam requests by id
/// </summary>
/// <returns></returns>
protected ExamRequests getExamsRequestsByID(int id)
{
//Get the list
TList<ExamRequests> requests = DataRepository.ExamRequestsProvider.GetAll();
ExamRequests request = requests.Find(delegate(ExamRequests r) { return r.RequestId == id; });

return request;
}

/// <summary>
/// get request by user id
/// </summary>
/// <param name=”id”></param>
/// <returns></returns>
protected TList<ExamRequests> getRequestsByUserId(int id)
{
//Get the list
return DataRepository.ExamRequestsProvider.GetByUserId(id);
}

/// <summary>
/// get student by id
/// </summary>
/// <param name=”id”></param>
/// <returns></returns>
protected ExamUsers getStudentByUserId(int id)
{
//Get student
return DataRepository.ExamUsersProvider.GetByUserId(id);
}

/// <summary>
/// find requests where course begins with
/// </summary>
/// <param name=”id”></param>
/// <returns></returns>
protected TList<ExamRequests> findRequestsWhereCourseBeginsWith(string course)
{

TList<ExamRequests> requests = DataRepository.ExamRequestsProvider.GetAll();

return requests.FindAllBy(TList<ExamRequests>.FindAllByType.StartsWith, ExamRequestsColumn.CourseNumber, course);

}

/// <summary>
/// find requests where course begins with
/// </summary>
/// <param name=”id”></param>
/// <returns></returns>
protected TList<ExamRequests> filterBySectionNumber(string section)
{

TList<ExamRequests> requests = DataRepository.ExamRequestsProvider.GetAll();

requests.ApplyFilter(delegate(ExamRequests r) { return r.SectionNumber.Contains(section); });

return requests;

}

/// <summary>
/// create a new faculty
/// </summary>
/// <returns></returns>
protected ExamFaculty createFaculty()
{
//Create object
ExamFaculty f = new ExamFaculty();

f.Ename = “fred”;
f.Name = “Fred the Man”;
f.Phone = “1231231234”;
f.Email = “fred.theman@colostate.edu”;
f.Dept = “Dept”;
f.DeptPhone = “1231231234”;

//Save to db
DataRepository.ExamFacultyProvider.Save(f);

return f;
}

/// <summary>
/// edit student
/// </summary>
/// <returns></returns>
protected ExamUsers editStudent(int id)
{
//Get the list
TList<ExamUsers> students = DataRepository.ExamUsersProvider.GetAll();
ExamUsers student = students.Find(delegate(ExamUsers u) { return u.UserId == id; });

//change entity property
student.Name = “Cletus Carruthers”;

//Save
DataRepository.ExamUsersProvider.Save(student);

return student;
}

protected bool deleteFaculty(int id)
{
ExamFacultyService s = new ExamFacultyService();

return s.Delete(id);

}

protected void isValidExamRequest(DateTime date)
{
ExamRequests r = new ExamRequests();

r.UserId = 1;
r.FacultyId = 2;
r.CourseNumber = “blah”;
r.SectionNumber = “blah”;
r.ExamDate = date;

r.Validate();

if (!r.IsValid)
{
litValid.Text = r.Error;
}
else
{
litValid.Text = “This is a valid entity”;
}

}