我需要在带有接口的存储库中插入内部联接,我该怎么做?

时间:2018-11-11 15:26:50

标签: asp.net-mvc asp.net-mvc-4

我在模型供应商,发票,InvoiceItem中有树类

//Supplier
namespace TestInvoice.Models
{
    public class Supplier
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public virtual List<Invoice> Invoices { get; set; }
        public virtual List<InvoiceItem> InvoiceItems { get; set; }



    }


//Invoice
 public class Invoice
    {
        public int ID { get; set; }
        [Display(Name = "Document Number")]
        public string DocumentNumber { get; set; }
        [DisplayFormat(DataFormatString = "{0:dd/MM/yy}", ApplyFormatInEditMode = true)]
        public DateTime Date { get; set; }
        [Key]
        [ForeignKey("Supplier")]
        [Display(Name = "Supplier ID")]
        public int SupplierID { get; set; }
        [Display(Name = "Tax Percentage")]
        public decimal TaxPercentage { get; set; }
        [Display(Name = "Total Netto Amount")]
        public decimal TotalNettoAmount { get; set; }
        [Display(Name = "Total Brutto Amount")]
        public decimal TotalBruttoAmount { get; set; }

        public virtual Supplier Supplier { get; set; }
        public virtual InvoiceItem InvoiceItem { get; set; }
    }
}
//Invoice Item
 public class InvoiceItem
    {
        public int ID { get; set; }
        [Key]
        [ForeignKey("Invoice")]
        public int InvoiceID { get; set; }
        public string ArticleNumber { get; set; }
        [DataType(DataType.MultilineText)]
        public string ArticleDescription { get; set; }
       public int Quantity { get; set; }
    public decimal PricePerUnit { get; set; }
    public decimal TaxPercentage { get; set; }
  //  public decimal NettoAmount { get; set; }
  //  public decimal BruttoAmount { get; set; }

        public decimal NettoAmount => Quantity * PricePerUnit;

        public decimal BruttoAmount => ((PricePerUnit * TaxPercentage) / 100 + NettoAmount);

        public virtual Supplier Supplier { get; set; }
        public virtual Invoice Invoice { get; set; }
    }

---即与对象的接口

namespace TestInvoice.Repository
{
  public interface IDataBase
    {//Add Suppliers and GetAll
        IEnumerable<Supplier> GetAll();

        Supplier Add(Supplier item);
        //Add Invoices, GetAllInvoice and delete
        IEnumerable<Invoice> GetAllInvoice();

        Invoice CreateInvoice(Invoice item);
        bool Delete(int id);

        //Add InvoiceItems and GetAllInvoiceItems
        IEnumerable<InvoiceItem> GetAllInvoiceItem();

        InvoiceItem CreateInvoiceItem(InvoiceItem item);
    }
}

以及带有实例类和方法的Repository中的

namespace TestInvoice.Repository
{
    public class DataBase:IDataBase
    {
        private static List<Supplier>suppliers = new List<Supplier>();  
        private static List<Invoice> invoices =new List<Invoice>();
        private static List<InvoiceItem> invoiceItems=new List<InvoiceItem>();

        private static int _nextId = 1;

        public DataBase()
        {

        }

        public Supplier Add(Supplier item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            // TO DO : Code to save record into database 
            item.ID = _nextId++;
            suppliers.Add(item);
            return item;
        }

        public IEnumerable<Supplier> GetAll()
        {
                return suppliers;

            throw new NotImplementedException();
        }
   public Invoice CreateInvoice(Invoice item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            // TO DO : Code to save record into database 
            item.ID = _nextId++;
            invoices.Add(item);
            return item;
        }

        public IEnumerable<Invoice> GetAllInvoice()
        {
            return invoices;

            throw new NotImplementedException();
        }
        public bool Delete(int id)
        {
            // TO DO : Code to remove the records from database 
            invoices.RemoveAll(p => p.ID == id);
            return true;
        }
//Invoice Item
        public InvoiceItem CreateInvoiceItem(InvoiceItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            // TO DO : Code to save record into database 
            item.ID = _nextId++;
            invoiceItems.Add(item);
            return item;
        }

        public IEnumerable<InvoiceItem> GetAllInvoiceItem()
        {
            return invoiceItems;

            throw new NotImplementedException();
        }

我需要通过ID加入内部树模型最终获取供应商及其发票和发票项目,换句话说,需要与此模型关联。 另外,我需要在Controller的“操作方法”中调用此方法。

0 个答案:

没有答案