woggles
woggles

Reputation: 7444

Repository pattern with simple select stored procedure

I have implemented the repository pattern using the following generic interface.

 public interface IRepository<T> where T : class
    {
        IQueryable<T> All { get; }
        IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties);
        T Find(int id);
        void InsertOrUpdate(T updateObject);
        void Delete(int id);
        void Save();
    }

I then create the individual repositories and call them from my service layer.

I have a table in my database that is populated with the last date that one of our data feeds runs (it will only ever contain one record). All I need to do is get this date from the database. What is a good way to do this? Surely creating a repository for this simple read only table is overkill? Should I create a function or stored proc in the database to return this date and if I do how would it fit in with the repository pattern?

Upvotes: 0

Views: 772

Answers (2)

Toodleey
Toodleey

Reputation: 950

If you can use an abstract for the repository definition rather than an interface, you can add the date-code directly to the abstract and have it inherited by all your repositories.

Upvotes: 1

Wouter de Kort
Wouter de Kort

Reputation: 39898

You shouldn't inherit from the general IRepository<T> interface because most of those methods won't make any sense.

But since you already state that you could use a stored procedure, a LINQ query or maybe even esql it would still be a good idea to encapsulate the data retrieval. What if you want to change the mechanism or apply some caching?

Also think about unit testing, your code depends on this 'last date', so in your unit tests you want to be able to supply different dates. A mockable repository will be the easiest.

Maybe you can add the LastModificationDate as a function to your ObjectContext. Then you have on single point of access and in a FakeObjectContext you can return specific dates for testing purposes.

Upvotes: 1

Related Questions