Generics and convention in C#

malikbhai

New Member
I have an interface to a service layer, which is designed to provide an interface to a third-party system via a web service. This handles a number of different tasks related to the third-party system's functionality (it is actually a bespoke CRM system, but as the context isn't relevant I will swap this out for something trivial).The interface looks something like this:\[code\]public interface IMyService{ CarModel GetCar(string registration); CarModel AddCar(Car car); PersonModel GetPerson(string personId); PersonModel AddPerson(Person person);}\[/code\]Now, my models currently work as follows: I have a \[code\]BaseResponseModel\[/code\], from which each \[code\]SomethingModel\[/code\] inherits. Each \[code\]SomethingModel\[/code\] contains some basic properties and also wraps a \[code\]Something\[/code\] - like so:Base response model\[code\]public class BaseResponseModel{ public List<string> Errors { get; set; } public bool HasErrors { get { return (Errors != null && Errors.Count > 0); } }}\[/code\]Specific response models\[code\]public class CarModel : BaseResponseModel{ public Car Car { get; set; }}public class PersonModel : BaseResponseModel{ public Person Person { get; set; }}\[/code\]Here, \[code\]Car\[/code\] and \[code\]Person\[/code\] simply contain a bunch of public properties. Then, each method of my \[code\]IMyService\[/code\] takes its arguments, formats a request to an .asmx web service, parses the response into its response model and returns it to the caller (an .ascx codebehind).However, the number of different \[code\]...Model\[/code\] classes (not to mention that they all have different property names for their wrapped objects) is becoming ugly. I am of a mind to do something along the lines of the following:\[code\]public class Car{ public string Registration { get; set; }}public class ServiceResponse<T>{ public List<string> Errors { get; set; } public bool HasErrors { ... } public T Result { get; set; }}public interface IMyService{ ServiceResponse<Car> GetCar(string registration); ServiceResponse<Car> AddCar(Car car); ServiceResponse<Person> GetPerson(string id); ServiceResponse<Person> AddPerson(Person person);}\[/code\]My ASP.NET controls will then receive \[code\]ServiceResponse<T>\[/code\] from every method in \[code\]IMyService\[/code\].Is this the "conventionally correct" usage of generics in C#? Or is this simply masking deeper architectural flaws with my solution? Is there something missing from my proposed solution (though note that the implementations of the different \[code\]Get\[/code\] and \[code\]Add\[/code\] methods aren't as generic as the prototypes make them seem)?Disclaimer: Apologies if this question is "too subjective," but it seemed too specific to be a theoretical question for Programmers.SE and a little too generic to be a question for CodeReview.SE. I am open to suggestions on how to improve the question if necessary.
 
Back
Top