patterncsharpMinor
The way to realise data access
Viewed 0 times
therealisewaydataaccess
Problem
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:
-
Common repository interface
-
Interface (with particular methods of the current class), class with real implementation of methods. For example
-
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
- ADO .NET Entity Data Model (*.edmx from Entity Framework) with default code generation.
-
Common repository interface
public interface IGenericRepository where T : class
{
T Get(int id);
void Create(T entityToCreate);
void Edit(T entityToEdit);
void Delete(T entityToDelete);
}-
Interface (with particular methods of the current class), class with real implementation of methods. For example
public interface ITestingRepository: IGenericRepository
{
List GetTestingUser(int id);
List GetNotTestingUser(int idTesting, Guid idCompany);
List List(Guid CompanyId);
}
public class TestingRepository : ITestingRepository
{
private TestEverybody _entities = new TestEverybody();
public List 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
}-
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
Solution
Your code seems really good, I would change the
Also, I suggest you would add a method signature to your generic repository interface that would let you query your DbContext.
Then you can use it this way
Finally, you might want to implement a generic repository class that implements your interface (a
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 where T : class
{
T Get(int id);
void Create(T entityToCreate);
void Edit(T entityToEdit);
void Delete(T entityToDelete);
IEnumerable Get(Expression> 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 Get(Expression> whereClause)
{
IQueryable 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 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 : IGenericRepository, 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-inCode Snippets
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)
}//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();
}Context
StackExchange Code Review Q#5364, answer score: 8
Revisions (0)
No revisions yet.