Class design and object references

Here is a very basic example of what I am trying to do. In reallity there are more relations but not something extreme or memory heavy.

public class ClassA : ISomething
{
    public double property { get; set; }
    ...
    public ClassB classb { get; set; }
}

public class ClassB : ISomething2
{
    public double Length { get; set; }
    ...
}

public class MyProject : BaseProject
{
    public IEnumerable<ISomething> ISomethings { get; set; }
    ...
    public IEnumerable<ISomething2> ISomethings2 { get; set; }
    ...
}

The issue here is that I have to keep a list of MyProject.ISomethings2 and then classb property of ClassA to reference only an existing item in MyProject.ISomethings2 list. The hard thing here is that:

  • Removing an item from ISomething2 should remove all references to it (classb property of ClassA instances to be set to null)
  • Prevent other developers setting classb property to a non existing object in list or a new user created object.
  • Objects in MyProject.ISomethings2 could be added without having to be referenced from elsewhere.

These classes are used for project description not database stuff. Like when you open a project file for an application. So changing a property in classB should be visible to all since it is the same object reference.
Is there a pattern to achieve what I want without much coupling? Alternative designs/approaches are also welcome.

4

Is there a pattern to achieve what I want without much coupling?

Not really, since at its core what you want to do “when X happens, I want to do Y to these other things” is coupling by definition. Which also makes this a rather undesirable design for the problem at hand.

If I had to work with this, I’d start by looking to make the classes immutable (publically at least) and controlled by the project. If you can’t set the classb property on the object, there’s no way to set it to be invalid. Instead the project class can do that (along with validation rules, and possible optimizations like reference counting perhaps).

But honestly, I would try very hard to not work with this. Project here is a manager of the other objects. Forcing class instances to be in some external collection is gross and unweildly. Occasionally it’s somewhat necessary (games especially do this), but it should be avoided where possible and used with care otherwise. Without knowing more about the problem you’re trying to solve, I can’t say for sure which bucket this falls in.

3

Note: I’m assuming the language is C#. If it’s Java, similar mechanisms should exist as well.

  • If you want to track changes to the properties of ClassA, you can use INotifyPropertyChanged.

  • If you want to track changes to the ISomethings2 sequence, instead of IEnumerable<T>, use ObservableCollection<T>.

  • If you just need to keep ISomethings2 sequence in sync with ISomethings, rewrite the property as:

    public IEnumerable<ISomething2> ISomethings2
    {
        get
        {
            return this.ISomethings.Select(c => c.classb);
        }
    }
    

If this doesn’t give you an answer, consider editing your question. Its actual form is very unclear because of the lack of proper terminology and cryptic names of your example. Also note that you currently cannot remove anything from ISomethings2, given its type.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật