模型类(MVC)中c#中的接口类

时间:2011-11-25 11:46:08

标签: c# model-view-controller interface

您能否在下面的代码中帮助我,无论接口和类是否结构合理?

我的意思是说,我需要在loaddata()接口中使用单个方法, 但我有2种不同的方法(返回列表),以满足2个不同的表。

类EmployeeDetails和JobDetails是只读类,数据来自外部数据库。

public interface IGetData
{
  public List<EmployeeDetails> LoadData{get;set;}
  public List<JobDetails> LoadData2{get;set;}
}

public  class EmployeeDetails : IGetData
{             
  public int EmployeeID { get; set; }
  public string EmployeeNumber { get; set; }
  public string EmployeePosition { get; set; }
  public string EmployeeDivision { get; set; }
  public string EmployeeName { get; set; }
  public string ApproverName { get; set; }
  public int ApproverID { get; set; }

  public EmployeeDetails()
  {
    public string LoggedInEmployeeName
    {
      get
      { 
        return System.Web.HttpContext.Current.User.Identity.Name;
      }
    }

    STASEntities STASContext = new STASEntities();

    public  List<EmployeeDetails> LoadData
    { 
      get
      {
        var EmployeeResults = from e in STASContext.Employee_VIEW
                              where
                              e.UserName == LoggedInEmployeeName 
                              select new EmployeeDetails
                              {
                                EmployeeDivision    = e.BusinessUnitName,
                                EmployeeID          = e.EmployeeId,
                                EmployeePosition    = " ",
                                EmployeeName        = e.EmployeeName,
                                ApproverID          =   e.EmpAppId,
                                ApproverName        =  e.ApproverName,
                                EmployeeNumber      = e.EmployeeNumber
                              };
        return EmployeeResults.ToList();  
      }
    }
  }
}

public class JobDetails:EmployeeDetails, IGetData//for future employee assigned against job.
{
  //  public IList<ContractDetails_View> JobNameList { get; set; }
  public int ContractID { get; set; }
  public string JobName { get; set; }
  public JobDetails() {
  public  List<JobDetails> LoadData2
  { 
    get
    {
      var jobResult = from c in STASContext.ContractDetails_View 
                      select new JobDetails
                      {                    
                        JobName=c.JobName,
                         ContractID = c.ContractId
                      };

      return jobResult.ToList();
    }
  }
}

1 个答案:

答案 0 :(得分:1)

在不了解您的代码的情况下,这将是一个更多的猜测。但你的界面看起来不对劲。它看起来不对,因为EmplyeeeDetails类不调用LoadData2函数,JobDetails不调用LoadData函数。

看起来你应该有两个独立的接口(它们看起来更像是属性的函数):

public interface IGetEmployeeDetails
{
  List<EmployeeDetails> LoadEmployeeDetails();
}

public interface IGetJobDetails
{
  List<JobDetails> LoadJobDetails();
}

然后单独实施:

public  class EmployeeDetails : IGetEmployeeDetails
{
  //class stuff
}

public class JobDetails : EmployeeDetails, IGetJobDetails
{
  //class stuff
}

因为JobDetails继承了EmployeeDetails并实现了IGetJobDetails,所以它可以使用两个接口调用LoadEmployeeDetails()LoadJobDetails()

您可以使用通用版本替换这两个接口:

public interface IGetDetails<T> {
  List<T> LoadData();
}

除此之外,您需要指定实际问题。