首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个DataContext/EntitiesObject

多个DataContext/EntitiesObject
EN

Stack Overflow用户
提问于 2009-10-30 04:39:06
回答 1查看 175关注 0票数 0

我已经在我的应用程序中实现了存储库模式,并且在我的一些控制器中,我使用了各种不同的存储库。(未实现IoC )

代码语言:javascript
复制
UsersRepository Users;
OtherRepository Other;
Other1Repository Other1;   

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

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

代码语言:javascript
复制
public class Repositories
{
    UsersRepository Users;
    OtherRepository Other;
    Other1Repository Other1;

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

在控制器中:

代码语言:javascript
复制
Repositories Reps;

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

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

每个存储库创建自己的DataContext/实体,因此对于10个存储库,即10个不同的DataContext/实体。

创建如此多的DataContext/Entitie是一个昂贵的对象吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-10-30 04:42:47

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

代码语言:javascript
复制
private UsersRepository _usersRepository;
private  UsersRepository UsersRepository
{
    get
    {
        if(_usersRepository == null)
        {
            _usersRepository = new UsersRepository();
        }
        return _usersRepository;
    }
}

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1646402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档