开发者

Can C# Delegates work this way?

开发者 https://www.devze.com 2023-02-08 04:45 出处:网络
I\'m trying to use delegates to cut down on my code in this project. I have 4 DropDownLists in my asp.net page. In my codebehind file I\'m binding them to different business object calls with data. Ri

I'm trying to use delegates to cut down on my code in this project. I have 4 DropDownLists in my asp.net page. In my codebehind file I'm binding them to different business object calls with data. Right now I have the following code:

DeptList.DataSource = bl.getAcademicDepts();
DeptList.DataBind();
TermList.DataSource = bl.getTerms();
TermList.DataBind();
InstructorList.DataSource = bl.getInstructors();
InstructorList.DataBind();
EEList.DataSource = bl.getEE();
EEList.DataBind();

This seems really repetitive so I decided to make a function as a shortcut

private void SourceAndBind(DropDownList d, <business layer method call>)
{
    d.DataSource = <businesslayer method call>();
    d.DataBind();
}

Then my first block of code simply becomes

SourceAndBind(DeptList, bl.getAcademicDepts());
SourceAndBind(TermList, bl.getTerms());
SourceAndBind(InstructorList, bl.getInstructors());
SourceAndBind(EEList, bl.getEE());

However, I don't know what to put for the second parameter. Each one of the business layer calls takes no parameters, but they each return objects of different types. I tried using delegates but I couldn't figure out how to create one without a defined return type or no parameters. Is it possible to achieve开发者_如何转开发 what I want with c#? I know that works in python which is where I'm coming from.


You don't need delegates to do this. Just declare the second parameter as object.

// Takes drop down list and data to assign to 'data source'
private void SourceAndBind(DropDownList d, object data) {
    d.DataSource = data;
    d.DataBind();
}

// Call methods from bussiness layer and bind results
SourceAndBind(DeptList, bl.getAcademicDepts());
SourceAndBind(TermList, bl.getTerms());
SourceAndBind(InstructorList, bl.getInstructors());
SourceAndBind(EEList, bl.getEE()); 

You could use delegates too. However, since you're only calling the method once, you can call the bussiness layer method to get the data and then pass the result to SourceAndBind. (Delegates would be useful for example if you wanted to choose one of several ways of loading the data, or if you wanted to delay loading until some later point).


Well, Func<object> would be a very general way of doing that. That's "a function with no parameters that returns an object". Any parameterless function returning a reference type should be convertible to that delegate type. However, your "usage" code wouldn't be quite right as it. It would be:

SourceAndBind(DeptList, bl.getAcademicDepts);
SourceAndBind(TermList, bl.getTerms);
SourceAndBind(InstructorList, bl.getInstructors);
SourceAndBind(EEList, bl.getEE);

Note the lack of brackets, which means these are method groups rather than method calls. (To follow .NET naming conventions I'd suggest renaming your methods to start with capital letters, btw.)

That's appropriate if you only want to call the method conditionally. As Tomas says though, you don't need to use delegates here. If you're happy for SourceAndBind to only get called after you've called the method, you can definitely just perform the method call in the argument and pass the result as object.


private void SourceAndBind(DropDownList d, Func<IEnumerable<object>> businessLayerMethod)
{
    d.DataSource = businessLayerMethod();
    d.DataBind();
}

IEnumerable<object> where object is your datatype.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号