有关此代码的问题 [英] Problem regarding this code

查看:71
本文介绍了有关此代码的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的DataLayer上,我有这个代码



On My DataLayer , I have this code

================= DL ===================
 public DataSet FetchEmailDetails(OrgDetails userSettings)
 {
      //some code goes here          
 }





现在假设从我的.aspx页面我想要访问此代码



Now suppose from my .aspx page I want to acess this code

=============  .aspx.cs ==============
//create object of BL
OrgDetails org = new OrgDetails();
//passing this object in DL 
DataSet dSetEmailDetails = FetchEmailDetails(org);





现在我想要这样的功能





Now I want functionality like this

public DataSet FetchEmailDetails(This_could_be_any_class_from_BL bl)
{
     //some code goes here

}





这样我就可以在任何页面上重用该方法



So that I can reuse that method on any page

推荐答案

只有存在一些可以放在所有的常用函数上才能实现你的潜在BL课程,但你通过界面解决这个问题



This is only going to be possible if there is some common function that you can put on all your potential BL classes, but you solve this with interface

public interface IDetailsRepository
{
    DataSet GetData();
}

public class OrgDetails : IDetailsRepository
{

    public DataSet GetData()
    {
        // code here
        return new DataSet();
    }
}

public class XyzDetails : IDetailsRepository
{

    public DataSet GetData()
    {
        // code here
        return new DataSet();
    }
}





功能;





Function;

public DataSet FetchEmailDetails(IDetailsRepository bl)
{
    DataSet ds = bl.GetData();
    return ds;
}





使用;





Usage;

FetchEmailDetails(new OrgDetails());
FetchEmailDetails(new XyzDetails());


使用通用方法来满足您的要求。尝试使用以下代码:

Use generic method to fulfil your requirement. Try with below code:
static DataSet FetchEmailDetails<t>(T value)
{
	// This generic method returns a List with ten elements initialized.
	// ... It uses a type parameter.
	// ... It uses the "open type" T.
	
	T tempObj = value;
	// Implement your logic to return DataSet
	
	DataSet ds = new DataSet();
	return ds;
}

//create object of BL
OrgDetails org = new OrgDetails();
//passing this object in DL 
DataSet dSetEmailDetails = FetchEmailDetails<orgdetails>(org);

NewClass org1 = new NewClass();
DataSet dSetEmailDetails = FetchEmailDetails<NewClass>(org1);


这篇关于有关此代码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆