This Is My Models :
public class Result1
{
public string Price { get; set; }
public string Id { get; set; }
}
public class Result2
{
public string firstName { get; set; }
public string lastName { get; set; }
public string Id { get; set; }
}
public class JsonResult1
{
public List<Product> ProductLists { get; set; }
}
public class JsonResult2
{
public List<User> UserLists { get; set; }
}
And This is My First And Second Methods
public async Task<List<Result1>> GetFirstMethod(string id, string a)
{
var info = JsonConvert.DeserializeObject<JsonResult1>(a);
var mylist = new List<Result1>();
foreach (var item in info.ProductLists)
{
mylist.Add(new Result1()
{
Price = item.Price,
Id = item.ID
});
}
return mylist;
}
public async Task<List<Result2>> GetSecondMethod(string id, string a)
{
var info = JsonConvert.DeserializeObject<JsonResult2>(a);
var mylist = new List<Result2>();
foreach (var item in info.UserLists)
{
mylist.Add(new Result2()
{
firstName = item.FirstName,
lastName = item.LastName,
Id = item.ID
});
}
return mylist;
}
//And the third method and the fourth method and...
Each method has different models…
How to make a generic method for All the above methods??
The main problem is in the mapping part inside the list…
Is it even possible to do something like this?
New contributor
Ya YA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Here is a step-by-step solution:
- Define a generic interface for mapping.
- Implement the interface for each type of result.
- Create a generic method that uses the interface to handle the mapping.
public interface IResultMapper<T>
{
List<T> MapFromJson(string jsonString);
}
public class Result1Mapper : IResultMapper<Result1>
{
public List<Result1> MapFromJson(string jsonString)
{
var info = JsonConvert.DeserializeObject<JsonResult1>(jsonString);
var mylist = new List<Result1>();
foreach (var item in info.ProductLists)
{
mylist.Add(new Result1()
{
Price = item.Price,
Id = item.ID
});
}
return mylist;
}
}
public class Result2Mapper : IResultMapper<Result2>
{
public List<Result2> MapFromJson(string jsonString)
{
var info = JsonConvert.DeserializeObject<JsonResult2>(jsonString);
var mylist = new List<Result2>();
foreach (var item in info.UserLists)
{
mylist.Add(new Result2()
{
firstName = item.FirstName,
lastName = item.LastName,
Id = item.ID
});
}
return mylist;
}
}
public class DataService
{
public async Task<List<T>> GetMethod<T>(string id, string jsonString, IResultMapper<T> mapper)
{
return await Task.Run(() => mapper.MapFromJson(jsonString));
}
}
Usage:
public async Task ExampleUsage()
{
var dataService = new DataService();
string jsonString1 = ""; // Your JSON string for Result1
var result1Mapper = new Result1Mapper();
List<Result1> result1List = await dataService.GetMethod("someId", jsonString1, result1Mapper);
string jsonString2 = ""; // Your JSON string for Result2
var result2Mapper = new Result2Mapper();
List<Result2> result2List = await dataService.GetMethod("someId", jsonString2, result2Mapper);
}