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

将类拆分为子类

  •  0
  • Nick  · 技术社区  · 17 年前

    BLL.Database MyDB = new BLL.Database();
    BLL.Entity.User MyUser = Database.UserGetById(42);
    

    BLL.Database MyDB = new BLL.Database();
    BLL.Entity.User MyUser = Database.User.GetById(42);
    

    namespace BLL
    {
        public partial class Database
        {
            // private members..
            // constructor
    
            #region User
            public IQueryable<BLL.Entity.User> UserGetAll()
            {
                // ...
            }
    
            public BLL.Entity.User UserGetById(int UserId)
            {
                // ...
            }
    
            public void UserSave(ref BLL.Entity.User user)
            {
                // ...
            }
    
            public void UserDelete(int userId)
            {
                // ...
            }
            #endregion
    
            // More sets of methods for other entities in database here..
        }
    }
    

    namespace BLL
    {
        public partial class Database
        {
            private _User;
            public User
            {
                if (_User == null)
                {
                    _User = new User();
                }
                return _User;
            }
    
            // Other data access classes here..
        }
    
        public partial class User
        {
            public IQueryable<BLL.Entity.User> GetAll()
            {
                // ...
            }
    
            // GetById, Save & Delete methods here..
        }
    }
    

    2 回复  |  直到 17 年前
        1
  •  1
  •   CodeRedick    17 年前

    partial 部分的 部分的 所有类必须位于同一程序集中。

    更好的解决方案可能是创建 extension methods .

        2
  •  2
  •   Adam Robinson    17 年前

    当然,在这样做之后,我听说了Linq to SQL,我们开始使用它。..所以你可能想把它作为另一种选择。