Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
On My DataLayer , I have this code

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


Now suppose from my .aspx page I want to acess this code
C#
=============  .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

C#
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
Posted
Comments
jimmson 25-Jan-16 9:42am    
You didn't mentioned what problem or question you have.
Dipak Mandol 25-Jan-16 9:48am    
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

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

C#
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;

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


Usage;

FetchEmailDetails(new OrgDetails());
FetchEmailDetails(new XyzDetails());
 
Share this answer
 
v2
Comments
[no name] 25-Jan-16 11:23am    
Good idea..
Use generic method to fulfil your requirement. Try with below code:
C#
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);
 
Share this answer
 
v3
Comments
PIEBALDconsult 25-Jan-16 10:24am    
But the method should have a constraint on the generic type, and the constraint should probably specify an Interface, at which point this seems to have little benefit over solution 1. Still good, of course, and maybe necessary in some cases.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900