Refactoring many methods in one

I do not know how to correctly name the question, so I could not change it. My question is: I have about 10 methods that look like this:

[WebMethod(EnableSession = true)] public string ReadUserAdditional() { EUser user = (EUser)Session["user"]; var json = new { result = true, user.Image, user.Biography }; return new JavaScriptSerializer().Serialize(json); } [WebMethod(EnableSession = true)] public string ReadUserBasicInformation() { EUser user = (EUser)Session["user"]; var json = new { result = true, user.Name, user.Username}; return new JavaScriptSerializer().Serialize(json); } 

The methods are very similar, but they return different fields. Im thinking of reorganizing all methods into one, getting fields to return as parameters. Is that a good idea? How can i do this? Reflection?

+4
source share
4 answers

First of all, you need to know that the object and the dictionary are represented in json simmilar.

 [WebMethod(EnableSession = true)] public string ReadUserAdditional() { return GetUserInfo(new [] { new FieldInfo {Name = "Image", u => u.Image}, new FieldInfo {Name = "Biography", u => u.Biography} }); } private string GetUserInfo(FieldInfo[] infos) { EUser user = (EUser)Session["user"]; var dict = new Dictionary<string, object>{ { "result", true } }; foreach(var info in infos) { dictionary.Add(info.Name, info.Accessor(user)); } return new JavaScriptSerializer().Serialize(dict ); } public class FieldInfo { public Func<EUser, object> Accessor { get; set; } public string Name { get; set;} } 
0
source

I don’t think this is a terrible idea, especially if you have tons of these methods and want to simplify your API.

A few minus:

1) Reflection occurs at maximum cost. It probably doesn't matter if you're not the size of Twitter.

2) There may be security issues if the data had any properties that you DO NOT want users to access, for example, some internal database keys or not. Make sure that all the properties of your class are that you become fully public information.

0
source

You can use lambda to reorganize duplication :. This will reduce all your methods to one line of code:

 [WebMethod(EnableSession = true)] public string ReadUserAdditional() { return GetUserJSON(x => new { result = true, x.Image, x.Biography }); } [WebMethod(EnableSession = true] public string ReadUserBasicInformation() { return GetUserJSON(x => new { result = true, x.Name, x.UserName }); } private string GetUserJSON(Func<EUser, string> jsonFields) { EUser user = (EUser)Session["user"]; var json = jsonFields(user); return new JavaScriptSerializer().Serialize(json); } 
0
source

Another approach is to use Automapper or a similar library to design your data.

 [WebMethod(EnableSession = true)] public string ReadUserAdditional() { return GetUserInfo<UserAdditionalDto>(); } private string GetUserInfo<TDto>(FieldInfo[] infos) { EUser user = (EUser)Session["user"]; var dto = Mapper.Map<TDto>(user); // Mapper is Automapper entry class. return new JavaScriptSerializer().Serialize(dto ); } public class UserAdditionalDto { public string Image { get; set; } public string Biography { get; set;} } 
0
source

All Articles