2
I created a partial class
to the model users
where I implemented some custom functions, such as a function that takes the assigned name and surname and generates a handle
, see in the following example:
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 inserting a new instance in the database I always have to manually call the user.handle = user.makeHandle()
, but I wish I could "overwrite" the method .add()
or the model users
whenever when creating a new instance and assigning values, the method makeHandle()
is called automatically. It is possible to make this call directly 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");
}
The 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: I renamed the method getHandle()
for makeHandle()
so that it does not generate a confusion... because the method now called makeHandle()
should be called only in set;
but in the get;
the value returned is the same instantiated in the column handle
table in the bank.
Have you tried putting users#getHandle in the constructor?
– Gabriel Katakura
Yes, but in the constructor Handle turns empty because the variables first_name and last_name are filled after the constructor starts.
– Rafael Alexandre
The estate Handle can receive any other value from a consumer of the class, or should it always be consented to the value assembled from the name + surname? Does the Handle, because it is a calculated value, shouldn’t it be read only for consumers of the class? And it is not quite certain that the set change the property in the instance and the get continue returning the value that was there before (value of the bank at the time of obtaining the entity).
– Caffé