在DAL BLL设置中访问和填充另一个类的属性

时间:2018-04-13 09:07:37

标签: c# asp.net data-access-layer business-logic-layer

我想在BLLServiceRequests DALServiceRequest属性及其对应的DALRequest属性中返回,因为我使用的SP是Request和{{之间的JOIN表1}}表。问题是当我点击这段代码时:

  

var requestNewProjects =   Common.ToList(DT);

找不到我在ServiceRequest的属性中设置的DALRequest属性。有人能指出我实现这个的正确方法吗?谢谢。

BLL

DALServiceRequest

DAL

public class BLLServiceRequests
{

    public List<DALServiceRequests.Properties> GetServiceRequestByRequestId(intrequestID)
    {
        var obj = new DALServiceRequests();
        return obj.GetServiceRequestByRequestId(requestID);
    }
}

这是我在DAL中的Common类。这里没什么可看的,它只是我用来将数据表转换为列表的实用程序类

public class DALRequests
{
    public class Properties
    {
        public int id {get;set;}
        public int requestType { get; set; }
        public DateTime createDate { get; set; }
    }
}

public class DALServiceRequests
{
    public class Properties
    {
        public int id { get; set; }
        public int requestId { get; set; }
        public string Issue { get; set; }
        public DALRequests.Properties request { get; set; } //Is this correct?
    }

    public List<DALServiceRequests.Properties> GetServiceRequestByRequestId(int requestID)
    {
        string cnStr = Common.dbConnStr;

        SqlConnection cn = new SqlConnection(cnStr);
        SqlCommand cmd = new SqlCommand();
        DataTable dt = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter();

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetServiceRequestByRequestId";
        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@requestID", requestID);

        try
        {
            cn.Open();
            cmd.Connection = cn;
            adapter.SelectCommand = cmd;
            adapter.Fill(dt);

            var requestNewProjects = Common.ToList<DALServiceRequests.Properties>(dt);
            //Code above can't find the DALRequests properties which is already defined in DALServiceRequests.Properties

            return requestNewProjects;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cn.Close();
            cn.Dispose();
            cmd.Dispose();
            dt.Dispose();
            adapter.Dispose();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

为此,您可以使用inheritance

public class DALServiceRequests:DALRequests

如果您将DALRequests添加到DALServiceRequests,它会让您访问DALRequests内的所有属性,包括所有方法,但我认为您只有DALRequests中的属性< / p>

另一种方法是将类定义为属性,就像之前一样,但没有.properties

public DALRequests request { get; set; }

然后您可以通过以下方式访问属性:

DALServiceRequests req = new DALServiceRequests(); 
var somePropInfo = req.DALRequests.createDate;// Fill it correcly in line above