3
\$\begingroup\$

I need a bit of help on if I have coded this class correctly.

I want to handle the setting of client cookie/client store.

public class UserSession : IUserSession
{

    private readonly IReadOnlySession _repo;
    public UserSession(IReadOnlySession repo)
    {
        _repo = repo;
    }

    private string _loginId;
    public string LoginID
    {
        get { return HttpContext.Current.User.Identity.Name; }
        set { _loginId = value; }
    }

    private string _companyIdentifier;
    public string CompanyIdentifier
    {
        get { return AuthenticateCookie.GetCompanyIdentifierFromTicket(LoginID); }
        set { _companyIdentifier = value; }
    }

    /// <summary>
    /// when the client store is set, this means the user has been changed
    /// </summary>
    /// <param name="loginId"></param>
    /// <param name="identifier"></param>
    public void SetClientStore(string loginId, string identifier)
    {
        MyProfile = null;
        AuthenticateCookie.AddDetailsToCookie(loginId, identifier);

        LoginID = loginId;
        CompanyIdentifier = identifier;
    }

    public void Logout()
    {
        // we could throw an exception here?
        MyProfile = null;
        HttpContext.Current.User = null;
        FormsAuthentication.SignOut();
        //clear cookie values too?

    }

    private UserProfile _myprofile;
    public UserProfile MyProfile
    {
        get { return SetupProfile(); }
        private set { _myprofile = value; }
    }

    private UserProfile SetupProfile()
    {
        if (_myprofile == null)
        {
            if (string.IsNullOrEmpty(LoginID) || string.IsNullOrEmpty(CompanyIdentifier))
            {
                Logout();
                return null;
            }

            //call repo to get object...

            _myprofile = _repo.All<User>()
                .Where(x => x.Login == LoginID)
                .Join(_repo.All<Profile>().Where(x => x.IsActive), 
                      x => x.UserID, y => y.UserID,
                      (x, y) =>
                          new
                            {
                                x.UserID, y.CompanyID, y.RoleID,
                            })
                .Join(_repo.All<Company>()
                        .Where(x => x.IsActive && x.Identifier == CompanyIdentifier),
                    x => x.CompanyID, y => y.CompanyID,
                    (x, y) =>
                        new
                          {
                              x.UserID, y.CompanyID, x.RoleID, x.IsSurveyor,
                              CompanyFriendlyName = y.Name,
                              CompanyType = y.Type
                          })
                .Join(_repo.All<Role>(), x => x.RoleID, y => y.RoleID,
                      (x, y) =>
                          new
                            {
                                x.UserID, x.CompanyID, y.RoleName, x.CompanyFriendlyName, x.CompanyType
                            })
                .Join(_repo.All<Subscription>(),
                      x => x.CompanyID, y => y.CompanyID,
                      (x, y) =>
                      new UserProfile
                          {
                              UserID = x.UserID,
                              CompanyID = x.CompanyID,
                              Role = x.RoleName.Convert<RoleName>(),
                              CompanyFriendlyName = x.CompanyFriendlyName,
                              LoginID = LoginID,
                              Identifier = CompanyIdentifier,
                              PricePlan = y.Name.Convert<PricePlanType>(),
                              CompanyType = x.CompanyType.Convert<CompanyType>()
                          })
                .SingleOrDefault();

            if (_myprofile == null)
                Logout();
        }

        return _myprofile;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ The Linq in SetupProfile is very confusing to understand as it is. I would really rather see that written in query syntax. Joins and groups using method syntax never make for readable code! \$\endgroup\$ – Mongus Pong Jan 19 '12 at 13:55
1
\$\begingroup\$

It's great that you separated the ASP.NET specific implementation from the interface. Two things to watch:

  1. Don't forget to call Session.Abandon();Session.Clear(); in the Logout method.
  2. SetupProfile contains a lot of code. This seems like business logic to me. Try to extract this code to its own class. An IUserService for instance. Or perhaps explicitly define Query Objects.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.