0

I know there are a lot of Repository Pattern questions out there but I hope someone will be kind enough to help me understand some principles.

I always see the repository class constructor using a context and this context is often EF or NHibernate. Its then easy to use the dbset<T> for CRUD methods. Since I do not use anything like this, but rather make calls to a non-sql data source through API calls, I can't figure out how to use a Repository<T> and a method like T GetById(int id) when there is no underlying context that knows how to translate <T> to the right obect. Do I have to create a custom context and my own data mapping? Will I end up having a specific Repository for every class I have? Somehow, somewhere, I need to take that <T> and know what it is in order create it and fill in its properties.

I've seen some examples where the Repository Pattern is used with a File and it looks like each type has it own repository where the call and the mapping is done.

Any examples of Repository Pattern that targets multiple data sources?

4

1 回答 1

0

The Repository Pattern is a concept independent of one or more data sources, one of the purposes being to abstract all persistence related. I think you want an example of a repository implementation where more than one persistence types are used. That's very easy as a repository (as an implementation) is just a class which has nothing to do with Ef or whatever.

In fact, most of the examples you've seen are 'wrong' because just wrapping a Db or a NH context without anything else, it doesn't do anything.

The point is the repository uses all the data sources it needs. It might be only one (most common case) or it may be 2-3.Let's say that a file is uploaded and you want to save the file on the file system (or send it on a cloud?) and store some metadata in the local db.

The repository will take as dependency a DAO (like EF context) to talk with the local db and it access directly the file system. If the file is to be uploaded to the cloud, the repository will also take a dependency on the cloud provider.

There's nothing complex here, the Repository just uses the db and the cloud while hiding them from the rest of the app. The app doesn't care where and how you'll store that file. So when you are writing the Repository class you can do whatever you want as long as you respect the contract (interface).

于 2013-02-08T15:47:38.373 回答