Ideally, I'd like to figure out a way to use the IOC to pass in my
dependency, but I seem not to be able to figure it out. As such, I ask
windsor directly for the object. Since I don't also release the object, we
have experienced problems in production. So, I need to figure out a way to
release the object I manually requested from the container. The problem I
have is that the object runs a process in a separate thread, and this
process can take many hours to complete. I don't have faith that the way
I'm trying to release the object from the calculator is going to work
properly. There is probably an smart way to do this, but right now that
escapes me. I'm hoping someone here can help me with a better solution.
PLEASE HELP!!!
Some code:
// installer code
public class WindsorInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container,
IConfigurationStore store)
{
container.Register(
Component.For<ICalculator>().ImplementedBy<Calculator>().Named("ICalculator")
.LifeStyle.Transient
.OnDestroy(myInstance => myInstance.ByeBye()));
container.Register(Component.For<IExtractionJob>().ImplementedBy<ExtractionJob>().Named("IExtractionJob")
.LifeStyle.Transient);
container.Register(Component.For<IWindsorFactory>().ImplementedBy<WindsorFactory>().Named("IWindsorFactory")
.LifeStyle.Singleton);
container.Register(Component.For<IRunner>().ImplementedBy<Runner>().Named("IRunner")
.LifeStyle.Singleton);
}
}
// runner test
namespace WindsorTest
{
public interface IRunner
{
ICalculator Calculator { get; set; }
void Extract();
void Release(ICalculator calculator);
}
public class Runner : IRunner
{
public IWindsorFactory Factory { get; set; }
public ICalculator Calculator { get; set; }
public void Extract()
{
var calculator = Factory.GetCalculator(); // would like to
replace this with Windsor passing in the dependency
Console.WriteLine("Extract with calculator: {0}",
calculator.ExtractionJobID);
var random = new Random();
var input1 = random.Next(1, 100);
var input2 = random.Next(1, 50);
Console.WriteLine("Calculating: {0} + {1}", input1, input2);
calculator.Calculate(input1, input2, this);
}
public void Release(ICalculator calculator)
{
Console.WriteLine("Releasing calculator...." +
calculator.ExtractionJobID);
Factory.Release(calculator);
}
}
}
// calculator class
namespace WindsorTest
{
public interface ICalculator
{
void Calculate(int param1, int param2);
void Log(string message);
void ByeBye();
int ExtractionJobID { get; }
IRunner Runner { get; }
}
class Calculator : ICalculator, IDisposable
{
CalcDelegate mDeleg;
private delegate int CalcDelegate(int input1, int input2);
public IRunner Runner { private set ; get; }
public int ExtractionJobID { private set ; get; }
public Calculator(IRunner runner)
{
Log(String.Empty);
var random = new Random();
ExtractionJobID = random.Next(1000, 2000);
Runner = runner;
}
public void Log(string message)
{
Console.WriteLine(message);
}
public void Calculate(int param1, int param2)
{
mDeleg = new CalcDelegate(Calc);
var callback = new AsyncCallback(CalcCallback);
int input1 = Convert.ToInt16(param1);
int input2 = Convert.ToInt16(param2);
mDeleg.BeginInvoke(input1, input2, callback);
}
private int Calc(int input1, int input2)
{
Thread.Sleep(new TimeSpan(0, 0, 0, 30));
return input1 + input2;
}
private void CalcCallback(IAsyncResult ar)
{
mDeleg.EndInvoke(ar);
Log(string.Format("Release instance of this class: {0}",
ExtractionJobID));
Runner.Release(this); // here's where I am trying to release this object
since we are done with it
}
public void ByeBye()
{
Log(string.Format("Calculator with ExtractionJobID {0} is in
ByeBye: {1}", ExtractionJobID, DateTime.Now));
}
// Flag: Has Dispose already been called?
bool disposed = false;
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
// Free any other managed objects here.
//
Log(string.Format("Calculator with ExtractionJobID {0} is
being disposed: {1}", ExtractionJobID, DateTime.Now));
}
// Free any unmanaged objects here.
//
disposed = true;
}
}
}
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.