Creating a layer of abstraction over the ORM layer

I believe that if you have your repositories use an ORM that it’s already enough abstracted from the database.

However, where I am working now, someone believe that we should have a layer that abstract the ORM in case that we would like to change the ORM later.

Is it really necessary or it’s simply a lot of over head to create a layer that will work on many ORM?

Edit

Just to give more detail:

  1. We have POCO class and Entity Class that are mapped with AutoMapper.
    Entity class are used by the Repository layer. The repository layer
    then use the additional layer of abstraction to communicate with
    Entity Framework.
  2. The business layer has in no way a direct access to Entity Framework. Even without the additional layer of abstraction over the ORM, this one need to use the service layer that user the repository layer. In both case, the business layer is totally separated from the ORM.
  3. The main argument is to be able to change ORM in the future. Since it’s really localized inside the Repository layer, to me, it’s already well separated and I do not see why an additional layer of abstraction is required to have a “quality” code.

9

That way lies madness. It is highly unlikely that you would ever need to change ORMs. And if you ever decide to change the ORM, the cost of rewriting the mappings will be a tiny fraction of the cost to develop and maintain your own meta-ORM. I would expect that you could write a few scripts to do 95% of the work needed to switch ORMs.

In-house frameworks are almost always a disaster. Building one in anticipation of future needs is almost a guaranteed disaster. Successful frameworks are extracted from successful projects, not built ahead of time to meet imaginary needs.

The ORM provides an abstraction for your data layer to be independent of its RDBMS, but it may not be enough to “untie” your business layer from your data layer. Specifically, you should not let objects that map to RDBMS tables to “leak” directly into the business layer.

At the very least, your business layer needs to program to interfaces that your ORM-managed, table-mapped objects from the data layer could potentially implement. Additionally, you may need to create an interface-based layer of abstract query building to hide the native querying capabilities of your ORM. The main goal is to avoid “baking in” any particular ORM into your solution beyond its data layer. For example, it might be tempting to create HQL (Hibernate Query Language) strings in the business layer. However, this seemingly innocent decision would tie your business layer to Hibernate, thus nailing the business and the data access layers together; you should try to avoid this situation as much as possible.

EDIT : In your case, the additional layer inside the repository is a waste of time: based on your point number two, your business layer is sufficiently insulated from your repository. Providing additional insulation would introduce unnecessary complexity, with no additional benefits.

The problem with building an extra abstraction layer inside your repository is that the particular “brand” of ORM dictates the way you interact with it. If you build a thin wrapper that looks like your ORM, but is under your control, replacing the underlying ORM will be roughly as hard as it would be without that additional layer. If, on the other hand, you build a layer that does not look anything like your ORM, then you should question your choice of the object-relational mapping technology.

4

The UnitOfWork usually provides this abstraction. It’s one place that needs to change, your repositories depend on it via an Interface. If you ever need to change O/RM, just implement a new UoW over it. One and done.

BTW it goes beyond just switching O/RM, think of unit testing. I have three UnitOfWork implementations, one for EF, one for NH (because I actually DID have to switch O/RMs mid-project for a client who wanted Oracle support), and one for InMemory persistence. The InMemory persistence was perfect for unit testing or even for rapid prototyping before I was ready to put a database behind it.

The framework is simple to implement. First you have your generic IRepository interface

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface IRepository<T>
where T:class
{
IQueryable<T> FindBy(Expression<Func<T,Bool>>filter);
IQueryable<T> GetAll();
T FindSingle(Expression<Func<T,Bool>> filter);
void Add(T item);
void Remove(T item);
}
</code>
<code>public interface IRepository<T> where T:class { IQueryable<T> FindBy(Expression<Func<T,Bool>>filter); IQueryable<T> GetAll(); T FindSingle(Expression<Func<T,Bool>> filter); void Add(T item); void Remove(T item); } </code>
public interface IRepository<T>
  where T:class
{
  IQueryable<T> FindBy(Expression<Func<T,Bool>>filter);
  IQueryable<T> GetAll();
  T FindSingle(Expression<Func<T,Bool>> filter);
  void Add(T item);
  void Remove(T item);

}

And the IUnitOfWork interface

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface IUnitOfWork
{
IQueryable<T> GetSet<T>();
void Save();
void Add<T>(T item) where T:class;
void Remove<T>(T item) where T:class;
}
</code>
<code>public interface IUnitOfWork { IQueryable<T> GetSet<T>(); void Save(); void Add<T>(T item) where T:class; void Remove<T>(T item) where T:class; } </code>
public interface IUnitOfWork
{
   IQueryable<T> GetSet<T>();
   void Save();
   void Add<T>(T item) where T:class;
   void Remove<T>(T item) where T:class;
}

Next is the base repository (your choice on whether or not it should be abstract

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public abstract class RepositoryBase<T>:IRepository<T>
where T:class
{
protected readonly IUnitOfWork _uow;
protected RepositoryBase(IUnitOfWork uow)
{
_uow=uow;
}
public IQueryable<T> FindBy(Expression<Func<T,Bool>>filter)
{
return _uow.GetSet<T>().Where(filter);
}
public IQueryable<T> GetAll()
{
return _uow.GetSet<T>();
}
public T FindSingle(Expression<Func<T,Bool>> filter)
{
return _uow.GetSet<T>().SingleOrDefault(filter);
}
public void Add(T item)
{
return _uow.Add(item);
}
public void Remove(T item)
{
return _uow.Remove(item);
}
}
</code>
<code>public abstract class RepositoryBase<T>:IRepository<T> where T:class { protected readonly IUnitOfWork _uow; protected RepositoryBase(IUnitOfWork uow) { _uow=uow; } public IQueryable<T> FindBy(Expression<Func<T,Bool>>filter) { return _uow.GetSet<T>().Where(filter); } public IQueryable<T> GetAll() { return _uow.GetSet<T>(); } public T FindSingle(Expression<Func<T,Bool>> filter) { return _uow.GetSet<T>().SingleOrDefault(filter); } public void Add(T item) { return _uow.Add(item); } public void Remove(T item) { return _uow.Remove(item); } } </code>
public abstract class RepositoryBase<T>:IRepository<T>
  where T:class
{
   protected readonly IUnitOfWork _uow;

   protected RepositoryBase(IUnitOfWork uow)
   { 
      _uow=uow;
   }

   public IQueryable<T> FindBy(Expression<Func<T,Bool>>filter)
   {
      return _uow.GetSet<T>().Where(filter);
   }

   public IQueryable<T> GetAll()
   {
      return _uow.GetSet<T>();
   }

   public T FindSingle(Expression<Func<T,Bool>> filter)
   {
      return _uow.GetSet<T>().SingleOrDefault(filter);
   }

   public void Add(T item)
   {
      return _uow.Add(item);
   }

   public void Remove(T item)
   {
      return _uow.Remove(item);
   }
}

Implementing IUnitOfWork is child’s play for EF, NH, and In Memory. The reason I return IQueryable, is for the same reason, Ayende mentioned in his post, the client can further filter, sort, group, and even project the result using LINQ and you still get the benefit of it all being done server side.

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật