I created a partial class
for the model users
where I implemented some custom functions, such as a function that takes the first and last name assigned and generates a handle
, see example below:
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;
}
}
}
}
Simple thing. But when I insert a new instance in the database I always have to manually call user.handle = user.makeHandle()
, but I'd like to be able to "override" the .add()
or the model users
method for whenever creating a new instance and assign the values, the makeHandle()
method is called automatically. Is it possible to make this direct call from 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 = "[email protected]",
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; }
}
}
NOTE: I renamed the getHandle()
method to makeHandle()
so it does not cause confusion ... because the method now called makeHandle()
should only be called in set;
but in get;
the value returned is the same instantiated in the column handle
of the table in the bank.