tags:

views:

65

answers:

4

HI all,

I am not sure whether this is possible in C# or in any other language for that matter. Below is my requirement

It is a service application which will get some commands to execute. Those commands are predefined (methods in classes). When user calls these methods, the application framework must call another method (a default method) and complete the execution then execute teh called method. I understand there is a possibility of performance but i am in need of this type of architecture.

To explain better below is an example

Step 1: User A calls a webservice method GetData (string dataid, string dataLocation).
step 2: webservice recieves this call as http get method and it must first execute a default method CheckData(GetData).
Step 3: CheckData will now check the data which was sent to GetData Method and then it executes the GetData Method.

This way i can perform some operations like authentication, cleanup works and also other various system activities. Will it be possible for this kind of model.

My requirement is

User calls Webservice method employee.GetQualification.

In webservices, the call stack must be

Validater.CheckData (GetQualification)
Employee.GetQualification()
Validator.CheckOutput(output)

I am not looking for something like below (method stack)

Employee.GetQualification()
Validater.CheckData (getQualification parameters)
execute actual execution
Validator.CheckOutput(output)
Return

Some links/ thoughts would be beneficial for me to start on

Thanks.

A: 

At the method level, there is no way to do exactly what you want, but if you're using WCF, you can create a custom behavior that does this for you.

See this article for examples, particularly the bit about IParameterInspector.

Personally, I don't think I would use this approach though, unless you need it to be configurable behavior. Hard coding a call to a single private method to do some validation at the start of some operation isn't so bad.

Thorarin
A: 

This seems to be a typical scenario for an AOP framework, e.g. have a look at the logging sample in the PostSharp tutorial

In web apps or in a WCF environment, there are certainly other approaches (e.g. HttModules), but by an AOP-based approach will work in any environment.

Note: there is a free community edition of PostSharp and the older version 1.5 is also free. And of course there are other AOP frameworks, such as Spring.NET.

M4N
+1  A: 

Sounds like you are looking for a weaver like PostSharp (unfortunately not free any more).

Edit: Just remembered another aspect-oriented programming (AOP) library: LOOM.NET.

Greets Flo

Florian Reischl
btw, on the site, its said that Community Edition is free
abatishchev
A: 

You could write a sink for yourself which hooks in before and after your method executes. I had written a sink sometime back, check it out : http://technologyandme.blogspot.com/2009/05/putting-c-attributes-to-use.html

Nitin Chaudhari