What is the name of this template?

What is called this template when you give an object a parameter in its constructor and the object fills its fields with a parameter?

For instance:

/// <summary>
/// Represents a user on the Dream.In.Code website.
/// </summary>
public class User
{       
    /// <summary>
    /// Load a user by providing an ID.
    /// </summary>
    /// <param name="ID">A user individual ID number.</param>
    public User(string ID)
    {
        WebClient webClient = new WebClient();
        string htmlSource = webClient.DownloadString(new Uri(String.Format("http://www.dreamincode.net/forums/xml.php?showuser={0}",ID)));
        XDocument xml = XDocument.Parse(htmlSource);

        var profileXML = xml.Element("ipb").Element("profile");

        //Load general profile information.
        this.ID = profileXML.Element("id").Value;
        this.Name = profileXML.Element("name").Value;
        this.Rating = profileXML.Element("rating").Value;
        this.Photo = profileXML.Element("photo").Value;
        this.Reputation = profileXML.Element("reputation").Value;
        this.Group = profileXML.Element("group").Element("span").Value;
        this.Posts = profileXML.Element("posts").Value;
        this.PostsPerDay = profileXML.Element("postsperday").Value;
        this.JoinDate = profileXML.Element("joined").Value;
        this.ProfileViews = profileXML.Element("views").Value;
        this.LastActive = profileXML.Element("lastactive").Value;
        this.Location = profileXML.Element("location").Value;
        this.Title = profileXML.Element("title").Value;
        this.Age = profileXML.Element("age").Value;
        this.Birthday = profileXML.Element("birthday").Value;
        this.Gender = profileXML.Element("gender").Element("gender").Element("value").Value;

        //Load contact information.
        var contactXML = xml.Element("ipb").Element("profile").Element("contactinformation");
        this.AIM = contactXML.XPathSelectElement("contact[title='AIM']/value").Value;
        this.MSN = contactXML.XPathSelectElement("contact[title='MSN']/value").Value;
        this.Website = contactXML.XPathSelectElement("contact[title='Website URL']/value").Value;
        this.ICQ = contactXML.XPathSelectElement("contact[title='ICQ']/value").Value;
        this.Yahoo = contactXML.XPathSelectElement("contact[title='Yahoo']/value").Value;
        this.Jabber = contactXML.XPathSelectElement("contact[title='Jabber']/value").Value;
        this.Skype = contactXML.XPathSelectElement("contact[title='Skype']/value").Value;
        this.LinkedIn = contactXML.XPathSelectElement("contact[title='LinkedIn']/value").Value;
        this.Facebook = contactXML.XPathSelectElement("contact[title='Facebook']/value").Value;
        this.Twitter = contactXML.XPathSelectElement("contact[title='Twitter']/value").Value;
        this.XFire = contactXML.XPathSelectElement("contact[title='Xfire']/value").Value;

        //Load latest visitors.
        var visitorXML = xml.Element("ipb").Element("profile").Element("latestvisitors");
        this.Visitors = (from visitor in visitorXML.Descendants("user")
                        select new Visitor(){
                            ID = visitor.Element("id").Value,
                            Name = visitor.Element("name").Value,
                            Url = visitor.Element("url").Value,
                            Photo = visitor.Element("photo").Value,
                            Visited = visitor.Element("visited").Value,
                        }).ToList();

        //Load friends.
        var friendsXML = xml.Element("ipb").Element("profile").Element("friends");
        this.Friends = (from friend in friendsXML.Descendants("user")
                        select new Friend()
                        {
                            ID = friend.Element("id").Value,
                            Name = friend.Element("name").Value,
                            Url = friend.Element("url").Value,
                            Photo = friend.Element("photo").Value
                        }).ToList();

        //Load comments.
        var commentsXML = xml.Element("ipb").Element("profile").Element("comments");
        this.Comments = (from comment in commentsXML.Descendants("comment")
                            select new Comment()
                            {
                                ID = comment.Element("id").Value,
                                Text = comment.Element("text").Value,
                                Date = comment.Element("date").Value,
                                UserWhoPosted = new Friend()
                                {
                                    ID = comment.Element("user").Element("id").Value,
                                    Name = comment.Element("user").Element("name").Value,
                                    Url = comment.Element("user").Element("url").Value,
                                    Photo = comment.Element("user").Element("photo").Value
                                }
                            }).ToList();
    } 
}

What will this template be? Also, to make my code cleaner, do you recommend using this template when creating objects inside objects, for example, in my UserWhoPosted variable. Instead of:

UserWhoPosted = new Friend()
{
    ID = comment.Element("user").Element("id").Value,
    Name = comment.Element("user").Element("name").Value,
    Url = comment.Element("user").Element("url").Value,
    Photo = comment.Element("user").Element("photo").Value
}

I would have succeeded:

UserWhoPosted = new Friend(myFriendXElementVariable);

And just let him make out what he needs and fill it with fields.

Thanks for the guide.

EDIT: After reading all your suggestions, I repaired the code a bit. Do you think this is better? What would you improve?

Thanks for taking the time to help, I'm trying to drop bad programming habits.

http://pastebin.com/AykLjF2i

+3
7

-: XML-, . , Factory, :

public static class FactoryUser{
public static User GetUserFromXml(Xml){
//your code here
}
}

public static class UserWebrequester{
public static Xml GetXmlUser(id){
}
}

, , unit test .

+8

Adapter, , , XML .

+2

. - , , , (Observer). , . "" .

+2

, private private Load, . , .

+2

, " ", , ( ... - ).

, , , DAO.

, . , , . , ; , , , .

... , "" , "", "" .. , ? , . , , , .

Load, Factory. ..

, . factory .

+2

, :

D = comment.Element("user").Element("id").Value,

​​ . - , .

, , ,... , . . , toplevel , .

+2

, , .

Another problem with passing xml data to the constructor, as opposed to a specific type. A particular type defines a well-known set of properties or fields, where xml does not guarantee a structure ...

0
source

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


All Articles