Generics and Conventions in C #

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.

+8
generics design c # architecture
source share
2 answers

I do not see a problem with this use of generics, unless you have someone who uses your service from something other than .NET. I think it generates pretty ugly contract names for WSDL.

So, for your use case, I would say that everything is in order. I'm glad you changed from Model to Response . I was going to guess that.

Depending on how the errors are used, I personally would prefer to create an (aggregated) exception for them. However, if you use it to validate a form or something else, I would say that this is acceptable.

+2
source share

My answer is probably too susceptible too, but I suggest avoiding creating dependencies before you have a stable working system, at least in beta. Especially when you are at the beginning of development. Just like inheritance in one of the strongest types of relationships, it should be postponed for as long as possible.

By adding a base class or a general one that looks the same, you put yourself in very tight bounds.

What if, say, there is a situation where you come up with a dictionary or mistakes? Or do you need a special response code for CarRequest? Or a special type of response, such as "redirection"?

So, my suggestion is that you do a special kind of response to the request until you find some similarity of answers after significant development.

Must be GetCarResponse before GetCar . Do you plan errors in response? Just throw an exception. Not found? Returns null. Found? Return what you find and don’t worry about errors.

This probably needs a bit more coding, but you have free hands on some artificial limitations.

0
source share

All Articles