PC Review


Reply
Thread Tools Rate Thread

A Class Validator

 
 
Shapper
Guest
Posts: n/a
 
      20th Jul 2012
Hello,

I have the following code which I repeat in many places:

FindUserByIdResponse response = disp.FindUserById(id);

if (response.Exception == null) {
if (response.User == null)
return new StMessage("NotFound");
} else
return new StMessage("Error");

Response can be a FindUserByIdResponse, FindPostByIdResponse, ...

Each response implements IRequestResponse and as a property:

- FindUserByIdResponse has the property User;

- FindPostByIdResponse has the property Post;

I would like to move the previous validation to another class:

Message msg = ValidateResponse<FindUserByIdResponse.User>(response);

In this case the response would be tested against null.

And its property of type User would also be tested against null.

Is this possible?

--

Thank You,
Miguel
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      21st Jul 2012
On 7/20/2012 7:23 AM, Shapper wrote:
> I have the following code which I repeat in many places:
>
> FindUserByIdResponse response = disp.FindUserById(id);
>
> if (response.Exception == null) {
> if (response.User == null)
> return new StMessage("NotFound");
> } else
> return new StMessage("Error");
>
> Response can be a FindUserByIdResponse, FindPostByIdResponse, ...
>
> Each response implements IRequestResponse and as a property:
>
> - FindUserByIdResponse has the property User;
>
> - FindPostByIdResponse has the property Post;
>
> I would like to move the previous validation to another class:
>
> Message msg = ValidateResponse<FindUserByIdResponse.User>(response);
>
> In this case the response would be tested against null.
>
> And its property of type User would also be tested against null.
>
> Is this possible?


Maybe a Func<T, object> argument.

Example:

using System;

namespace E
{
public interface IRequestResponse
{
Exception Exception { get; set; }
}
public class FindUserByIdResponse : IRequestResponse
{
public Exception Exception { get; set; }
public string User { get; set; }
}
public class FindPostByIdResponse : IRequestResponse
{
public Exception Exception { get; set; }
public string Post { get; set; }
}
public class Demo
{
public FindUserByIdResponse FindUserById(int id)
{
return new FindUserByIdResponse();
}
public FindPostByIdResponse FindPostById(int id)
{
return new FindPostByIdResponse { Post = "Something" };
}
}
public class Message
{
}
public class OkMessage : Message
{
public IRequestResponse Response { get; set; }
}
public class ErrorMessage : Message
{
public string Text { get; set; }
}
public class Util
{
public static Message ValidateResponse<T>(T response,
Func<T,object> f) where T : IRequestResponse
{
if(response.Exception != null)
{
return new ErrorMessage { Text = "Exception: " +
response.Exception };
}
else if(f(response) == null)
{
return new ErrorMessage { Text = "Not found" };
}
else
{
return new OkMessage { Response = response };
}
}
}
public class Program
{
public static void Main(string[] args)
{
Demo demo = new Demo();
FindUserByIdResponse resp1 = demo.FindUserById(123);
Message msg1 = Util.ValidateResponse(resp1, r => r.User);
Console.WriteLine(msg1.GetType().FullName);
FindPostByIdResponse resp2 = demo.FindPostById(456);
Message msg2 = Util.ValidateResponse(resp2, r => r.Post);
Console.WriteLine(msg2.GetType().FullName);
Console.ReadKey();
}
}
}

Arne

PS: There are several thing in the demo that are not good code, but the
Func<T, object> may still be a good suggestion.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FREE HTML Editor and Validator - CSE HTML Validator Lite Albert Wiersch Freeware 2 28th Oct 2005 02:48 PM
Custom Validator Class Question needin4mation@gmail.com Microsoft ASP .NET 1 21st Sep 2005 01:55 PM
Getting a reference to the base class of a base class from an inherited class Edward Forgacs Microsoft C# .NET 1 24th Apr 2004 11:06 AM
Pass an instantiated base class to a new class that inherits the same class -- Possible?? Brad Navarro Microsoft C# .NET 3 9th Jan 2004 02:07 AM
activex ,money tree dialer,RdxE Class,updaate class,YInstStarter Class damaged RUSSELL Windows XP Performance 1 23rd Nov 2003 03:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:57 AM.