1

So I have redone some of the code to the previous question, however the same underlying issue remains. What i am trying to do is this: When my lightswitch application launch through Application.cs i am using a method Application_LoggedIn() to call a function in my Common Project to get all the location a user has access to (this data is stored in a table).

Client Project - Application.cs

  partial void Application_LoggedIn()
    {
        Secure.Membership aa = new Membership();
        aa.membership();
    }

Common Project - After calling the method, i then run my queries in the membership() function to get all the location a user has access to. They are stored in three list: estateList, deptList, groupList;

public void membership() {

    List<int> estateList = new List<int>();
    List<int> deptList = new List<int>();
    List<int> groupList = new List<int>();

    var dws = Application.Current.CreateDataWorkspace().sspData;
    String uName = Application.Current.User.Identity.Name;
    try
    {
        var qryUser = (from a in dws.aspnet_Users
                       where a.UserName == uName
                       select a).Execute().Single();

        int membershipId = qryUser.Payroll_MembershipGroup.Id;
        var qryOrgMember = (from a in dws.PayrollOrg_MembershipGroups
                            where a.Payroll_MembershipGroup.Id == membershipId
                            select a).Execute().ToList();

        foreach (var x in qryOrgMember)
        {

            if (!estateList.Contains(x.Payroll_Organisation.Estate1.ID))
            {
                estateList.Add(x.Payroll_Organisation.Estate1.ID);
            }
            if (!deptList.Contains(x.Payroll_Organisation.Estate1.ID))
            {
                deptList.Add(x.Payroll_Organisation.Department1.ID);
            }
            if (!groupList.Contains(x.Payroll_Organisation.Estate1.ID))
            {
                groupList.Add(x.Payroll_Organisation.Payroll_Group1.ID);
            }
        }
    }
    catch (Exception e)
    {

        Debug.WriteLine("***************" + e.InnerException);
    }

}

What i would like after doing this is to be able to access the 3 lists (estate,dept,group) from inside my server project so i can use the list to perform filters on my entities as shown below.

partial void Employee_Employs_Filter(ref Expression<Func<Employee_Employ, bool>> filter)
{ 
   filter = e => estateList.Contains(e.Estate1.ID) && deptList.Contains(e.Substantive_Department) && groupList.Contains(e.Payroll_Group1.ID);
}

This is what i want to do and i am having problem. Any suggestion would be welcomed. What i tried to do is to create a static class in my Common Project and set the values however as one user rightly pointed out, i cannot do that as it will be null when i try to call it from inside my Server Project. Please help me out here, this is a big issue i am having with this application and is the only remaining part. Thanks in advance.

************ Filter code on server side* @Duran this is what i had on the server side.

public List<User> membership() {

    List<User> aList = new List<User>();
    User aUser = new aUser();


    var dws = Application.Current.CreateDataWorkspace().sspData;
    String uName = Application.Current.User.Identity.Name;
    try
    {
        var qryUser = (from a in dws.aspnet_Users
                       where a.UserName == uName
                       select a).Execute().Single();

        int membershipId = qryUser.Payroll_MembershipGroup.Id;
        var qryOrgMember = (from a in dws.PayrollOrg_MembershipGroups
                            where a.Payroll_MembershipGroup.Id == membershipId
                            select a).Execute().ToList();

        foreach (var x in qryOrgMember)
        {

            aUser.estateId = x.estateId
            auser.deptId = x.deptId;
            aUser.groupId = x.groupId;

            aList.add(aUser);
        }
    }
    catch (Exception e)
    {

        Debug.WriteLine("***************" + e.InnerException);
    }

    return aList;

}

public class User{

    public estateId{set;get:}
    public deptId{set;get:}
    public groupId{set;get:}

}

Now in my filter i would call the method membership() that returns a list<User>

partial void Employee_Employs_Filter(ref Expression<Func<Employee_Employ, bool>> filter)
{ 
    var x = membership();
    estId[int] = new [x.count()];
    deptId[int] = new [x.count()];
    groupId[int] = new [x.count()];

    for (int i=0; i<x.count();i++){
         estId[i] = x.elementAt(i).estateId;
         deptId[i] = x.elementAt(i).deptId;
         groupId[i] = x.elementAt(i).groupId;

    }

   filter = e => estId.Contains(e.Estate1.ID) && deptId.Contains(e.Substantive_Department) && groupId.Contains(e.Payroll_Group1.ID);
}

I have to put a filter similar to the one above on ALL my tables, thats when i get the stackoverflow error. It works when its on ONE table alone but not when it is on all the tables. What am i doing wrong?

Maverick1415
  • 113
  • 1
  • 12
  • How are you expecting the information to get from the client to the server? – Jon Skeet Jan 16 '14 at 18:42
  • Through the Common Project. The "global" variable i created is in the common. – Maverick1415 Jan 16 '14 at 18:44
  • Im new to lightswitch, dont know if what i am doing is wrong. – Maverick1415 Jan 16 '14 at 18:44
  • 2
    Static variables don't work like that. They're shared within a process, but they're not automatically shipped across the network. – Jon Skeet Jan 16 '14 at 18:46
  • Okay thanks, any suggestions on how i should go about getting what i want. Basically get a list of rights based on user id when he/she logs in... and then being able to access that list in my server project to filter my data. any ideas? Thanks for the clarification – Maverick1415 Jan 16 '14 at 18:49
  • 1
    Well I wouldn't start off with the client performing the enforcement at all - it should be enforced on the server, generally. The client should tell the server who the user is (along with authentication) and then the server should determine the rights and only supply appropriate information. – Jon Skeet Jan 16 '14 at 18:51
  • The client is not enforcing, all the client is doing is getting a list based on the authenticated user. the server would be doing the enforcing..i agree. – Maverick1415 Jan 16 '14 at 18:53
  • I restructure the question, hopefully you can help me out a little more. @JonSkeet – Maverick1415 Jan 17 '14 at 19:14

1 Answers1

1

Even though the code in the Common project is "shared" between the Client & Server projects, any variables created there have their own separate instances, one in the Client project, & one in the Server project.

So you're not really accessing a single shared variable at all, which is why when you set a value on one tier, the value on the other tier doesn't reflect that change in value, because they're actually two distinct instances of the variable.

Yann Duran
  • 3,842
  • 1
  • 24
  • 24
  • Noted. Any idea on how i can go about getting this task done? How would you approach it. I need a list of all locations (a db field in my tables) that the user has and then use that list in my filter. – Maverick1415 Jan 17 '14 at 11:52
  • I restructure the question, hopefully you can help me out a little more. @Yann Duran – Maverick1415 Jan 17 '14 at 19:13
  • If this is all for use in a filter method, why are you not simply executing the function on just the server? Am I missing something that requires the client to be involved? – Yann Duran Jan 18 '14 at 10:18
  • @Dinesh Persaud - why have you so drastically edited user3037006's question? You've changed it into an almost entirely different question, & you've removed what the OP had already tried, in his attempt to solve his problem. If you think you understand the scenario that well, how about attempting to answer the question with your own answer, making your suggested changes there, instead of morphing his question into what you think he should have asked? I don't believe that the "edit question" feature is meant to be used to change that much of a question's text. – Yann Duran Jan 18 '14 at 10:35
  • i tried implementing it on the server side but creating a method to get the list and then calling the method on each table filter, but what happen is that i got a stackoverflow error. If a table has values for other tables that also has to filter, it keep filtering over and over and over.. unless that was the wrong approach .. and you have a better one.. please help.. – Maverick1415 Jan 18 '14 at 22:20
  • If you have a query, which is based on a query, which is also based on another query, and each query has its own Filter method defined for it, each of those query's Filter methods will be chained one after the other. This is expected & desired behavior. For there to be a stack overflow error, one of your filters must be being called recursively, or one query is calling a second query which then calls the first. Best to add your filter code to your question, to see what you're possibly doing wrong. Otherwise I can only guess. – Yann Duran Jan 19 '14 at 09:49
  • I added the code u requested. thats how i implemented the code on the server side. please take a look and hopefully you can help me out. this issue has been bothering me all week. – Maverick1415 Jan 19 '14 at 15:22
  • where are you my friend? – Maverick1415 Jan 21 '14 at 11:49
  • Sorry, *really* under the pump at work. Haven't had a chance to look atm. I will as soon as I get a chance. – Yann Duran Jan 22 '14 at 11:14