代码之家  ›  专栏  ›  技术社区  ›  onedaywhen

组件对象模型教程?

  •  4
  • onedaywhen  · 技术社区  · 14 年前

    有人知道为.NET 3.0或更高版本(即充分利用泛型)创建组件层次对象模型的好教程(C_首选)吗?

    • 处理和暴露不定数量的被包含对象;
    • 防止客户端应用程序直接实例化包含的对象;
    • 允许客户端代码使用“隧道”样式,例如

    string columnName = db.Tables["Customers"].Columns["customer_number"].Name;

    我找到了这篇文章, Component Object-Model Recommendations 但因为它适用于Visual Studio.NET 2003,但我认为.NET 3.0中有更好的方法。

    谢谢。

    更新:

    如果我正确理解索引器…

    db 我可以用一个 SortedList 作为表的容器,使用和索引器访问以返回 Table 实例。同样地, 的索引器将访问 Column 实例。因此客户机代码可能如下所示:

    string tableName = db["Customers"].Name;

    问题是, 分贝 类除了公开 Tables ( StoredProcs , DBViews 等)。所以我想保留每个类的集合类,将它们作为属性公开,而不是使用索引器。这听起来对吗?不过,我应该在这些集合类上使用索引器吗?

    2 回复  |  直到 14 年前
        1
  •  2
  •   stakx - no longer contributing Saravana Kumar    14 年前
    • string columnName = db.Tables["Customers"].Columns["customer_number"].Name;

    public DataTable this[string tableName] { ... }
    //     ^^^^^^^^^
    //     choose one, unspecific type for all exposed collections
    

    public IList<Customer> Customers { get { ... } }
    public IList<Supplier> Suppliers { get { ... } }
    //           ^^^^^^^^
    //           the right type for each collection
    


    public class Db
    {
        public IList<Customer> Customers { get { ... } }
        ...
    }
    

    • Customers

    • Customer db.Customers

    private List<Customer> customers = new List<Customer>();
    // ^ for class-internal use only
    
    public IList<Customer> Customers { get { return this.customers.AsReadOnly(); } }
    //                                                            ^^^^^^^^^^^^^
    //                                                expose collection as read-only
    

    • db DataTable ICollection

    • Suppliers

    db.Customers["John Smith"].BillingAddress
    

    db.Tables["Customers"].Rows["John Smith"].Columns["BillingAddress"] as Address
    

    BillingAddress

        2
  •  1
  •   Hans Passant    14 年前