c# - 有没有必要让存储库变得懒惰?

标签 c# entity-framework lazy-evaluation lazy-initialization

我想知道在我的项目中实例化一个新实例需要多长时间,是否有某种工具可以做到这一点?或者只是在打印时间的 ctor 之后放置日志就足够了?

我想知道创建一个新的常规 c# 公共(public)类与通用类(例如 Repository)之间是否存在时间差异,以便决定是否有任何让它变得懒惰的点:

假设我有一项服务,根据请求注入(inject)了我所有应用程序的存储库:

public class DataService
{
   private IRepository<Folder> _folders;
   private IRepository<Folder> _letters;

   // Gets repositories per request
   public DataService(IRepository<Folder> foldersRepo, IRepository<Letter> lettersRepo..........)
   {
       _letters = lettersRepo;
       _folders = foldersRepo;
   }
}

public IRepository<T> where T: BaseEntityObject
{
    void Add(T entity);

    void Remove(T entity);

    List<T> Get();

    T FindById(int id);

    int SaveChanges();

    void Dispose(); 
}


public abstract class EFRepository<T> : IRepository<T>
{
    protected readonly DbContext Context;

    public EFRepository(DbContext context)
    {
        Context = context;
    }

    public abstract List<T> Get();

    public void Add(T item)
    {
        Context.Set<T>().Add(item);
    }

    public void Remove(T item)
    {
        Context.Set<T>().Remove(item);
    }

    public void Update(T item)
    {
        Context.Entry(item).State = EntityState.Modified;
    }

    public void Dispose()
    {
        Context.Dispose();
    }

    public int SaveChanges()
    {
        return Context.SaveChanges();
    }

    public T FindById(int id)
    {
        return Context.Set<T>().Find(id); 
    }
}


public LettersRepository : EFRepository<Letter>
{
    public LettersRepository(DbContext context) : base(context) {}

    // Override for case includes will be needed in future
    public override List<T> Get()
    {
        return Context.Set<T>().ToList();
    }
}

public FoldersRepository : EFRepository<Folder>
{
    public FoldersRepository(DbContext context) : base(context) {}

    public override List<T> Get()
    {
        return Context.Set<T>().Include("Letters").ToList();
    }
}

将这些存储库设置为惰性存储库是否有任何意义 - 意味着它们将在首次使用时实例化?我一直在考虑这个问题,因为我不会在每次请求时都使用所有这些存储库,通常我只使用一个。

最佳答案

I'm interested in knowing how long does it take to instantiate a new instance in my project, is there some kind of tool for doing that?

该工具称为 profiler .

Or just put log after ctor that prints time is good enough?

在绝大多数情况下(即,除非类的构造函数中有大量处理),类实例化是一个如此快速的操作,以至于几乎没有任何意义去打扰它。

I wanna know if there's a difference in time between creating a new regular c# public class vs generic class

没有显着差异。

Is there any point at all for getting those repositories as Lazy - meaning they will be instantiated at first usage? I've been thinking about it since I don't use all of those repositories at each request, usually I use only one.

从性能的角度来看:这将是 premature optimization .别担心,除非您要实例化其中的一百万个。 但是,从软件设计的角度来看,只实例化那些真正需要的存储库确实有意义。您正在有效地减少问题的表面 - 在给定的时间内没有什么需要打破和关心的。

关于c# - 有没有必要让存储库变得懒惰?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35362283/

相关文章:

c# - 我正在处理一个表,其中最后一列是 16368 位数字,在这种情况下,代码失败

c# - 无法查询 EF 对象上名为 "group"的属性

c# - Dapper 无法将类型为 'Microsoft.SqlServer.Types.SqlGeography' 的对象转换为类型 'System.Data.Entity.Spatial.DbGeography'

c++ - 处理 C++ 类中的惰性计算

c# - ThreadPool 线程执行 I/O 操作 - 线程可以在等待时重用吗?

c# - "Open File"对话框默认文件夹位置

c# - Entity Framework MVC 和多个数据库

haskell - 查找对于内存来说太大的列表的大小?

swift - 如果全局属性总是延迟计算,而局部属性从不延迟计算,那么lazy修饰符会修改什么?

c# - 如何在扩展管理器中卸载 Microsoft Web 开发人员工具