For deletion of records you may not have, but for Insertion and Amendment I believe there should already be something discussed.
The best recommended practice on Insertion and Change is ViewModel's , where you create a view that is appropriate for each case, then you will have the form data simple to do an Insertion, just passing the ViewModel data to the Domain .
I do not want to encourage or ask for advice on bad practices, but good details are always good, and yet there can be many simple cases of ViewModel's or cases where a Domain is simple for our case of a ViewModel
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; }
}
Phone :
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 people register that allows you to enter phone numbers, and with this we have some cases:
Note: I believe I have listed them all.
In editing cases it may even be simple, just leave the property Telefones
of Pessoa
fed and then add the database.
But for insertion , a question: The Id
of person will be passed directly to Telefones
and then this simple insertion example registers Pessoa
and 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 satisfy 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 best practices in these scenarios, which I believe to be very general and explanatory in many cases?
Detail : Same as example with ASP.NET MVC , it responds to other types and projects as well.