简体   繁体   中英

How to determine value of property based on other instance of class

How I can make all instances of a class that contain the same property, to all have the same value? For example in my class:

public class Doctor
{
   public int Id {get;set;}
   public string FirstName {get;set;}
   public string LastName {get;set;}
   public string Address {get;set;}
   public bool? IsAvailable {get;set;}
   public virtual ICollection<Center> Centers {get;set;}
 }

I might have multiple doctors with the same address, and each doctor is either available or not available, after creating the project, now I've realized that if any doctor is unavailable then that means that every doctor with the same address must be unavailable. Should I add it into the controller? Or add it to my DbContext class? Here is the DbContext :

public class DoctorContext : DbContext
{
    public DoctorContext() : base("DoctorContext")
    {
    }
    public DbSet<Doctor> Doctors { get; set; }
    public DbSet<Center> Centers { get; set; }
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

This kind of logic can be added on the domain level, for example, constrain object creation with the conditions related to it, like the following:

  public class Doctor
{

    public Doctor(string firstName , string LastName, string address , string isAvailable , ICollection<Center> centers)
    {
        //Check for the avaiblity
        IsAvailable(string address);

         // after checking the data assign in it 
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Address = address;
        this.IsAvailable = isAvailable;
        this.Centers = centers;
    }
    public int Id { get; private set; }
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
    public string Address { get; private set; }
    public bool? IsAvailable { get; private set; }
    public virtual ICollection<Center> Centers { get; private set; }


}

The controller should not include business logic, for better design put your logic in a different layer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM