Criei uma partial class
para o model users
onde implementei algumas funções customizadas, como por exemplo, uma função que pega o nome e sobrenome atribuídos e gera um handle
, veja no exemplo a seguir:
namespace E_Learning.Models
{
using System;
using System.Collections.Generic;
using E_Learning.Helpers;
public partial class users
{
public string makeHandle()
{
return Slugify.Make(this.first_name + ' ' + this.last_name);
}
public string full_name
{
get
{
return this.first_name + ' ' + this.last_name;
}
}
private Nullable<int> age
{
get
{
DateTime now = DateTime.Today;
if (this.birthday.HasValue)
{
DateTime birthday = DateTime.Parse(this.birthday.ToString());
int age = now.Year - birthday.Year;
if (now < birthday.AddYears(age))
age--;
return age;
}
return null;
}
}
}
}
Coisa simples. Porém ao inserir uma nova instância no banco eu tenho sempre que chamar manualmente o user.handle = user.makeHandle()
, mas eu gostaria de poder "sobrescrever" o método .add()
ou o model users
para sempre que ao criar uma nova instância e atribuir os valores, o método makeHandle()
seja chamado automaticamente. É possível fazer essa chamada direto da partial class
?
public ActionResult Index(Entities db)
{
string password = BCryptHelper.HashPassword("admin", BCryptHelper.GenerateSalt(8));
users user = new users
{
first_name = "Rafael",
last_name = "Alexandre",
email = "rafael.alexandre@exemple.com",
password = password,
};
user.handle = user.makeHandle();
db.Set<users>().Add(user);
db.SaveChanges();
return View("~/Views/Index.cshtml");
}
O Model:
namespace E_Learning.Models
{
using System;
using System.Collections.Generic;
using E_Learning.Helpers;
public partial class users
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public users()
{
this.signatures = new HashSet<signatures>();
this.testimonials = new HashSet<testimonials>();
}
public int id { get; set; }
public string identity_ { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string telephone { get; set; }
public string password { get; set; }
public Nullable<System.DateTime> birthday { get; set; }
public Nullable<int> sexuality { get; set; }
public string remember_token { get; set; }
public System.DateTime updated_at { get; set; }
public System.DateTime created_at { get; set; }
public Nullable<byte> status { get; set; }
public string handle { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<signatures> signatures { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<testimonials> testimonials { get; set; }
}
}
OBS: renomeei o método getHandle()
para makeHandle()
para que não gere uma confusão... porque o método chamado agora de makeHandle()
deverá ser chamado apenas no set;
mas no get;
o valor retornado é o mesmo instanciado na coluna handle
da tabela no banco.