我有一个API包装器,它具有一系列在首次使用时初始化的属性.Disposed
调用类的方法时,它会处理它存储的所有其他类.但是,很有可能只有其中一个或两个类已初始化.如果这些类尚未初始化,则该Dispose
方法实际上最终会初始化它们并将它们处理掉.这是一个庞大的资源和时间密集型,但我想不出一种方法来弥补它.
这是班级.
public class SalesForceApi : ISalesForce, IDisposable { public Logger Logger = LogManager.GetCurrentClassLogger(); private IAccounts _accounts; private IAttachments _attachments; private ICases _cases; private IContacts _contacts; private IGroups _groups; private IRecordTypes _recordTypes; private IUsers _users; public IAccounts Accounts => _accounts ?? (_accounts = new Accounts()); public IAttachments Attachments => _attachments ?? (_attachments = new Attachments()); public ICases Cases => _cases ?? (_cases = new Cases()); public IContacts Contacts => _contacts ?? (_contacts = new Contacts()); public IGroups Groups => _groups ?? (_groups = new Groups()); public IRecordTypes RecordTypes => _recordTypes ?? (_recordTypes = new RecordTypes()); public IUsers Users => _users ?? (_users = new Users()); public SalesForceApi() { } public void Dispose() { Logger.Trace("Disposing of Acccounts..."); Accounts.Dispose(); Logger.Trace("Disposing of Attachments..."); Attachments.Dispose(); Logger.Trace("Disposing of Cases..."); Cases.Dispose(); Logger.Trace("Disposing of Contacts..."); Contacts.Dispose(); Logger.Trace("Disposing of Groups..."); Groups.Dispose(); Logger.Trace("Disposing of RecordTypes..."); RecordTypes.Dispose(); Logger.Trace("Disposing of Users..."); Users.Dispose(); } }
以下是生成的日志的样子:
SalesForceApi_v1.SalesForceApi::Disposing of Acccounts... SalesForceApi_v1.Requests.RequestBase::Logging into SalesForce... SalesForceApi_v1.Requests.RequestBase::SalesForce login successful! SalesForceApi_v1.Requests.RequestBase::Logging out of SalesForce. SalesForceApi_v1.SalesForceApi::Disposing of Attachments... SalesForceApi_v1.Requests.RequestBase::Logging into SalesForce... SalesForceApi_v1.Requests.RequestBase::SalesForce login successful! SalesForceApi_v1.Requests.RequestBase::Logging out of SalesForce. SalesForceApi_v1.SalesForceApi::Disposing of Cases... SalesForceApi_v1.Requests.RequestBase::Logging into SalesForce... SalesForceApi_v1.Requests.RequestBase::SalesForce login successful! SalesForceApi_v1.Requests.RequestBase::Logging out of SalesForce. SalesForceApi_v1.SalesForceApi::Disposing of Contacts... SalesForceApi_v1.Requests.RequestBase::Logging out of SalesForce. SalesForceApi_v1.SalesForceApi::Disposing of Groups... SalesForceApi_v1.Requests.RequestBase::Logging into SalesForce... SalesForceApi_v1.Requests.RequestBase::SalesForce login successful! SalesForceApi_v1.Requests.RequestBase::Logging out of SalesForce. SalesForceApi_v1.SalesForceApi::Disposing of RecordTypes... SalesForceApi_v1.Requests.RequestBase::Logging into SalesForce... SalesForceApi_v1.Requests.RequestBase::SalesForce login successful! SalesForceApi_v1.Requests.RequestBase::Logging out of SalesForce. SalesForceApi_v1.SalesForceApi::Disposing of Users... SalesForceApi_v1.Requests.RequestBase::Logging into SalesForce... SalesForceApi_v1.Requests.RequestBase::SalesForce login successful! SalesForceApi_v1.Requests.RequestBase::Logging out of SalesForce.
adv12.. 5
在处理时,不要调用lazy-init的方法; 测试并处理私有成员变量(_accounts等).
在处理时,不要调用lazy-init的方法; 测试并处理私有成员变量(_accounts等).