6
\$\begingroup\$

There are a several articles about true data access in MVC. I tried to make the best one according to the architecture flexibility. My way is following:

  1. ADO .NET Entity Data Model (*.edmx from Entity Framework) with default code generation.
  2. Common repository interface

     public interface IGenericRepository<T> where T : class
     {
         T Get(int id);
         void Create(T entityToCreate);
         void Edit(T entityToEdit);
         void Delete(T entityToDelete);
     }
    
  3. Interface (with particular methods of the current class), class with real implementation of methods. For example

    public interface ITestingRepository: IGenericRepository<Testing>
    {
        List<dynamic> GetTestingUser(int id);
        List<dynamic> GetNotTestingUser(int idTesting, Guid idCompany);
        List<Testing> List(Guid CompanyId);
    }
    
    public class TestingRepository : ITestingRepository
    {
       private TestEverybody _entities = new TestEverybody();
    
       public List<Testing> List(Guid CompanyId)
       {
           return _entities.Testing.Where(t => t.idCompany == CompanyId).ToList();
       }
       public void Edit(Testing productToEdit)
       {
           var originalProduct = Get(productToEdit.id);
           _entities.ApplyCurrentValues(originalProduct.EntityKey.EntitySetName, productToEdit);
           _entities.SaveChanges();
       }
       public void Create(Testing productToCreate)
       {
           _entities.Testing.AddObject(productToCreate);
           _entities.SaveChanges();
       }
       //other methods
    }
    
  4. The following data access method in the controller

     public class TestingController : Controller
     {
       //
       // GET: /Testing/
       private ITestingRepository _repository;
       public TestingController() : this(new TestingRepository()) { }
    
       public TestingController(ITestingRepository repository)
       {
          _repository = repository;
       }
       [HttpPost]
       public ActionResult Create(Testing newobject)
       {
           _repository.Create(newobject);            
           return RedirectToAction("Index");
       }
       [HttpPost]
       public ActionResult Edit(Testing item)
       {
          _repository.Edit(item);
          return RedirectToAction("Index");
       }
     }
    

Is it a good one? Have I any mistakes there?

\$\endgroup\$
8
\$\begingroup\$

Your code seems really good, I would change the ITestingRepository methods to return IEnumerable<> instead of List<>.

Also, I suggest you would add a method signature to your generic repository interface that would let you query your DbContext.

public interface IGenericRepository<T> where T : class
{
    T Get(int id);
    void Create(T entityToCreate);
    void Edit(T entityToEdit);
    void Delete(T entityToDelete);
    IEnumerable<T> Get(Expression<Func<T,bool>> whereClause)
}

Then you can use it this way

//Class example, I don't know what Testing class is
public class Testing
{
    public string PropertyOne { get; set;}
    public string PropertyTwo { get; set;}
}

public class TestingRepository
{
    //Method implementation
    public IEnumerable<Testing> Get(Expression<Func<Testing,bool>> whereClause)
    {
        IQueryable<Testing> result = _entities.Testing;

        if(whereClause != null)
            return result.Where(whereClause);

        return result.ToList();
    }
}

public static void AMethod() //Just to show an example at work!
{
    List<Testing> testings = new TestingRepository().Get(x => x.PropertyOne == "Hello" && x.PropertyTwo == "World").ToList();
}

Finally, you might want to implement a generic repository class that implements your interface (a public class GenericRepository<T> : IGenericRepository<T>, I won't write it down for you but you could check this http://www.codeproject.com/Articles/640294/Learning-MVC-Part-Generic-Repository-Pattern-in

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.