Bog inheritance class, how to make this maintainable code

I want to create maintainable code, but this inheritance situation is causing me problems.

The problem is with my second database helper class named InitUserExtension .

Since UserExtension inherits from the user, I have to make sure that I mirror any changes in my InitUser helper for InitUserExtension.

I really don't like it as its error prone, what's the solution?

My class definitions:

public class User
{
    public string Name {get; set; }
    public string Age { get; set; }
}

public class UserExtension : User
{
    public string Lastname {get; set; }
        public string Password {get; set; }

}

      

My database helpers:

public static SqlDataReader InitUser(SqlDataReader dr)
{
      User user = new User();

      user.Name = Convert.ToString(dr["name"]);
      user.Age ...
}


public static SqlDataReader InitUserExtension(SqlDataReader dr)
{
      UserExtension user = new UserExtension();


      // note: mirror attributes from User
      user.Name = Convert.ToString(dr["name"]);
      user.Age ...



      user.Lastname = Convert.ToString(dr["lastname"]);
      user.Password = ....;
}

      

+1


source to share


4 answers


How to move name handling etc. into a method (accepting user or user T: user) and call this from both?

private static void InitUser(User user, SqlDataReader dr)
{ // could also use an interface here, or generics with T : User
  user.Name = Convert.ToString(dr["name"]);
  user.Age ...
}


public static User InitUser(SqlDataReader dr)
{
    User user = new User();
    InitUser(user, dr);
    return user;
}

public static UserExtension InitUserExtension(SqlDataReader dr)
{
  UserExtension user = new UserExtension();
  InitUser(user, dr);
  user.Lastname = Convert.ToString(dr["lastname"]);
  user.Password = ....;
  return user;
}

      



Alternatively, you can reduce the number of lines but increase the complexity using generics:

private static T InitUserCore<T>(SqlDataReader dr) where T : User, new()
{
    T user = new T();
    // ...
    return user;
}
public static User InitUser(SqlDataReader dr)
{
    return InitUserCore<User>(dr);
}
public static UserExtension InitUserExtension(SqlDataReader dr)
{
    UserExtension user = InitUserCore<UserExtension>(dr);
    // ...
    return user;
}

      

+2


source


Why don't you call InitUser

from the method InitUserExtension

. Let base initialization handle the properties of the base class and let the extended initializer handle the properties of the extension class.



+1


source


If you prefer the helpers do the same with the helpers

public class InitUser
{
    public InitUser(SqlDataReader dr, User u) { }
}

public class InitUserExtension : InitUser
{
    public InitUserExtension(SqlDataReader dr , UserExtension u) : base(dr, u) { }
}

      

+1


source


I'm not sure why you didn't put the assignment code in the classes themselves, but it might have something to do with how you get the information to store it ...

My solution would be to rewrite it something like this:

public class User
{
    public string Name {get; set; }
    public string Age { get; set; }

    public User(DataReader dr)
    {
       user.Name = Convert.ToString(dr["name"]);
       user.Age ...
    }
}

public class UserExtension : User
{
    public string Lastname {get; set; }
    public string Password {get; set; }

    public UserExtension(DataReader dr):base(dr)
    {
        user.Lastname = Convert.ToString(dr["lastname"]);
        user.Password = ....;

    }
}

      

Police may depend on your circumstances

So a call like

var MyExtension = new UserExtension(dr); 

      

will fill in all fields accordingly.

0


source







All Articles