Search code examples
c#entity-frameworkmvp

Insert related entities using MVP


I'm using MVP pattern and the EF in my C# application. In my database design there is a one-to-many relationship between 'personas' and 'referencias', each 'personas' can have 0 or mutiple 'referencias'.

According to MVP pattern I have a 'Personas' Model which perfoms CRUD operations in my physical database. I have a method which performs insertion like this:

public void AgregaPersona(_Persona persona)
        {
               Persona per = new Persona()
                {
                    Nombres = persona.nombres,
                    ApellidoP = persona.apellidoP,
                    ApellidoM = persona.apellidoM,
                    FechaNacimiento = persona.fechaNacimiento,
                    Sexo = persona.sexo.ToString(),
                    EdoCivil = persona.edoCivil,
                    RFC = persona.RFC,
                    CURP = persona.CURP,
                    Domicilio = persona.domicilio,
                    CP = persona.codigoPostal,
                    Telefonos = persona.telefonos,
                    Celular = persona.celular,
                    Email = persona.email,
                    IDDel = persona.idDelegacion,
                    IDEmpresa = persona.idEmpresa
                };
                context.personas.AddObject(per);
                context.SaveChanges();
        }

The question is: how do I relate 'referencias' insertion in my code? Following MVP rules I must create a Model for 'referencias', isn't it? Should I call an insertion method defined in 'referencias' model?


Solution

  • Here is the code, then I will explain :)

    public class Persona
    {
        public Persona()
        {
            //Make sure that Referencias is instantiated by default
            Referencias = new List<Referencia>();
        }
    
        public String Nombres {get; set;}
        //...The other properties of Persona
        public Int publicIDEmpresa {get; set;}
        //The virtual is here for lazy loading 
        public virtual ICollection<Referencia> Referencias {get; set;} 
    }
    
    public class Referencia
    {
        public Int ReferenciaId {get; set;}
        public String Nombre {get; set;}
        //...Other properties of Referencia
    }
    

    Your code to make these work together:

        public void AgregaPersona(_Persona persona)
        {
               Persona per = new Persona()
                {
                    Nombres = persona.nombres,
                    ApellidoP = persona.apellidoP,
                    ApellidoM = persona.apellidoM,
                    FechaNacimiento = persona.fechaNacimiento,
                    Sexo = persona.sexo.ToString(),
                    EdoCivil = persona.edoCivil,
                    RFC = persona.RFC,
                    CURP = persona.CURP,
                    Domicilio = persona.domicilio,
                    CP = persona.codigoPostal,
                    Telefonos = persona.telefonos,
                    Celular = persona.celular,
                    Email = persona.email,
                    IDDel = persona.idDelegacion,
                    IDEmpresa = persona.idEmpresa
                };
                Referencia newRef = new Referencia
                {
                    Nombre = referenciaNombre;
                    //Fill the rest of the properties except ID (this should be auto)
                }
                per.Referencias.Add(newRef);
                context.personas.AddObject(per);
                context.SaveChanges();
        }
    

    This is all you need to do as far as creating two separate objects (as you expected) that are related to each other. Here is my best description of what is going on here

    When you create the ICollection<Referencia> Referencias, all this is doing is creating a link (relationship) between the two objects (Persona and Referencia). The objects are still separate, only being linked via this collection.

    When you go to actually create the Persona with Referencia mappings, you have to create your Persona, then you create the separate object of the Referencia and relate it to the Persona by adding it to Persona's ICollection mapping (Referencias). When the actual code is run to persist this to the database, it will treat this as separate inserts, something like this pseudo-code:

    BEGIN TRANSACTION
    INSERT PERSONA
    GET PERSONA ID
    INSERT REFERENCIA USING NEW PERSONA ID(Repeat until all Referencias are inserted)
    COMMIT TRANSACTION
    

    Now, keep in mind the note I made about lazy loading. By default, whenever you load Persona, it will not load the Referencias until you actually need it. It will only load these objects from the database if you try to access values within this property. Thus, emphasizing even further that these are, indeed two separate objects.

    Also, you can create a two-way mapping if you want. You simply add another link (relationship), this time from Referencia to its corresponding Persona.

    public class Referencia
    {
        public Int ReferenciaId {get; set;}
        public String Nombre {get; set;}
        //....Other properties of Referencia
        public virtual Persona Persona {get;set;}
    }
    

    Do take note that the property name is the same as the class name. This is a convention, and if you deviate by naming the property something else, then you will need to add an attribute above your Referencias object in Persona. This is so that EF knows that this is a two-way relationship. So, if you decide to name the Persona property in Referencia something like PersonaRef, then your code would look more like this:

    public class Persona
    {
        public Persona()
        {
            //Make sure that Referencias is instantiated by default
            Referencias = new List<Referencia>();
        }
    
        public String Nombres {get; set;}
        //...The other properties of Persona
        public Int publicIDEmpresa {get; set;}
        //The virtual is here for lazy loading 
        [InverseProperty("PersonaRef")]
        public virtual ICollection<Referencia> Referencias {get; set;} 
    }
    

    Hopefully, that gives you a better understanding of how relationships work in EF (and this translates fairly well to other ORMs). So, you get the two distinct models that you need, and can traverse between the two with relationship mapping properties.

    Here is a very good article on EF Code First by Scott Gu that you might find helpful