As mentioned before, DM API calls are old-styled HRESULT functions, which return 0 (S_OK), to indicate the successful execution, or an error code. On the other hand, .NET developers commonly expect handling an exception, if something has gone wrong, not an error code.
When creating managed wrappers for DM API calls, we want an exception to be thrown in case on an error. Hence, the result of every DM API call is to be checked and a specialized exception is to be thrown:
Here is an example of a DM API method call, the result check, and exception throwing:
Get full source code of DMApiHelpers from GitHub: https://github.com/dotnetnick/edocs
When creating managed wrappers for DM API calls, we want an exception to be thrown in case on an error. Hence, the result of every DM API call is to be checked and a specialized exception is to be thrown:
namespace DMApiHelpers { public class DMApiException : Exception { public DMApiException() { } public DMApiException(string message) : base(message) { } public DMApiException(string message, Exception innerException) : base(message, innerException) { } public DMApiException(SerializationInfo info, StreamingContext context) : base(info, context) { } public DMApiException(string message, int errNumber, string errDescription) : base(string.Format("{0} Error {1}: {2}", message, errNumber, errDescription)) { } } }
Here is an example of a DM API method call, the result check, and exception throwing:
using Hummingbird.DM.Server.Interop.PCDClient; namespace DMApiHelpers { public class DMDocument : DMBase { public void DeleteProfile(int docNumber, bool clearLinks) { if(clearLinks) ClearLinks(docNumber); var doc = new PCDDocObjectClass(); doc.SetDST(Dst); doc.SetObjectType(ObjectFormDefaultProfile); doc.SetProperty(PropertyTargetLibrary, Library); doc.SetProperty(PropertyObjectIdentifier, docNumber); int result = doc.Delete(); if(result != S_OK || doc.ErrNumber != 0) throw new DMApiException(string.Format("Cannot delete document# {0}.", docNumber), doc.ErrNumber, doc.ErrDescription); } } }
Get full source code of DMApiHelpers from GitHub: https://github.com/dotnetnick/edocs
No comments:
Post a Comment