开发者

Propogate property value in class hierachy

I want to propagate the property from child class to parent class, ie: If MySchool.ModifiedTime is changed it should change the ModifiedTime in Student Class too, like wise LstBook[0].ModifiedTime is changed it should change MySchool.ModifiedTime as well Student.ModifiedTime... (basically ModifiedTime should be in sync),any Idea

I'm looking for a Generic function in BaseClass to achieve this.

public class MyBaseClass
{
  public开发者_Go百科 DateTime ModifiedTime{ get; set; }
}

public class Student: MyBaseClass
  {       
 public string Name { get; set; }
 public school MySchool {get;set;} 
 }

public class School : MyBaseClass
{
 public string SchoolName { get; set; }
 public List<Book> LstBook {get;set;}
}

public class Book:MyBaseClass
{
  public string BookName{get;set;}
}


You could make ModifiedTime virtual and then in each child class override it to perform the syncing.

public class MyBaseClass
{
  public virtual DateTime ModifiedTime{ get; set; }
}

public class Student: MyBaseClass
{       
  public string Name { get; set; }
  public school MySchool {get;set;} 
  public virtual DateTime ModifiedTime
  { 
    get {
       return MySchool.ModifiedTime;
    }
    set {
       MySchool.ModifiedTime = value;
    }
  }
}

And so on.

However, I would reconsider your class hierarchy because it seems like the factoring is incorrect. If all the properties need to be in sync across the entire hierarchy then maybe only one class should have that property and other classes should refere to it. For example, only School should have the ModifiedTime property and when you need to get the modified time for a student you would retrieve it through the MySchool property


You seem to be misunderstanding how object hierarchy works.

Implementing this as a class member only links it to the object created, and a static method would of course mean all objects access the same property.

Instead, as I understand it, you want groups of instances (not all) to share a property.

The simplest way to do this is to create a shared object that provides the modified time for all instances in a group.


As the other commenters have pointed out, you can't do this in a straightforward way with a base class simply because that's not how class hierarchies work. What you could do is create another class called "GroupInfo" or something like that. Make ModifiedTime a property on that.

In all your other classes, add a property for a GroupInfo. Then whenever you create a new book or whatever, as part of the constructor pass in a reference to the GroupInfo for the book.

That way all the objects in the group will share a single GroupInfo, and thus a single ModifiedTime.


You can make ModifiedTime static, which will cause it to be shared among all derived instances of MyBaseClass.

public class MyBaseClass
{
  public static DateTime ModifiedTime{ get; set; }
}


Update: More complete example; better explanation of methodology

Your base class could be better described as a interface since your enforcing that each class implement a common property and not making common calculations or sweeping generalizations that could group schools, students, and books together.

Pursuing an event driven solution there are a few things you can do such as using BindingList<T> which is basically List<T> on steroids.

Unfortunatly, you'll need to explode your pretty little { get; set; } properties into full fields, but the best way is for each modification of a property to trigger an event. Each subsequent object that is affected by the modification is subscribed to your modified objects Modified event.

public interface IChangeAware
{
    event EventHandler<EventArgs> OnChange;
    DateTime ModifiedTime { get; set; }
}

public class Student : IChangeAware
{
    public event EventHandler<EventArgs> OnChange;
    public DateTime ModifiedTime { get; set; }

    public string Name { get; set; }
    public School School
    {
        get { return School; }
        set 
        {
            School = value; 
            if (this.OnChange != null)
                this.OnChange(this, new EventArgs());
        }
    }

    public Student()
    {
        if (School != null)
            School.OnChange += MySchoolOnChange;
    }

    void MySchoolOnChange(object sender, EventArgs e)
    {
        ModifiedTime = DateTime.Now;
    }
}

public class School : IChangeAware
{
    public event EventHandler<EventArgs> OnChange;
    public DateTime ModifiedTime { get; set; }

    public string SchoolName { get; set; }
    public BindingList<Book> Books { get; set; }

    public School()
    {
        Books = new BindingList<Book>();
        Books.ListChanged += BooksListChanged;
    }

    void BooksListChanged(object sender, ListChangedEventArgs e)
    {
        ModifiedTime = DateTime.Now;
        OnChange(this, new EventArgs());
    }
}

public class Book
{
    public string BookName { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜