Reputation: 43
WebAPI (MyProject) referencing a ThirdPartyLib for data retrieval and posting. There are many models defined in ThirdPartyLib which I wanted to reuse in WebAPI and also pass onto client/consumer. I don't want the client to reference the ThirdPartyLib. I am having trouble finding a way to wrap ThirdPartyLib model/class in MyProject.
Is this the correct approach? Just define an empty and reference third party Customers model? How about serialization?
public class Customers : ThirdPartyLib.Models.Customers
{
}
By using this way, the client has to reference ThirdPartyLib in their project to get Customers model/class attributes. How do I wrap Customers model in MyProject in a way that I don't have to update MyProject whenever ThirdPartyLib Models changed or added?
namespace MyProject.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<ThirdPartyLib.Models.Customers> Get(int id)
{
return GetCustomers();
}
}
}
Upvotes: 1
Views: 696
Reputation: 239430
If any part of your third party library is exposed, then consumers will have to take a dependency on it. Simple as that. The only way to remove that dependency is to not expose it at all.
That means you'll have to map to/from classes of your own making to represent the classes in the third party library. Consumers will use your classes, and you will map that over to the third party library classes to do the actual work. Then, the consumers don't know or care what's actually doing the work.
To be more explicit, that means you cannot even inherit from the third party library. Something like MyLib.Customers : ThirdPartyLib.Customers
still requires a dependency on ThirdPartyLib
even if you're using only MyLib.Customers
. You need to create a clean POCO that doesn't inherit from anything or at least anything outside of your library for you version of Customers
. That's where the mapping part comes in. You'll basically move the values of the properties from your Customers
instance to an instance of third party library's Customers
. This can either be handled manually, prop by prop, or you can employ a library like AutoMapper to do it for you based on convention/configuration.
Upvotes: 1