0

I want to create column validator where I can pass custom functions/methods.

Edit: I don't need to dynamically build these functions it is possible to build solution before hand, and I am constrained to .net 3.5. Basically I'm trying to create reusable class for my SSIS Script processes.

I did not know how to do that in C# so I created a fast representation in JavaScript of what I want to achieve here

// Data extracted from file
var data = "2|User2|15\r\n3|User1|13";

// Define columns in file
var columnKeys = ["ID", "Username", "Age"];

// Custome column validation
var columnValidation = {
    "ID": function (value) {
        if (isNaN(value)) return false;
        else return true;
    },
    "Username": function (value) {
        return true;
    },
    "Age": function (value) {
        return true;
    }
};

var ValidateColumns = function (data, keys, rules) {
    var rowSep = "\r\n";
    var colSep = "|";

    // Get rows
    var rows = data.split(rowSep);
    _.each(rows, function (row) {
        // Get columns
        var columns = row.split(colSep);

        // Validate columns
        _.each(columns, function (column) {
            if (rules[keys[columns.indexOf(column)]](column)) {
                console.log("Column " + keys[columns.indexOf(column)] + ": Passed");
            }
            else {
                console.log("Column " + keys[columns.indexOf(column)] + ": Failed");
            }
        });
    });
};

ValidateColumns(data, columnKeys, columnValidation);

Questions:

  1. How to create functions/methods that will go in?
  2. How to pass these functions inside class/method that will validate columns?

3 Answers 3

2

You should read about the Func and Actions

If you function return something (like a bool!) and/or uses multiple parameters, you should use Func<TReturn,TParam1,TParam2>[] and use as many TParam as you have parameters. There is alot of functionnality behind these so I recommend you to read about it. This link seems good : Explanation of Func

If your functions do not take any parameters and return void (which is not the case in your example, sorry), you could use a Action[] which is used like this :

Action action = () => //your function's code;
//Lame example of using Action[]
YourMethodThatTakesFuncAsParams(new Action[]{action,action,action});

If all of your functions return something different and/or uses different parameters, I don't think there is a clean solution to achieve this.

Little edit (since I can't comment on stuff!) I think Ben's answer is the most appropriate and direct approach to your problem

Sign up to request clarification or add additional context in comments.

1 Comment

Dictionary<string, Func<object,bool>> validationRules = new Dictionary<string, Func<object, bool>>(); this is what I created.
1

What you are looking for is a predicate. Essentially it represents a function with a definable input type and a boolean output.

Here is an example of passing an array of Predicates.

//this is a Predicate<string> b/c it takes a string as input and outputs a boolean
bool ExamplePredicate(string input)
{
    return input == "something";
}

//function accepting an array of Predicate<string> as input
bool Validate(Predicate<string>[] validators /*, other data input*/ )
{
    //use the array of validators to process your data.
}

void Test()
{
    //array of validators
    var validators = new Predicate<string>[]
    {
        aString => !string.IsNullOrEmpty(aString), // you can use lambda expressions
        ExamplePredicate               // or reference functions matching the predicate definitions
    }
    Validate(validators);
}

Documentation: http://msdn.microsoft.com/en-us/library/bfcke1bz(v=vs.110).aspx

2 Comments

Just saw Lajos post about Delegates - those are good too. Slightly more complicated but also more powerful.
Starting in .NET 3.5, Func<T, bool> is preferred over Predicate<T>. For this purpose they do the same thing, but most of the framework (and all of LINQ) expects you to use the newer, more general-purpose Func-family of delegates.
0

You are looking for delegates. Please read the MSDN documentation. I can advise you these particular links:

http://msdn.microsoft.com/en-us/library/aa288459%28v=vs.71%29.aspx

http://msdn.microsoft.com/en-us/library/ms173172.aspx

The first is a tutorial about delegates and the second is about using them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.