Insert related objects using MVP

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

According to the MVP pattern, I have a Personas model that performs CRUD operations in my physical database. I have a method that performs the insert as follows:

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(); } 

Question: how do I link the insert "referencias" in my code? Following the rules of MVP, I have to create a model for 'referencias', right? Should I call the insert method defined in the referencias model?

+4
source share
4 answers

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 for them to 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 to create 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 an ICollection<Referencia> Referencias , all this does is create a link (relationship) between the two objects ( Persona and Referencia ). Objects are still separate, only linked through this collection.

When you go to create Persona mappings with Referencia , you need to create your Persona , then you will create a separate Referencia object and related to it Persona , adding it to Persona display ICollection ( Referencias ). When the actual code is run to save this in the database, it will treat it as separate inserts, something like this pseudocode:

 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 download Persona , it does not load Referencias until you need it. It will only load these objects from the database if you try to access the values โ€‹โ€‹in this property. Thus, further emphasizing that these are indeed two separate objects.

In addition, you can create two-way matching if you want. You simply add one more link (relation), this time from Referencia to the 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;} } 

Note that the name of the property is the same as the class name. This is a convention, and if you deviate by calling the property something else, you need to add an attribute above your Referencias object in Persona . It is so that EF knows that this is a two-way communication. So, if you decided to name the Persona property in Referencia something like PersonaRef , then your code would look 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 this will give you a better idea of โ€‹โ€‹how relationships work in EF (and it translates pretty well with other ORMs). This way you get two different models that you need, and you can intersect them using relationship matching properties.

Here is a very good article about EF Code First by Scott Gu that you can find useful

+1
source

You can add them to the Referencia navigation property. This way they are also inserted into db.

 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 ref1 = new Referencia(); //populate related properties. per.Referencias.Add(ref1); context.personas.AddObject(per); context.SaveChanges(); } 
+2
source

Disclaimer: I am not familiar with EF and MVP, only with ORM.

You will most likely have to create new instances of your Referencia class, possibly adding them to your collection in the Persona class.

On my ActiveRecord / NHibernate stack, here's how I would do it:

 Persona per = new Persona(...); // as you do now per.Referencias = new List<Referencia>(); per.Referencias.Add(new Referencia(...)); // same way you create Personas 

Hope this helps.

0
source

if i understand you ...

In the view (IViewPersona), you can use the Referencia property, then you can create a new object and insert it.

Sincerely.

0
source

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


All Articles