0

I have 2 classes that share some properties

public class SportsUser
{
    public string errorCode { get; set; }
    //the sportsuser and SportsAdminUser is from a different database hence the long and int difference
    public long userId { get; set; }      
    ...        
}

public class SportsAdminUser
{
    public int userId { get; set; }
    public string errorCode { get; set; }
    ...
}

I also have an web api call that can be either passed a sessionToken or a username and password which determines which class above I need to instantiate.

...
UserHandler userHandler = new UserHandler();                    
string errorCode = "";
int adminId = 0;
if (!string.IsNullOrEmpty(sessionToken))
{
      SportsUser sportsUser = new SportsUser();
      sportsUser = userHandler.CheckUpdateUserSessionToken(sessionToken);
      adminId = Convert.ToInt32(sportsUser.userId);
      errorCode = sportsUser.errorCode;
}
else
{
      SportsAdminUser sportsAdminUser = new sportsAdminUser();
      sportsAdminUser = userHandler.LoginUser(username, password);
      adminId = sportsAdminUser.userId;
      errorCode = sportsAdminUser.errorCode;
}

if (errorCode != "SESSION_INVALID" && errorCode != "INVALID_CREDENTIALS" && errorCode != "ACCOUNT_LOCKED")
{
Tuple<int, List<PunterTransaction>> resultTupe = businessLogic.GetCustomerTransactions(customerId, transactionsFromDateTimeTicks, transactionsToDateTimeTicks, adminId);
...
                    

So from above i would like to improve the way this works without having to keep instantiating different classes that share properties.

One issue i may have is on my data layer i user dapper to retrieve and map my sportsuser classes so if I declare a base class to contain my shared values will dapper be able to auto map still without having to change code in my DAL.

Also declaring an interface i'm not sure how to implement this with regards to having a single class I could use. What would then be the best solution Generics, Interfaces, DI? I did look at doing something with dynamics types as below but would need to fix the long/int type but not sure if dynamic is best/good practice.

UserHandler userHandler = new UserHandler();
dynamic tmp = new System.Dynamic.ExpandoObject();
if (!string.IsNullOrEmpty(sessionToken))
{
     tmp = userHandler.CheckUpdateUserSessionToken(sessionToken);
}
else
{
     tmp = userHandler.LoginUser(username, password);
}

if (tmp.errorCode != "SESSION_INVALID" && tmp.errorCode != "INVALID_CREDENTIALS" && tmp.errorCode != "ACCOUNT_LOCKED")
{
    Tuple<int, List<CustomerTransaction>> resultTupe = businessLogic.GetCustomerTransactions(customerId, transactionsFromDateTimeTicks, transactionsToDateTimeTicks, tmp.bookmakerId);
                    
3
  • 1
    This question is jumping from one concern to another as if it's all one big ball of twine (or, more appropriately, spaghetti). I strongly suggest you separate your concerns better. Both in code and in communication when tackling specific issues that should be situated in one of the concerns.
    – Flater
    Jan 28, 2021 at 14:48
  • Thanks @Flater. Any more insights?
    – pinman
    Jan 28, 2021 at 15:10
  • 1
    It has been two weeks and no attempt has been made to improve the question, I have opted to vote for closure due to lack of focus.
    – Flater
    Feb 11, 2021 at 13:07

0

Your Answer

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

Browse other questions tagged or ask your own question.