I have an interface to the .NET service level, which, in turn, will interact with a third-party system through a web service. This handles a number of different tasks related to the functionality of third-party systems (this is actually a custom CRM system, but since the context does not matter, I will change this to something trivial).
The interface looks something like this:
public interface IMyService { CarModel GetCar(string registration); CarModel AddCar(Car car); PersonModel GetPerson(string personId); PersonModel AddPerson(Person person); }
Now my models work as follows: I have a BaseResponseModel from which every SomethingModel is inherited. Each SomethingModel contains some basic properties and also wraps Something - for example:
Basic response model
public class BaseResponseModel { public List<string> Errors { get; set; } public bool HasErrors { get { return (Errors != null && Errors.Count > 0); } } }
Specific Response Models
public class CarModel : BaseResponseModel { public Car Car { get; set; } } public class PersonModel : BaseResponseModel { public Person Person { get; set; } }
Here, Car and Person simply contain a bunch of public properties. Then each method of my IMyService takes its arguments, formats the request to the .asmx web service, analyzes the response in its response model and returns it to the caller (.ascx codebehind code).
However, the number of different classes ...Model (not to mention the fact that they all have different property names for their wrapped objects) becomes ugly. I mean to do something in the following lines:
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); }
My ASP.NET controls will receive ServiceResponse<T> from each method in IMyService .
Is this a "conditionally correct" use of generics in C #? Or does it just mask the deeper architectural flaws with my decision? Is there something missing in my proposed solution (although note that the implementations of the different Get and Add methods are not common, as their prototypes show)?
Disclaimer: Sorry if this question is "too subjective", but it was too specific to be a theoretical question for Programmers.SE and too general to be a question for CodeReview.SE. I am open to suggestions on how to improve the issue, if necessary.