有没有必要让存储库变得懒惰?
Is there a point to make repository lazy?
我想知道在我的项目中实例化一个新实例需要多长时间,是否有某种工具可以做到这一点?或者只是在打印时间的 ctor 之后放置日志就足够了?
我想知道创建新的常规 c# public class 与通用 class(例如存储库)之间是否存在时间差异,以便确定是否有任何意义一切都是为了让它变得懒惰:
假设我有一项服务,根据请求注入了我所有应用程序的存储库:
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();
}
}
将这些存储库设置为 Lazy 是否有任何意义 - 这意味着它们将在首次使用时实例化?我一直在考虑它,因为我不会在每次请求时都使用所有这些存储库,通常我只使用一个。
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?
Class 实例化在绝大多数情况下(即,除非 class 的构造函数中有大量处理),如此快速的操作几乎没有任何意义那。
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。别担心,除非您要实例化其中的一百万个。 但是,从软件设计的角度来看,只实例化那些真正需要的存储库是有意义的。你有效地减少了问题的表面——在给定的时间没有什么需要打破和关心的。
如果您对如何使注入 属性 惰性感兴趣 - 一些 DI 容器支持,例如:https://github.com/ninject/Ninject.Extensions.Factory/wiki/Lazy
您的存储库的创建成本很低。原因 DbContext
成本是 relatively low too:
Not very much happens when the context instance is created. The
initialization is mostly lazy so that if you never use the instance,
then you pay very little cost for creating the instance.
每个请求上下文也是一种常见的做法。
我想知道在我的项目中实例化一个新实例需要多长时间,是否有某种工具可以做到这一点?或者只是在打印时间的 ctor 之后放置日志就足够了?
我想知道创建新的常规 c# public class 与通用 class(例如存储库)之间是否存在时间差异,以便确定是否有任何意义一切都是为了让它变得懒惰:
假设我有一项服务,根据请求注入了我所有应用程序的存储库:
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();
}
}
将这些存储库设置为 Lazy 是否有任何意义 - 这意味着它们将在首次使用时实例化?我一直在考虑它,因为我不会在每次请求时都使用所有这些存储库,通常我只使用一个。
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?
Class 实例化在绝大多数情况下(即,除非 class 的构造函数中有大量处理),如此快速的操作几乎没有任何意义那。
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。别担心,除非您要实例化其中的一百万个。 但是,从软件设计的角度来看,只实例化那些真正需要的存储库是有意义的。你有效地减少了问题的表面——在给定的时间没有什么需要打破和关心的。
如果您对如何使注入 属性 惰性感兴趣 - 一些 DI 容器支持,例如:https://github.com/ninject/Ninject.Extensions.Factory/wiki/Lazy
您的存储库的创建成本很低。原因 DbContext
成本是 relatively low too:
Not very much happens when the context instance is created. The initialization is mostly lazy so that if you never use the instance, then you pay very little cost for creating the instance.
每个请求上下文也是一种常见的做法。