Reputation: 813
I have recently finished a project that uses xmlserializer and generics to decode an API. We then work with the data and present it as another API that our apps consume. We are looking at letting a couple of other companies in town use the API as they are running the same software as us.
I would obviously like to encapsulate the code so they cant steal all of our hard work. The problem i am facing is that all of our classes must be public classes in order to serialize and deserialize. I've tried marking them internal but XMLSerializer always errors out.
What i would like to be able to do is give them a dll that exposes the public 8 methods of the API rather than exposing the 30 public classes of the library.
I currently have it working in the follow fashion: Client app that has a reference to the API API that has a reference to the library. This houses the 8 public methods. Library that has all the classes and serialization libraries.
The problem is the client needs access to a class in the Library or i have to write convert to and from methods on the API (I have done this but it feels dirty) Some code. This is a public class that is serialized and then deserialized hence marked public
public class Ping
{
public class Request
{
public string methodName = "Ping";
}
public class Response
{
public String methodName { get; set; }
public int methodErrorCode { get; set; }
public String methodErrorMessage { get; set; }
}
private Method<Request, Response> ping;
public Ping(Credentials auth)
{
ping = new Method<Request, Response>(auth);
}
public bool isAlive()
{
/* send the request directly as there are no variables in the ping request that need to be set */
ping.post();
/* analyze the response */
return ping.response.methodResponse.methodErrorMessage.Equals("Pong") ? true : false;
}
}
There are about 30 classes like that that all need to be marked public for serialization but should not be accessible via the API. How can i handle this? Thanks in advance
Upvotes: 1
Views: 1084
Reputation: 12295
You can hide the classes to make your API simpler such that there are less classes a consumer needs to be aware of. However, as mike z points out, this wont prevent someone with a decompiler from analyzing your source code. For that, you'll need an obfuscator.
Either way, you can create another library (ie PublicLibrary) that sits on top of the library you have now (ie CurrentLibrary) and creates a facade. The classes in CurrentLibrary, will still be public (but obfuscated if you choose). However, a consumer could use PublicLibrary and wont need to be exposed to the 30 extraneous classes in CurrentLibrary.
For example:
//PublicLibrary.dll
public class PublicExampleClass
{
public string SomeMethod()
{
//simplify CurrentLibrary
return new CurrentLibrary.Serializer()
.SerializeCurrentLibrayrObject(new CurrentLibraryObject());
}
}
//CurrentLibrary.dll
public class Serializer
{
public string SerializeCurrentLibrayrObject(CurrentLibraryObject obj){
//Serialize obj
}
}
Upvotes: 1