控制器IRepository问题

时间:2014-08-10 22:40:59

标签: c# asp.net-mvc entity-framework repository-pattern generic-programming

我正在学习c#和asp.net mvc 5.我已经实现了一个通用的Repository IRepository,并且有一个实现名为ReporterReprository的存储库的Generic Class。我有三个实体(将它们称为P,I,C)。以下是我的实体代码:

public class P
{
    [Key]
    public int PId { get; set; }
    public int CId { get; set; }
    public C C { get; set; }

    [Display(Name="Policy Number")]
    public string PNumber { get; set; }
    public int IId { get; set; }
    public I I { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString="{0:d}")]
    public DateTime PDate { get; set; }

    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString="{0 : C}")]
    public decimal PValue { get; set; }

    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0 : C}")]
    public decimal PrePaid { get; set; }

    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0 : C}")]
    public decimal Com { get; set; }
}
public class C
{
    public int CId { get; set; }
    public string ClientName { get; set; }
    public virtual ICollection<P> Ps { get; set; }

}
public class I
{
    public int IId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<P> Ps { get; set; }
}

这是我的存储库代码:

public interface IRepository<Tentity> : IDisposable where Tentity : class
{
    //the common behaviours that will be found among all my entities
    void Add(Tentity t);
    void Delete(Tentity t);
    void AddOrUpdate(Tentity t);
    void Commit();
    IQueryable<Tentity> Query (Expression<Func<Tentity, bool>> predicate);
    IEnumerable<Tentity> GetAll();
    Tentity GetById(int id);

    void Dispose();
}

public class ReporterRepository<T> : IRepository<T> where T : class
{
    private DbContext db;
    private DbSet set { get; set; }

    public ReporterRepository(DbContext dbParam)
    {
        db = dbParam;
        set = db.Set<T>();
    }

    public void Add(T t)
    {
        set.Add(t);
    }

    public void Delete(T t)
    {
        set.Remove(t);
    }

    public void AddOrUpdate(T t)
    { //check the state of the entity to see if it is being tracked by context which means its been modified
        if(db.Entry(t).State == EntityState.Detached)
        {
            db.Set<T>().Attach(t);
            db.Entry(t).State = EntityState.Modified;
        }
    }

    public IQueryable<T> Query(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
        return db.Set<T>().Where(predicate);
    }

    public IEnumerable<T> GetAll()
    {
        return db.Set<T>();
    }

    public T GetById(int id)
    {
        return db.Set<T>().Find(id);
    }

    public void Commit()
    {
        db.SaveChanges();
    }

    public void Dispose()
    {
        if(db != null )
            db.Dispose();
    }
}

现在一切都可以为I,C和P创建控制器。因为我和C有1:N的关系。一个创建方法是我的问题。你能帮我理解我应该怎么做吗?这是方法中的代码。我想将其更改为使用我的存储库。

// GET: Policies/Create
public ActionResult Create()
{
    //ViewBag.CId = new SelectList(db.Cs, "CId", "CName");
    //ViewBag.IId = new SelectList(db.Is, "IId", "Name");
    IRepository<Client> client;

    ViewBag.CId = new SelectList(c.GetAll(), "CId", "CName");
    return View();
}

0 个答案:

没有答案