Blankman
Blankman

Reputation: 267140

Advice on isolating my nhibernate layer such that I could swap it out with EF potentially

Ok it seems my project setup could use some improvments.

I currently have:

1. ASP.NET MVC3 Web project
2. NHibernate project with Repositories/Mappings and some session code.
3. Entities (models used in nhibernate like User.cs)
4. Interfaces (like IUser, IRepository<IUser>, IUserRepository...)
5. Common (UserService, ..)

Now the issue is that I my nhibernate models now need to implement IUser, which I don't like, but I was forced to do this since my IRepository is generic, and I could use IRepository<User> since User is in another project, so I had to create an interface and do IRepository<IUser>

I will never need to have another implemention of User, so this is bugging me.

How can I fix this while keeping things seperate so I can swap out my ORM?

Upvotes: 2

Views: 318

Answers (2)

Dmitry
Dmitry

Reputation: 17350

I think you should approach this problem from Domain Driven Design perspective. Domain should be persistent-ignorant. Proper implementation of DDD repository is a key here. Repository interface is specific, business-focused, not generic. Repository implementation encapsulates all the data access technicalities (ORM). Please take a look a this answer and these 2 articles:

Your entities should be concrete types, not interfaces. Although you may never need to swap your ORM (as Ladislav is saying in comments), you should design it as if you will need to swap it. This mindset will really help you achieve persistence ignorance.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039080

The IUser interface must be defined in the Entities layer if your entities implement it, not in the Interfaces layer. Also I would probably rename this generic Interfaces layer to Repositories or AbstractRepositories or something. Also I would rename the Common layer to Services if it contains services aggregating your repositories.

So the picture could be:

  1. ASP.NET MVC3 Web project
  2. NHibernate project with Repositories/Mappings and some session code.
  3. Domain Entities (models used in nhibernate like User.cs and implementing domain interfaces like IUser)
  4. Repositories (like IRepository<IUser>, IUserRepository...)
  5. Services (UserService, ..)

Upvotes: 1

Related Questions