Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I using IRepository correctly?

I'm looking to use the IRepository pattern (backed by NHibernate, if it matters) in a small project. The domain is a simple one, intentionally so to allow me to focus on understanding the IRepository pattern. The lone domain class is Movie, with properties for Year, Genre, and Title. My intent would be to "get" movies whose properties match criteria of the aforementioned types.

Convention seems to be to have a generic IRepository interface, similar to the following:

public interface IRepository<T>
{
    T Get(int id);
    T[] GetAll();
    void Add(T item);
    void Update(T item);
    void Delete(T item);
}

With a base implementation:

public abstract class Repository<T> : IRepository<T>
{
    public T Get(int id) { ... }
    public T[] GetAll() { ... }
    public void Add(T item) { ... }
    public void Update(T item) { ... }
    public void Delete(T item) { ... }
}

Then to have a domain-specific interface:

public interface IMovieRepository
{
    Movie[] GetByGenre(Genre genre);
    Movie[] GetByYear(int year);
    Movie[] GetByTitle(string title);
}

With an implementation that also extends the base Repository class:

public class MovieRepository : Repository<Movie>, IMovieRepository
{
    public Movie[] GetByGenre(Genre genre) { ... }
    public Movie[] GetByYear(int year) { ... }
    public Movie[] GetByTitle(string title) { ... }
}

I would need to add necessary implementation to the base class as well as the concrete one, using NHibernate, but I would like to know if I am on the right track with this setup.

There seems to be a fair bit of overhead for just one domain class, though it would be less noticeable if there were multiple domain classes involved. Right now I'm trying to keep it simple so I can pin down the concept.

like image 237
Grant Palin Avatar asked Jul 31 '10 19:07

Grant Palin


People also ask

How do I know which Git repository I am on?

You can inspect a Git repository by using the git status command. This command allows you to see which changes have been staged, which haven't, and which files aren't being tracked by Git.

How do I know if I have access to Git repository?

how can i check write access to a git repository, if i do have a clone of it? A very easy way to check is whether you see an edit 'pencil' icon in the top right of the README.MD on the main Code page of the repo (scroll down to it if there's a long list of top level files/folders).

Do you have to clone repository every time?

When you clone a repository, you don't get one file, like you may in other centralized version control systems. By cloning with Git, you get the entire repository - all files, all branches, and all commits. Cloning a repository is typically only done once, at the beginning of your interaction with a project.

How do you please make sure you have the correct access rights and the repository exists?

The “Please make sure you have the correct access rights” error occurs if you do not have the right permissions to access a Git repository. To solve this error, make sure you are referring to the correct remote URL and that you have set up SSH authentication correctly.


1 Answers

  1. try not to pass back an array. use IEnumerable<T>, ICollection<T> or IList<T>, this will loosely couple your code further.

  2. your IMovieRepository interface. this repository includes the CRUD. therefore make it

IMovieRepository : IRepository<Movie> {}

This will not change your MovieRepository class as that will implement the interface correctly. it will allow you to decouple your classes if you wish to change the implementation at a later date.

finally. this is fine for one of the methods. as you have specialised functionality you have specialised the repository to suit.

there are other ways, which enable you to use 1 repositry class and pass in the required query. This is called the Specification pattern. I did a project which uses this located on codeplex with report http://whiteboardchat.codeplex.com

the other way would to to have a method to pass in the criteria. there is a open source project called Sharp Architecture, which i believe has this coded up.

Hope this helps

like image 88
dbones Avatar answered Oct 10 '22 13:10

dbones