How to do this using OOP

I have an object called User, with properties like Username, Age, Password email, etc.

I initialize this object in ado.net code, for example:

private User LoadUser(SqlDataReader reader)
{
      User user = new User();

      user.ID = (int)reader["userID"];
      // etc
}

Now let's say that I create a new object that is inherited from the user, for example:

public class UserProfile : User
{
     public string Url {get;set;}
}

Now I need to create a method that will load userprofile, so currently I'm doing:

public UserProfile LoadUserProfile(SqlDataReader reader)
{
         UserProfile profile = new UserProfile();

         profile.ID = (int)reader["userID"];
         // etc. copying the same code from LoadUser(..)
         profile.Url = (string) reader["url"];
}

Is there another approach to OOP, so I don't need to reflect my code in LoadUserProfile () from LoadUser ()?

I would like to do this:

UserProfile profile = new UserProfile();

profile = LoadUser(reader);

// and then init my profile related properties

Is it possible to do something like this?

+3
source share
11 answers

I really donโ€™t understand why the user profile is inherited from the user, logically for me this does not make much sense.

,

public class UserProfile
{
     User user;
     public string Url {get;set;}
}

-

public UserProfile LoadUserProfile(SqlDataReader reader)
{
         User user = LoadUser(reader);
         UserProfile profile = new UserProfile(user);

          //load profile stuff from reader...
         return profile;
}
+3

LodUser User Base . UserProfile .

public class User
{
   public virtual void Load(SqlDataReader reader)
   {
      this.Id = reader["Id"];
      //.. whatever else
   }
}

public class UserProfile : User
{
   public string ExtraProp {get;set;}
   public override void Load(SqlDataReader reader)
   {
      base.Load(reader);
      this.ExtraProp = reader["ExtraProp"];
   }
}

- :

UserProfile up = new UserProfile();
up.Load(myReader);
+5

, . UserProfile . .

+2

, User UserProfile SQL (persistence ignorance = good), /, :

private static User LoadFromReader(this User user, SqlDataReader reader)
{
      user.ID = (int)reader["userID"];
      // etc

      return user;
}

public UserProfile LoadFromReader(this UserProfile profile, SqlDataReader reader)
{
     ((User)profile).LoadFromReader(reader);

     profile.Url = (string) reader["url"];
     // etc

     return profile;
}

:

UserProfile profile = new UserProfile().LoadFromReader(reader);
+2

:

 class User 
 {
      public User(SqlDataReader reader)
      {
          Initialize(reader);
      }

      protected virtual void Initialize(SqlDataReader reader)
      {
           this.ID = (int)reader["userID"];
       // etc
      }
 }

 class UserProfile : User
 {
      public UserProfile(SqlDataReader reader) : base(reader) {}

      protected override void Initialize(SqlDataReader reader)
      {
           base.Initialize(reader); // Initialize "user" variables
           this.MyProperty = (int)reader["myProperty"];
      }
 }

, ( ), .

+1

Factory . :. , User UserProfile. UserProfile User .

    public static class UserFactory
    {
        public static User LoadUser(SqlDataReader reader)
        {
            int id = (int)reader["userID"];
            return new User(id);
        }

        public static UserProfile LoadUserProfile(SqlDataReader reader)
        {
            User user = LoadUser(reader);
            // extra properties
            string url = (string)reader["url"];
            return new UserProfile(user, url);
        }
    }
+1

LoadUser . UserProfile .

0

LoadUser base.LoadUser(reader) UserProfile, UserProfile.

, , . .

0

, :

public User(DbDataReader reader) {
   ID = reader.GetInt32(reader.GetOrdinal("userID"));
   // etc
}

:

User adam = new User(reader);

UserProfile :

public UserProfile(DbDataReader reader) : base(reader) {
   Url = reader.GetString(reader.GetOrdinal("url"));
}
0

public class User
{
//...all current members of the user class...

     UserProfile profile;
}

public class UserProfile
{

     public string Url {get;set;}

}

, -

0

, - :

public class User { public Guid Id { get; set; } }
public class UserProfile : User { public string Url { get; set; } }

class MyReader
{
    private T LoadUser<T>(IDataReader reader)
        where T : User, new()
    {
        T user = new T();
        user.Id = reader.GetGuid(reader.GetOrdinal("userID"));
        return user;
    }

    public UserProfile LoadUserProfile(IDataReader reader)
    {
        UserProfile profile = LoadUser<UserProfile>(reader);
        profile.Url = (string)reader["url"];
        return profile;
    }
}
0

Source: https://habr.com/ru/post/1718393/


All Articles