Perhaps my eyes are fooling me, but how is it that in .NET 2.0, XmlReader implements Dispose but does not have a Dispose() method? I see it has Dispose(bool), but not a parameterless overload.
It implements it explicitly System.IDisposable.Dispose(). Dispose(boolean) is a normal method that does this ...
protected virtual void Dispose(bool disposing)
{
if (this.ReadState != ReadState.Closed)
{
this.Close();
}
}
... so you need to call it for ex. this way
XmlReader r = XmlReader.Create(s);
((IDisposable)r).Dispose();
Related
I need to create a custom IObservable. I've read a bit over there, I've ended up I shouldn't implement IObservable<T> directly.
I've noticed that there is an ObservableBase<T>. This is an abstract class, so I need to implement the abstract method:
public class Store<TState> : ObservableBase<TState>, IObserver<IAction>
{
public void OnCompleted()
{
throw new NotImplementedException();
}
public void OnError(Exception error)
{
throw new NotImplementedException();
}
public void OnNext(IAction value)
{
throw new NotImplementedException();
}
protected override IDisposable SubscribeCore(IObserver<TState> observer)
{
>>>>>>>>>>>>>>>>>******<<<<<<<<<<<<<<<<<<<<<<<
throw new NotImplementedException();
}
}
How should I implement this method?
I don't know your problem, but if you can replace implementing IObservable for exposing a property/method that returns IObservable you'll be a lot better off. If you can do that, you can easily return an Rx-based Observable from some of Rx's create methods.
If you can't do that, I would recommend wrapping a Subject<T>:
public class MyIntObservable : IObservable<int>
{
private readonly Subject<int> _mySubject = new Subject<int>();
public IDisposable Subscribe(IObserver<int> observer)
{
return _mySubject.Subscribe(observer);
}
}
I'm guessing you don't want to be doing what you are doing.
From a brief look at the "Store" class that you want to mimic, it appears to be some sort of Subject. A Subject is both a IObserver and an IObservable and there are many implementations that are provided out of the box. Here is the interface:
public interface ISubject<in TSource, out TResult> : IObserver<TSource>, IObservable<TResult>
{
}
This is a very good article about Subjects and when to use them here:
http://davesexton.com/blog/post/To-Use-Subject-Or-Not-To-Use-Subject.aspx
I have run into issues where after #Around skips the method execution and returns a value right away, it also skips rest of Aspect advices. See my example code:
public class MyService {
...
#AnnotationAround
#AnnotationAfterReturning
public MyResult get() {
...
}
...
}
#Aspect
public class AroundInjector {
#Pointcut(value="execution(* *(..))")
public void anyPublicMethod() {
}
#Around("anyPublicMethod() && #annotation(around)")
public Object aroundThis(ProceedingJoinPoint pjp, AnnotationRound around) throws Throwable {
return new MyResult();
}
}
#Aspect
public class AfterReturningInjector {
#Pointcut(value="execution(* *(..))")
public void anyPublicMethod() {
}
#AfterReturning(pointcut="#annotation(afterReturn)", returning="result")
public void permissionCheckOwnership(JoinPoint jp, AnnotationAfterReturning afterReturn, Object result) throws Throwable {
System.out.println(">>>>>>>>>>>>>> never here");
}
}
So, after #Around advice returns directly, the #AfterReturning is never executed. Please help!
Your method never returns anything because its execution is prevented by the around advice, so the advice for its return joinpoint is not executed.
To solve this either call the method inside your around advice (and take care to have the correct aspect precedence), or make its content part of the around advice.
What’s the best way to invoke a method by reflection using GWT, I know that there are some frameworks like "GWT Reflection" but I really want to hear some feedback about this.
How is the best way to convert something like this:
GreetingServiceAsync service = GWT.create(GreetingService.class);
AsyncCallback callBack = new AsyncCallback< Void>() {
#Override
public void onFailure(Throwable caught) {
}
#Override
public void onSuccess(Void result) {
}
};
service.doSomething(callBack);
in:
GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
String methodName = “doSomething”;
Object service;
AsyncCallback callBack = new AsyncCallback< Void>() {
#Override
public void onFailure(Throwable caught) {
}
#Override
public void onSuccess(Void result) {
}
};
/*somehow invoke by reflection*/
Class<?> c = Class.forName(GreetingServiceAsync.class.getName());
Method method = c.getMethod(methodName, AsyncCallback.class);
method.invoke (service, callBack);
Many thanks,
Luis.
Javascript 101 - there is no concept of reflection. GWT java translates to javascript. So gwt does not provide reflection support. Every other library that states gwt reflection in their homepage are just addressing a corner functionality and mis-stating their feature.
Doing my app, I got bored from always implement the same default error treatment (show a message, caught.printstacktrace and etc..) in the asynccallback onfailure.
I wonder if you can make a generic treatment or standard treatment, something like that.
Thanks.
I assume you are using standard GWT-RPC. Something like this might help
public abstract class AbstractCallBack<T> implements AsyncCallback<T>{
#Override
public void onFailure(Throwable caught) {
//Default error Handling code goes here
}
}
And whenever you use your service instead of instantiating an AsyncCallback you can instantiate this class and have generalized error handling.
SomeServiceAsync service = GWT.create(SomeService.class);
service.someMethod("Hello!", new AbstractCallBack<String>() {
#Override
public void onSuccess(String result) {
// TODO Auto-generated method stub
}
});
I am using using GWT2.3 with GWTP. Now in this application I need to make a server side call from a non presenter class (So there id no dispatch async instance).
Here is my class
public class NameTokenHandler implements ValueChangeHandler<String> {
#Inject
DispatchAsync dispatchAsync;
#Override
public void onValueChange(ValueChangeEvent<String> event) {
if (event != null) {
String nameToken = event.getValue();
if(dispatchAsync!=null)
{
System.out.println("yes");
} else {
System.out.println("No");
}
History.newItem(nameToken);
}
}
}
Here dispatchAsync is always null. I am getting from where it should be initialized so that I can make a server side call. If there is any other way then please let me know.
Thanks in advance.
You need to inject the NameTokenHandler, so your dispatcher will be injected too.
public class C {
private NameTokenHandler handler;
#Inject
public C(NameTokenHandler handler) {
this.handler = handler;
}
}
This way the handler will be injected to the C class, and your dispatcher will also be injected in the NameTokenHandler. BTW you might need to have a constructor in NameTokenHandler that follows the same pattern (DispatchAsync as a parameter).