0

I'm trying to find the best way to populate my constructor with another constructor that have almost the same structure without setting each attribute,

So i have constructor Altridatiidentificativi in ModelRA constructor:

public class Altridatiidentificativi
{
    public string denominazione { get; set; }
    public string indirizzo { get; set; }
    public string numeroCivico { get; set; }
    public string cap { get; set; }
    public string comune { get; set; }
    public string provincia { get; set; }
    public string nazione { get; set; }
    public bool modificati { get; set; }
    public string defAliquotaIVA { get; set; }
    public bool nuovoUtente { get; set; }
}

And Altridatiidentificativi in Documenti:

    public class Altridatiidentificativi
    {
        public bool nuovoUtente { get; set; }
        public string denominazione { get; set; }
        public string indirizzo { get; set; }
        public string numeroCivico { get; set; }
        public string cap { get; set; }
        public string comune { get; set; }
        public string provincia { get; set; }
        public string nazione { get; set; }
    }

As you can see the structure is almost the same, just constructor in ModelRA has this two extras modificati and defAliquotaIVA

So i was wondering if it's possible in some way to pass inside ModelRA.Altridatiidentificativi the Documenti.Altridatiidentificativi and then add the value to the extras

I was trying to do something like this :

 public ModelRA initializeRA(Documento documento)
    {
        ModelRA model = new ModelRA();
        model.altriDatiIdentificativi = <Altridatiidentificativi>(documento.altriDatiIdentificativi);
        model.altriDatiIdentificativi.defAliquotaIVA = "";
        model.altriDatiIdentificativi.modificati = false;

        return model;
    }

but i get error in <Altridatiidentificativi> "it's a type not a valid constructor in specific context"

Is there a way to reach what i'm trying to do or i have to set all the attributes manually?

NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100
  • 1
    First of all, please, learn about: what is constructor in C#. You can use common interface for two models or common base class to solve your task. – Maradik Apr 14 '20 at 15:09
  • @Maradik actually i'm "forced" to use two split models so to use one common interface or common base wouldn't be a solution – NiceToMytyuk Apr 14 '20 at 15:13
  • Then if you are forced, you can use a library like AutoMapper to map different fields from two objects. Then you would do something like: `model.altriDatiIdentificativi = mapper.Map(documento.altriDatiIdentificativi);` Have a look here: https://docs.automapper.org/en/stable/Getting-started.html – Anouar Apr 14 '20 at 15:15
  • 1
    There are no constructors in the question. – Scott Hannen Apr 14 '20 at 15:25
  • 1
    I think you mean "structure" (maybe better user class or object) and not "constructor" – Anouar Apr 14 '20 at 15:42
  • 1
    There are also a large number of questions with answers on Stack Overflow that discuss using reflection to copy properties between two instances of the same type. The code for that scenario can easily be adapted to your own. Please do some research before posting your question to Stack Overflow. – Peter Duniho Apr 14 '20 at 15:53
  • Is `ModelRA` a namespace or another class? – John Alexiou Apr 14 '20 at 15:59
  • @ja72 it's another class – NiceToMytyuk Apr 14 '20 at 16:00

1 Answers1

1

Usually this pattern is a signal that there's a concept in your business model that needs to be abstracted into a composable pattern. The ModelRA.Altridatiidentificativi class could look like:

public class Altridatiidentificativi
{
    public ModelRA.Altridatiidentificativi ModelRAAltridatiidentificativi { get; set; }
    public bool modificati { get; set; }
    public string defAliquotaIVA { get; set; }
}

Then your initialization code could look like this:

    public ModelRA initializeRA(Documento documento)
    {
        ModelRA model = new ModelRA();
        model.altriDatiIdentificativi.ModelRAAltridatiidentificativi = documento;
        model.altriDatiIdentificativi.defAliquotaIVA = "";
        model.altriDatiIdentificativi.modificati = false;

        return model;
    }

Tangentially I should mention that it's usually good practice to use property initializers and constructors unless you have a specific reason that you need initialization methods.

public class Altridatiidentificativi
{
    public string ModelRA.Altridatiidentificativi ModelRAAltridatiidentificativi { get; set; }
    public bool modificati { get; set; } = false; // unnecessary: this is default.
    public string defAliquotaIVA { get; set; } = "";
    public Altridatiidentificativi(ModelRA.Altridatiidentificativi modelRAAltridatiidentificativi)
    {
        this.modelRAAltridatiidentificativi = ModelRAAltridatiidentificativi;
    }
}
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • At the end after trying to use automapper i ended to use the following solution as it get to me more readable and understandable code, but so if i have nested object one inside other to add values to nested object i have to first initialize child object it is not enough to initialize only the parent? – NiceToMytyuk Apr 15 '20 at 10:29
  • 1
    @IgorMytyuk: If you have sane default values for the child object, you can use property initializers to set those values whenever the child object is constructed and then use a property initializer on the parent's property to set it to be a `new ModelRA.Altridatiidentificativi()` by default, avoiding the need to take it as a constructor argument. – StriplingWarrior Apr 15 '20 at 16:48