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

多个DataContext/EntitiesObject

  •  0
  • Omar  · 技术社区  · 16 年前

    我在应用程序中实现了一个存储库模式,在一些控制器中,我使用各种不同的存储库。(未实施国际奥委会)

    UsersRepository Users;
    OtherRepository Other;
    Other1Repository Other1;   
    
    public HomeController()
    {
         this.Users = new UsersRepository();
         this.Other = new OtherRepository();
         this.Other1 = new Other1Repository();
    }
    

    为了避免将来出现膨胀的控制器构造函数的问题,我创建了一个包装类,其中包含作为类对象的所有存储库,并在控制器构造函数中调用该类的单个实例。

    public class Repositories
    {
        UsersRepository Users;
        OtherRepository Other;
        Other1Repository Other1;
    
        public Repositores()
        {
             this.Users = new UsersRepository();
             this.Other = new OtherRepository();
             this.Other1 = new Other1Repository();
        }
    }
    

    在控制器中:

    Repositories Reps;
    
    public HomeController()
    {
         this.Reps= new Repositories();
    }
    

    这是否会影响我的应用程序现在或将来的性能,当应用程序预计会增长时。

    每个存储库都创建自己的数据上下文/实体,因此对于10个存储库,这是10个不同的数据上下文/实体。

    DataContext/Entitie是否是创建数量如此庞大的昂贵对象?

    1 回复  |  直到 16 年前
        1
  •  3
  •   NotDan    16 年前

    您最好只在使用存储库时创建它们,而不是在构造函数中。

    private UsersRepository _usersRepository;
    private  UsersRepository UsersRepository
    {
        get
        {
            if(_usersRepository == null)
            {
                _usersRepository = new UsersRepository();
            }
            return _usersRepository;
        }
    }
    

    然后使用属性而不是字段进行访问。