I'm looking for a good way to dispose of a series of objects, so the problem is in GetMessage if I use a using statement for the underlying stream to create the attachment... in method 1 when I go to send the mail message the stream for the attachment is disposed of and will receive an error. If I take off the using statement it works find but I haven't disposed of DataStream Dest properly. My initial though was adding a parameter List to method one that I can add Dest to each time GetMessage is called and in method 1 at the end of the method loop through the list to dispose of the streams. SendMessage can be called in a loop from a batch process so many streams could potentially be created and I want to leave it to the caller (SendMessage) to dispose of them rather than in GetMessage since SendMessage is where the send takes place and I need to make sure the message has sent before disposal. Any suggestions???
void sendMessage(MessageType type){
MailMessage m = Method2();
switch(type)
{
case type1:
m = Object1().GetMessage(param1)
case type2:
m = Object2().GetMessage(param1, param2)
case type3:
m = Object3().GetMessage(param1)
case type4:
m = Object4().GetMessage(param1, param2, param3)
case NewType5:
m = NewType5().GetMessage(param1, param2)
}
// this method does the send cause it has the mailmessage
m.Send();
}
class NewType5()
{
MailMessage GetMessage(){
// used to be
// using (var Dest = new DataStream(true)){ .... }
DataStream Dest = new DataStream(true);
// code that reads info into the stream
// Go back to the start of the stream
Dest.Position = 0;
// Create attachment from the stream
Attachment mailattachement = new Attachment(Dest, contentType);
//Create our return value
var message = new MailMessage();
message.To.Add(UserInfo.UserDetail.Email);
message.Subject = "P&L Data View - " + DateTime.Now.ToString();
message.Attachments.Add(mailattachement);
return message;
}
}
To move send into GetMessage I would have to change all the other object implementations on all the other objects. I would rather let send message take care of the disposal for newtype5 because from a batch process it could be called 500 times. Does this make the example a little more clear. Send Message was already written an implemented against is several other objects with a GetMessage implementation. Mine just happens to use a stream and I can't dispose of the stream before the send takes place.
Method2
or create/dispose the stream inMethod1
and inject it as a parameter inMethod2
From your comments to the answers currently given I can only conclude that (1) you did not provide all information in your question and (2) your design is flawed.