无接口的存储库模式
在没有接口的情况下实现存储库模式有什么不好?
Repository -
public class WebRepository<T>
{
private readonly Type persitentType = typeof(T);
public virtual T GetById(int id)
{
return NHibernateSession.Get<T>(id);
}
public virtual List<T> GetAll()
{
return GetByCriteria();
}
public List<T> GetByCriteria(params ICriterion[] criterion)
{
ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
foreach (ICriterion criterium in criterion)
criteria.Add(criterium);
return criteria.List<T>() as List<T>;
}
public T Save(T entity)
{
NHibernateSession.Save(entity);
return entity;
}
public T SaveOrUpdate(T entity)
{
NHibernateSession.Update(entity);
return entity;
}
public void Delete(T entity)
{
NHibernateSession.Delete(entity);
}
private ISession NHibernateSession
{
get
{
return SessionManager.CurrentSession;
}
}
}
如果我们想扩展我们使用的存储库 的类 ProductRepository :存储库和覆盖\扩展方法。
我知道接口让我们:
- 使用 TDD 方法
- 替换持久化引擎
但如果我不想替换我的 nhibernate 并且没有足够的时间来编写测试。那么使用经典存储库模式(使用 IRepository
)的其他优点是什么,
谢谢,Andrew
what's bad to implement repository pattern without interfaces?
Repository - class
public class WebRepository<T>
{
private readonly Type persitentType = typeof(T);
public virtual T GetById(int id)
{
return NHibernateSession.Get<T>(id);
}
public virtual List<T> GetAll()
{
return GetByCriteria();
}
public List<T> GetByCriteria(params ICriterion[] criterion)
{
ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
foreach (ICriterion criterium in criterion)
criteria.Add(criterium);
return criteria.List<T>() as List<T>;
}
public T Save(T entity)
{
NHibernateSession.Save(entity);
return entity;
}
public T SaveOrUpdate(T entity)
{
NHibernateSession.Update(entity);
return entity;
}
public void Delete(T entity)
{
NHibernateSession.Delete(entity);
}
private ISession NHibernateSession
{
get
{
return SessionManager.CurrentSession;
}
}
}
if we would like extend repository we use
ProductRepository : Repository and overrider\extend methods.
I know interfaces let us:
- Use TDD methods
- Replace persistance engine
But if I don't want replace my nhibernate and haven't enough time to write tests. So what's the other advantages to use classic repository pattern (with IRepository<T>, IProductRepository
)
Thanks, Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在没有接口的情况下实现存储库模式没有什么坏,这取决于您是否认为需要使用接口。
正如您所说,使用接口的充分理由是使持久层从业务逻辑层中抽象出来,当然也是为了更容易测试。但是,如果您可以保证,您将不会更改后端(或者至少您不能预见它在不久的将来发生变化)并且您不打算编写测试(大错误)那么您可能不需要使用接口。
我看到的危险信号是“没有足够的时间来编写测试”。现在可能是这样,但是,将来有时间的时候怎么办?再说一次,这是你的决定,但是,如果我是你,我会使用接口(即使你根本没有编写测试),因为它不会对你的代码造成任何损害,也不会花费太多时间来这样做,并且如果您确实决定切换后端或编写测试,那么将来可以为您省去很多麻烦。
There is nothing bad about implementing the repository pattern without interfaces, it's up to you whether you feel you need to use interfaces or not.
Like you have stated good reasons to use interfaces is to keep your persistance layer abstract from your business logic layer and of course for easier testability. However, if you can guarentee you won't change your backend (or at least you can't forsee it changing in the near future) and you don't intend to write tests (big mistake) then there is probably no need for you to use interfaces.
The red flag I see though is "haven't enough time to write tests". This may be the case now, however, what about in the future when you do have the time? Again, this is your decision, however, if I were you I would use interfaces (even if you didn't write tests at all) as it won't do your code any harm, nor take up that much time to do so and save you a lot of hassle in the future if you ever did decide to switch your backend or write tests.
嘲笑!
您的数据库可能由于某种原因无法访问,通过使用界面,您可以非常轻松地在真实数据和虚拟数据之间切换。
Mocking!
Your database may be inaccessible for some reason, and by using interface you can very easily switch between real data and dummy data.