This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hey All,
I'm working on an API wrapper (for Snipe-IT) and I've run into delema that I'm not sure how to approach. I'm semi-new to C# and this is the largest project I've tried my hand at.
Each endpoint of the API returns a different type of object (Asset, Model, Location, ect). To model this I started with a base class and interface that covers all the shared properties (name, id, updated at, ect). I then extended this class for each specific type of object each endpoint returns.
To interact with these endpoints I have a single "manager" class. All CRUD operations pass through an instance of this class. Many of the methods return an object from an endpoint. To avoid making duplicate managers for each endpoint, I have methods in the manager return the interface shared by all end point objects.
However, this means the client has to cast everything it receives to the correct concrete class. Due to how this will be consumed it means tons of casting and null checking. It feels super convoluted and wrong to me.
Every time the clients gets an object it ends up like this:
Asset asset = snipe.AssetManager.Get("Loaner1") as Asset;
The obvious solution would be to make a manager class for each endpoint that returns the concrete object for that specific endpoint. However, that means duplicating the CRUD logic on every manager class, which also feels wrong.
To illustrate: Line 66
I just can't see the correct way to return the concrete object without duplicating the code with a unique manager for each endpoint.
Subreddit
Post Details
- Posted
- 7 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/csharp/comm...