- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Best Practices (also known as "how crappy is my code?")
Joined: 20-Oct-2008
(using LLBLGen Prom2.6 with C# in VS 2008 with SQL 2005)
I have been using llblgen since version 1.6 I think but I am in no way an expert at llblgen or coding in general. I have a snippet of code that I created some time ago and am wondering how much of the code I am doing is either deprecated and or not best practices. For instance, I remember someone mentioning that there is a new way to do sorting and filtering. I found the Isort expression but haven't found an example that I could understand that explains how the predicate factory has changed.
It should be fairly easy to see that I have instructors and students and I am going through both lists to see who is logging in and giving them proper session variables and security levels. Normally I might be pulling from just one view but this example covered a few of the issues all in one set of code so I thought I might get more answers from this snippet.
I should mention that I haven't coded anything in over a year and just picked up this project that will probably take me a few months. Being rusty and not doing this everyday has really hurt me so any help would be greatly appreciated.
protected void btnLogin_Click(object sender, EventArgs e)
{
// Pull up Records and see how they are logged in.
FHLMSDAL.CollectionClasses.StudentsCollection students = new FHLMSDAL.CollectionClasses.StudentsCollection();
SD.LLBLGen.Pro.ORMSupportClasses.IPredicateExpression filter = new SD.LLBLGen.Pro.ORMSupportClasses.PredicateExpression();
filter.Add(FHLMSDAL.FactoryClasses.PredicateFactory.CompareValue(FHLMSDAL.StudentsFieldIndex.Email, SD.LLBLGen.Pro.ORMSupportClasses.ComparisonOperator.Equal, this.tbEmail.Text));
filter.AddWithAnd(FHLMSDAL.FactoryClasses.PredicateFactory.CompareValue(FHLMSDAL.StudentsFieldIndex.Password, SD.LLBLGen.Pro.ORMSupportClasses.ComparisonOperator.Equal, this.tbPassword.Text));
students.GetMulti(filter);
// Pull up Instructor Records
FHLMSDAL.CollectionClasses.InstructorsCollection instructors = new InstructorsCollection();
IPredicateExpression filter2 = new PredicateExpression();
filter2.Add(PredicateFactory.CompareValue(FHLMSDAL.InstructorsFieldIndex.Email, ComparisonOperator.Equal, this.tbEmail.Text));
filter2.AddWithAnd(PredicateFactory.CompareValue(FHLMSDAL.InstructorsFieldIndex.Password, ComparisonOperator.Equal, this.tbPassword.Text));
instructors.GetMulti(filter2);
//Find out whether they are student or instructor and give them correct security levels
if (students.Count.Equals(1))
{
foreach (StudentsEntity student in students)
{
Session["clientID"] = student.Id.ToString();
Session["SecurityLevel"] = "1";
Session["Fullname"] = student.FirstName.ToString() + " " + student.LastName.ToString();
if (this.Request.QueryString.Get("ReturnUrl") == null)
{
Response.Redirect(this.Request.RawUrl.ToString());
}
else
{
Response.Redirect(this.Request.QueryString.Get("ReturnUrl"));
}
}
}
else if (instructors.Count.Equals(1))
{
foreach (InstructorsEntity instructor in instructors)
{
string returnURL = (Request.QueryString.Get("ReturnURL"));
Session["crewID"] = instructor.Id.ToString();
Session["SecurityLevel"] = "2";
Session["Fullname"] = instructor.FirstName.ToString() + " " + instructor.LastName.ToString();
if (this.Request.QueryString.Get("ReturnUrl") == null)
{
Response.Redirect(this.Request.RawUrl.ToString());
}
else
{
Response.Redirect(this.Request.QueryString.Get("ReturnUrl"));
}
}
}
else
{
this.lblError.Text = "Incorrect Username/Password";
//this.ImgLeftBar.Height = 109;
//this.ImgRightBar.Height = 109;
this.lblError.Visible = true;
}
}
}
Hi salterclan, welcome back. Here are some points:
- Build a filter now is easier (ref...):
StudentsCollection students = new StudentsCollection();
IPredicateExpression filter = new PredicateExpression();
filter.Add(StudentsFiels.Email == this.tbEmail.Text);
filter.AddWithAnd(StudentsFields.Password == this.tbPassword.Text);
students.GetMulti(filter);
- You can avoid the second fetch, in case that the email comes from a student:
// fetch students...
if (students.Count > 0)
{
// set the session variables
}
else
{
// fetch instructors...
if (intructors.Cojnt > 0)
{
// set the session variables
}
}
- Seeing your entities. Seems that there exists a change to normalize a little bit: User UserId Email Password SecurityLevel
Student UserId FullName ...
Instructor UserId FullName ...
Then you only have to do one fetch and the objects are conceptually separated. They also are candidates to hierarchy.
Maybe you would like to take a look at the documentation to see the whole new features.
Joined: 20-Oct-2008
Otis wrote:
I do wonder what v1.6 is though
![]()
Oh whatever version it was. Maybe 1.02. Something in the ones that was back in 2004 If I remember correctly. Now add something insightful besides mocking my bad memory
Joined: 20-Oct-2008
daelmo wrote:
- Build a filter now is easier (ref...):
StudentsCollection students = new StudentsCollection(); IPredicateExpression filter = new PredicateExpression(); filter.Add(StudentsFiels.Email == this.tbEmail.Text); filter.AddWithAnd(StudentsFields.Password == this.tbPassword.Text); students.GetMulti(filter);
I read the page you sent me and yes it seems much easier to implement. However, I don't see a StudentsFields as available for me. where would I find that? I assume that I am missing a "using" statement or something simple.
daelmo wrote:
- You can avoid the second fetch, in case that the email comes from a student:
// fetch students... if (students.Count > 0) { // set the session variables } else { // fetch instructors... if (intructors.Cojnt > 0) { // set the session variables } }
That would be great to skip the "for each". However, I am not positive what would be the best way to set these session variables as I am calling a collection up to this point. Are you thinking of calling students[0].Id.toString(); so that you get the first record or are you thinking of something different?
thanks for your quick responses.
salterclan wrote:
daelmo wrote:
- Build a filter now is easier (ref...):
StudentsCollection students = new StudentsCollection(); IPredicateExpression filter = new PredicateExpression(); filter.Add(StudentsFiels.Email == this.tbEmail.Text); filter.AddWithAnd(StudentsFields.Password == this.tbPassword.Text); students.GetMulti(filter);
I read the page you sent me and yes it seems much easier to implement. However, I don't see a StudentsFields as available for me. where would I find that? I assume that I am missing a "using" statement or something simple.
Yep. It must be FHLMSDAL.HelperClasses
salterclan wrote:
daelmo wrote:
- You can avoid the second fetch, in case that the email comes from a student:
// fetch students... if (students.Count > 0) { // set the session variables } else { // fetch instructors... if (intructors.Cojnt > 0) { // set the session variables } }
That would be great to skip the "for each". However, I am not positive what would be the best way to set these session variables as I am calling a collection up to this point. Are you thinking of calling students[0].Id.toString(); so that you get the first record or are you thinking of something different?
thanks for your quick responses.
Well, as you are fetching a collection, you have to access that way (students[x]...). If the email field would a PK or have a UC (unique constraint) you should be able to fetch only one entity. I don't know if that is your case.