我已经创建了一个非常基本的存储库模式,我想包括一个干净的方式来加载相关数据。我已经看到人们使用.Include()以前,但我不是100%确定如何将这个介绍给我的解决方案。如何在存储库模式中加载相关数据
这是到目前为止我的仓库:
/Repository/IRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyProject.Repository
{
public interface IRepository<T> where T : class
{
IEnumerable<T> GetAll();
T GetById(object Id);
void Insert(T obj);
void Update(T obj);
void Delete(Object Id);
void Save();
}
}
/Repository/Repository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SeaBay.Models;
using System.Data.Entity;
namespace MyProject.Repository
{
public class Repository<T> : IRepository<T> where T : class
{
private myEntities db;
private DbSet<T> dbSet;
public Repository()
{
db = new myEntities();
dbSet = db.Set<T>();
}
public IEnumerable<T> GetAll()
{
return dbSet;
}
public T GetById(object Id)
{
return dbSet.Find(Id);
}
//... etc etc
}
基本控制器
public class MyController : Controller
{
private IRepository<entity> _repository = null;
public MyController()
{
this._repository = new Repository<entity>();
}
public ActionResult Index()
{
var mydata = _repository.GetAll();
return View(mydata);
}
public ActionResult Details(int Id)
{
var mydata = _repository.GetById(Id);
return View(mydata);
}
}
可以说例如我有两张数据表'Student'和'Classes',假如IRepository使用'Student'作为源代码,我将如何在存储库模式中返回这个相关数据?