views:

394

answers:

1

Hello all,

I have this:

public interface IRepository<T> where T : class
{
    void Delete(T entity);
    void Add(T entity);
    void Attach(T entity);
    void Detach(T entity);
    void SaveChanges();
}

now for every of my Entity I make concrete classes implementing the generic IRepository =>

public class SchoolclassRepository : IRepository<Schoolclass>
{
    public void Delete(Schoolclass entity)
    {
        throw new NotImplementedException();
    }

    public void Add(Schoolclass entity)
    {
        throw new NotImplementedException();
    }

    public void Attach(Schoolclass entity)
    {
        throw new NotImplementedException();
    }

    public void Detach(Schoolclass entity)
    {
        throw new NotImplementedException();
    }

    public void SaveChanges()
    {
        throw new NotImplementedException();
    }
}

In my ViewModel`s constructor (mvvm pattern) I do this =>

IRepository<Schoolclass> repo = new SchoolclassRepository();

What advantage is there with IRepository when I have anyway to write the code for my CRUD operations in every entities class?

In Customer, Product, Pupil whatever class I implement the IRepository<Product> , IRepository<Customer>, IRepository<Pupil> etc... and I implement the methods of the interface.

Why could I not say =>

SchoolclassRepository repo = new SchoolclassRepository(); ??? 

I do not care for having the possibility to write unit tests for a small app.

+3  A: 

The repository pattern is based around IoC and dependency injection patterns, unit test just happen to need IoC and dependency injection to make things easier to test. It was not originally intended to be another way to write the data access layer, although many post and implement it as such. For small apps it depends on how much effort you want to put in.

Typically the implemenation SchoolclassRepository will be in a separate namespace from the IRepository interfaces so you can support multiple implementations. (Not a rule)

You can set your ViewModels constructor (mvvm pattern) to take a parameter for the repository interface IRepository<Schoolclass>. Now your ViewModels constructor is based on the interface and not the implementation.

private IRepository<Schoolclass> _repository
public ViewModel(IRepository<Schoolclass> Repository) 
{ this._repository = Repository; }

Why do the above ....

The pattern also makes future changes easier to make.

If your implementation of SchoolclassRepository() used ADO.NET (sqlconnections, sqlcommands...) to access data, you can later build another SchoolclassRepository() that uses NHibernate, Entity Framework or some other data access method. Now all you have to do is change your consumers to use the targetted implementation and inject them into the ViewModel.

Repository repository = new ADONET.SchoolclassRepository(); 
or
Repository repository = new EntityFramework.SchoolclassRepository(); 
ViewModel viewmodel = new ViewModel(repository);

This is just q quick view into the uses, I would recommend further study into the repository pattern and how to make it work for you. You are obviously interested in it which it good.

Hello longday,I am using EF 4.0 RC. What is your opinion considering this concerning repository pattern and my origion question?
msfanboy
quote: “One thing I’ll suggest is that with a new feature set (.NET 3.5) comes some new ways of doing things.” source: http://weblogs.asp.net/fredriknormen/archive/2008/04/24/what-purpose-does-the-repository-pattern-have.aspx
msfanboy
ok found my answer a bit: http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspxbut why pass the VM`s constructor the IRepo<T> object? why not do it this way see above: IRepository<Schoolclass> repo = new SchoolclassRepository();
msfanboy
You can have the viewmodel use two overloaded constructors, one parameterless that sets the IRepository<Schoolclass> repo = new SchoolclassRepository() by default like you suggest and another with IRepository<Schoolclass> parameter. This allows injection if needed but has a default if not passed IRepository.
Will my ViewModel/IRepo approach not work as good as your approach injecting the IRepo via constructor parameter? Please tell me the advantages/disadvantages.
msfanboy
It will "work" just as equal.
@longday You have told me much even made a compliment ;-) But actually the original question is not answered. I will rephrase it:"concrete sense of IRepository<T> vs Repository if I do not do unit tests with mocks". Do You or anyone else has a good answer on that?
msfanboy
Your rephrase is the same and does not ask an actual question or make a complete sentence. Are you asking why use an interface vs its implementation?
Yes this I do ;-)
msfanboy
I actually do not like the IRepository because I have to implement many Properties/Methods from the interface into Repository classes which are not needed by those classes. For example I need only a detach method for a special entity so every other lets say 20 entities have an empty public void detach(T entity) method body in its class. That is going to be a big waste of code and overview.better => ? see next comment
msfanboy
public class EntityBase<T> where T : class { public void Delete(T entity) { // context.DeleteObject(entity); // context.SaveChanges(); } } public class SchoolclassRepository : EntityBase<Schoolclass> { public void DeleteSchoolclass(Schoolclass entity) { base.Delete(entity); } }That way I call and define in my Repo classes only those methods I need. Class is not abstract else I could not call the base method... I still could make the Base class with private constructor , none can instantiate
msfanboy