D
D
Daeamon2017-08-08 10:30:53
ASP.NET
Daeamon, 2017-08-08 10:30:53

How can I shorten the child class constructor to set all parameters of the parent object to the child?

Hello, is it possible to write the constructor of the ArticleForSearch(Article doc) class in a couple of lines so that it would not have to be supplemented each time?)
The task was to enter all parent parameters containing data into the newly created object.

namespace Proved.Models
{
    public class ArticleForSearch:Article
    {

        public ArticleForSearch()
        {

        }
        public ArticleForSearch(Article doc)
        {
            if (!String.IsNullOrEmpty(doc.Title)) Title = doc.Title;
            if (!String.IsNullOrEmpty(doc.Alias)) Alias = doc.Alias;
            if (!String.IsNullOrEmpty(doc.Lead)) Alias = doc.Lead;
            if (!String.IsNullOrEmpty(doc.Content)) Content = doc.Content;
            if (!String.IsNullOrEmpty(doc.Image)) Image = doc.Image;
            if (!String.IsNullOrEmpty(doc.MetaTitle)) MetaTitle = doc.MetaTitle;
            if (!String.IsNullOrEmpty(doc.MetaDescription)) MetaDescription = doc.MetaDescription;
            if (!String.IsNullOrEmpty(doc.MetaKey)) Robots = doc.MetaKey;
            if (!String.IsNullOrEmpty(doc.Robots)) Robots = doc.Robots;

            ID = doc.ID;
            State = doc.State;
            Category = doc.Category;
            Created = doc.Created;
            AuthorID = doc.AuthorID;
            AuthorPseudo = doc.AuthorPseudo;
            YandexGenre = doc.YandexGenre;
            GoogleGenre = doc.GoogleGenre;
            MaterialType = doc.MaterialType;
            ArticleThemeID = doc.ArticleThemeID;
            Featured = doc.Featured;
            ShowImage = doc.ShowImage;
            Hits = doc.Hits;

            if (AuthorID == 0)
            {
                AuthorID = BaseController.UserId;
            }
            if(AuthorPseudo > 0)
            {
                if(MvcApplication.AuthorsPseudo.Where(o => o.ID == AuthorPseudo).FirstOrDefault() == null) AuthorID = BaseController.UserId;
                ArticleAuthorName = MvcApplication.AuthorsPseudo.Where(o => o.ID == AuthorPseudo).FirstOrDefault().FullName;
            }
            else
            {
                if (MvcApplication.AuthorsPseudo.Where(o => o.ID == AuthorPseudo).FirstOrDefault() == null) AuthorID = BaseController.UserId;
                ArticleAuthorName = MvcApplication.Authors.Where(o => o.UserId == AuthorID).FirstOrDefault().FullName;
            }
            ArticleCategoryName = MvcApplication.AllCategories.Where(o => o.ID == Category).FirstOrDefault().Name;
        }

        public string ArticleThemeName { get; set; }
        public string ArticleCategoryName { get; set; }
        public string ArticleAuthorName { get; set; }
        public List<Tag> Tags { get; set; }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question