16
For deletion of records you may not have, but for Insertion and Change I believe there should already be something discussed.
Probably the best practice on Insertion and Amendment is Viewmodel s, where you create a suitable view for each case, you will then have the data in a simple way to make an Insertion, needing only to pass the data of the Viewmodel to the Domain.
I don’t want to encourage or ask for advice on bad practices, but it’s always good details well clarified, and yet there may be many simple cases of Viewmodel s or cases where a Domain is simple as to our case of need for a Viewmodel that we decide not to replicate a class.
Well, an example:
Person:
public class Pessoa
{
public int Id { get; set; }
[StringLength(50)]
[Required(AllowEmptyString = false)]
public string Nome { get; set; }
[Required]
[DataType(DataType.Date)]
[Column(TypeName = "Date")]
public DateTime DataNascimento { get; set; }
[InverseProperty("Pessoa")]
public virtual ICollection<Telefone> Telefones { get; set; }
}
Telephone:
public class Telefone
{
public int Id { get; set; }
[Required]
public TipoTelefone TipoTelefone { get; set; }
[StringLength(3)]
[Required(AllowEmptyString = false)]
public string Ddd { get; set; }
[StringLength(10)]
[Required(AllowEmptyString = false)]
public string Numero { get; set; }
}
And then we have a View for registering people that allows you to enter phone numbers, and with that we have some cases:
- There may be numbers already registered (editing case);
- New numbers can be inserted (for insertion or editing);
- You can remove some numbers and even add others (editing cases).
Note: I believe I have listed all.
In editing cases can even be simple, just leave the property Telefones
of Pessoa
fed and then add the database.
But for insertion, a question: The Id
person will be passed directly to Telefones
and then this simple example of a Pessoa
and the Telefones
?
[HttpPost]
public ActionResult Save(Pessoa model)
{
if (ModelState.IsValid)
{
dbContext.Pessoas.Add(model);
dbContext.SaveChanges();
}
return View(model);
}
To change: What is recommended to do to then meet the possible conditions presented?
[HttpPost]
public ActionResult Edit(Pessoa model)
{
if (ModelState.IsValid)
{
var entry = dbContext.Pessoas.Find(model.Id);
if (entry != null)
{
dbContext.Entry(Pessoa).CurrentValues.SetValues(model); // ???
dbContext.SaveChanges();
// E se:
// 1. Existir números já cadastrados?
// 2. Inserir novos números e outros já existiam?
// 3. Alguns números e adicionar outros?
}
else
{
ModelState.AddModelError("", "Pessoa não encontrada!");
}
}
return View(model);
}
What are the best practices in these scenarios, which I believe is very generic and explanatory for several cases?
Detail: Even if exemplifying with ASP.NET MVC, responds to other types and projects as well.
This Begincollectionitem would not be very good, where it has numerous fields, besides only being able to leave them all side by side, horizontally, at least it is the examples I see... many data would be a very ugly view kkk, but in the case of Tiago, only 3 properties, of good
– Rod
@Rod Totally wrong what you said. The Begincollectionitem is a enabler data processing of dependent entities. Having more or less fields does not change anything in the behavior of the component. Anyway, I will improve this answer when I can (within the reward period).
– Leonel Sanches da Silva