Simple Injector decorator for non-generic interface - decorator

I tried the following but doesn't work (ActivationException).
public interface IOp { object Execute(object value); }
public class SimpleOp : IOp { public object Execute(object value) { return value; } }
public class CompressOp : IOp {
private readonly IOp nextOp;
public CompressOp(IOp nextOp) { this.nextOp = nextOp; }
public object Execute(object value)
{
return this.Compress(this.nextOp.Execute(value));
}
}
var container = new Container();
container.RegisterAll<IOp>(typeof(SimpleOp), typeof(CompressOp));
Container.RegisterDecorator(typeof(IOp), typeof(CompressOp));
var op = container.GetInstance<IOp>(); <-- ActivationException
The IOp interface really doesn't need to be a generic type because it's designed to do same processing for any object. Do I have to force it to be a generic then always use object as type parameter, just so I can use the decorator pattern?

Try this
var container = new Container();
container.Register<IOp, SimpleOp>();
Container.RegisterDecorator(typeof(IOp), typeof(CompressOp));
var op = container.GetInstance<IOp>();

Related

Abstract property with JsonIgnore is being serialized (Json.NET)

I have the following setup:
public abstract class A
{
[JsonIgnore]
public abstract Type Foo { get; }
}
public abstract class B : A
{
public override Type Foo { get { return typeof(D); } }
}
public class C : B
{
}
public abstract class D
{
}
My problem is that when I serialize an instance of C via the following:
JsonConvert.SerializeObject(instanceOfC, serializationSettings));
The resulting JSON includes an entry for property Foo. Is this the expected result?
My serializationSettings are as follows:
JsonSerializerSettings _serializationSettings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
TypeNameHandling = TypeNameHandling.All,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
Edit: Using Json.NET 6.0.0 and .NET 4.5.
I've had the same problem and I've solved it by specifying a "ContractResolver" property of JsonSerializerSettings like this (initialize all necessary properties for you ):
var settings = new JsonSerializerSettings
{
Formatting = Formatting.None,
ContractResolver = new PublicNotInternalPropertiesCamelCaseContractResolver()
};
And here is my PublicNotInternalPropertiesCamelCaseContractResolver:
public class PublicNotInternalPropertiesCamelCaseContractResolver : CamelCasePropertyNamesContractResolver
{
#region Overrides of DefaultContractResolver
protected override List<MemberInfo> GetSerializableMembers(Type objectType)
{
var members = base
.GetSerializableMembers(objectType)
.Where(m => m.GetCustomAttribute<InternalAttribute>() == null)
.ToList();
return members;
}
#endregion
}
I've had to create an attribute "InternalAttribute", because this solution does not work with JsonIgnoreAttribute for some reasons:
public class InternalAttribute : Attribute
{
}
And I use it like this:
public abstract class RavenDbEntity
{
private string _dbKey;
[Internal]
public string DbKey
{
get { return _dbKey; }
set
{
_dbKey = value;
Id = int.Parse(value.Split('/')[1]);
}
}
public int Id { get; set; }
}
So the output JSON will be without DbKey property

Using find method of org.apache.commons.collections4.CollectionUtils with Predicate

I was using org.apache.commons.collections.CollectionUtils and for this version using find method was like this:
BeanPropertyValueEqualsPredicate objIdEqualsPredicate = new BeanPropertyValueEqualsPredicate("objId", objId);
myObj = (MyClass) CollectionUtils.find(myObjSet, objIdEqualsPredicate);
But with org.apache.commons.collections4.CollectionUtils, I don't know how to make it work.
Here what I do now but if there is a clear way of it, I will be glad to learn:
Predicate<MyClass> objIdEqualsPredicate = new Predicate<MyClass>() {
#Override
public boolean evaluate(MyClass obj) {
return obj.getObjId().equals(objId);
}
};
myObj = CollectionUtils.find(myObjSet, objIdEqualsPredicate);
Is there a way to filter some objects according to the their fields' values. If possible I don't want to use anonymous class for this.
Thanks.
As the common-beanutils still have commons-collections as dependency, you must implement the Predicate interface.
For example you can take the source code of BeanPropertyValueEqualsPredicate and refactor it, so your version implements the org.apache.commons.collections4.Predicate interface.
Or you write your own version. I would prefer not to use anonymous inner classes, because of the possibility to write unit tests for the predicate and reuse it.
Quick Example (not nullsafe,..)
#Test
public class CollectionsTest {
#Test
void test() {
Collection<Bean> col = new ArrayList<>();
col.add(new Bean("Foo"));
col.add(new Bean("Bar"));
Predicate<Bean> p = new FooPredicate("Bar");
Bean find = CollectionUtils.find(col, p);
Assert.assertNotNull(find);
Assert.assertEquals(find.getFoo(), "Bar");
}
private static final class FooPredicate implements Predicate<CollectionsTest.Bean> {
private String fieldValue;
public FooPredicate(final String fieldValue) {
super();
this.fieldValue = fieldValue;
}
#Override
public boolean evaluate(final Bean object) {
// return true for a match - false otherwise
return object.getFoo().equals(fieldValue);
}
}
public static class Bean {
private final String foo;
Bean(final String foo) {
super();
this.foo = foo;
}
public String getFoo() {
return foo;
}
}
}

How to invoke an static method which contains generics types from an static generic class

I'm using Reflection to work out my project classes with the Generics from a third party, however, I keep getting the error "Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true." when I try to Invoke an static method from an static generic class which contains generics invocation.
Third Party code looks like this
public interface INestedGeneric<TResult>
{
INestedGeneric<TResult> DoSomethingElse();
}
public static class GenericClass<TResult> where TResult : new()
{
public static INestedGeneric<TResult> DoSomething()
{ return new NestedClass<TResult>(); }
}
public class NestedClass<TResult> : INestedGeneric<TResult>
{
public INestedGeneric<TResult> DoSomethingElse()
{ return new NestedClass<TResult>(); }
}
My code looks like:
public class Someone
{
private int _integerProperty;
private string _stringProperty;
public int IntegerProperty
{
get { return _integerProperty; }
set { _integerProperty = value; }
}
public string StringProperty
{
get { return _stringProperty; }
set { _stringProperty = value; }
}
}
static void Main(string[] args)
{
Type classType = typeof(Someone);
Type theclass = typeof(GenericClass<>); theclass.MakeGenericType(classType);
Type theinterface = typeof(INestedGeneric<>); theinterface.MakeGenericType(classType);
MethodInfo dosomething = theclass.GetMethod("DoSomething", BindingFlags.Public | BindingFlags.Static);
dosomething.Invoke(null, null);
dosomething = null;
}
Any idea how to invoke the method strictly in this scenario? I have read and try the help from other posts, but didn't work.
Thank you so much...
I already figure it out. The solution was to use the type provided by the MakeGenericType method.
Like this:
...
Type theclass = typeof(GenericClass<>).MakeGenericType(classType);
Type theinterface = typeof(INestedGeneric<>).MakeGenericType(classType);
MethodInfo dosomething = theclass.GetMethod("DoSomething", BindingFlags.Public | BindingFlags.Static);
dosomething.Invoke(null, null);
...

Finding out where TypeErrors are thrown

I am having a problem that is driving me nuts.
Recently I configured my BlazeDS to use Array instead of ArrayCollection for performance reasons. Additionally I adjusted my templates to generate Array properties.
Everything wen't fine. All except one function that causes TypeError: Error #1034. These are being thrown before the result callback is called. It claims to have problems casting an ArrayCollection to Array. I removed the generated types to make Flex use Objects instead, but these did not contain any ArrayCollections. My question now is: How can I get the stack-traces of errors thrown in event-handlers?
I allready added handlers for unhandledExceptions in all of my modules and they are called if errors occur in code triggered from user-interaction, but they don't seem to be able to catch stuff thrown by event-handlers.
How can I track these Errors?
Chris
PS: The classes are:
package de.upw.ps.ucg.model.ucg.scheduler {
[Bindable]
[RemoteClass(alias="de.upw.ps.ucg.model.ucg.scheduler.Task")]
public class Task extends TaskBase {
}
}
And:
package de.upw.ps.ucg.model.ucg.scheduler {
import de.upw.ps.ucg.model.oval.common.OvalVersionedIdentifier;
import flash.utils.IExternalizable;
[Bindable]
public class TaskBase {
public function TaskBase() {}
private var _aborted:Boolean;
private var _characteristicsId:String;
private var _currentExecutorPhase:JobExecutorPhase;
private var _definitionSetName:String;
private var _definitionSetVid:OvalVersionedIdentifier;
private var _endTime:Date;
private var _enqueueTime:Date;
private var _environmentId:String;
private var _environmentName:String;
private var _messages:Array;
private var _numberOfDefinitions:int;
private var _processedNumberOfTests:int;
private var _resultsId:String;
private var _schedulerJob:SchedulerJob;
private var _startTime:Date;
private var _statusMessage:String;
private var _taskId:String;
private var _totalNumberOfTests:int;
public function set aborted(value:Boolean):void {
_aborted = value;
}
public function get aborted():Boolean {
return _aborted;
}
public function set characteristicsId(value:String):void {
_characteristicsId = value;
}
public function get characteristicsId():String {
return _characteristicsId;
}
public function set currentExecutorPhase(value:JobExecutorPhase):void {
_currentExecutorPhase = value;
}
public function get currentExecutorPhase():JobExecutorPhase {
return _currentExecutorPhase;
}
public function set definitionSetName(value:String):void {
_definitionSetName = value;
}
public function get definitionSetName():String {
return _definitionSetName;
}
public function set definitionSetVid(value:OvalVersionedIdentifier):void {
_definitionSetVid = value;
}
public function get definitionSetVid():OvalVersionedIdentifier {
return _definitionSetVid;
}
public function set endTime(value:Date):void {
_endTime = value;
}
public function get endTime():Date {
return _endTime;
}
public function set enqueueTime(value:Date):void {
_enqueueTime = value;
}
public function get enqueueTime():Date {
return _enqueueTime;
}
public function set environmentId(value:String):void {
_environmentId = value;
}
public function get environmentId():String {
return _environmentId;
}
public function set environmentName(value:String):void {
_environmentName = value;
}
public function get environmentName():String {
return _environmentName;
}
public function set messages(value:Array):void {
_messages = value;
}
public function get messages():Array {
return _messages;
}
public function set numberOfDefinitions(value:int):void {
_numberOfDefinitions = value;
}
public function get numberOfDefinitions():int {
return _numberOfDefinitions;
}
public function set processedNumberOfTests(value:int):void {
_processedNumberOfTests = value;
}
public function get processedNumberOfTests():int {
return _processedNumberOfTests;
}
public function set resultsId(value:String):void {
_resultsId = value;
}
public function get resultsId():String {
return _resultsId;
}
public function set schedulerJob(value:SchedulerJob):void {
_schedulerJob = value;
}
public function get schedulerJob():SchedulerJob {
return _schedulerJob;
}
public function set startTime(value:Date):void {
_startTime = value;
}
public function get startTime():Date {
return _startTime;
}
public function set statusMessage(value:String):void {
_statusMessage = value;
}
public function get statusMessage():String {
return _statusMessage;
}
public function set taskId(value:String):void {
_taskId = value;
}
public function get taskId():String {
return _taskId;
}
public function set totalNumberOfTests(value:int):void {
_totalNumberOfTests = value;
}
public function get totalNumberOfTests():int {
return _totalNumberOfTests;
}
}
}
Both classes are generated by my maven build from a corresponding Java Class and the Types do fit together nicely.
Do you have access to the socket class that's reading in all these messages? Trace out the buffer before the deserialisation and you should at least be able to find the class that's giving you hassle.
Failing that, trace out the object after deserialisation and it should be the very first one after the error is thrown.
This is something you'll have to debug on your own, but I have a gut feeling that the problem is because the data being sent by your java DTO is not the same as your AS3 class, even though that you have the RemoteClass metadata saying that it is.
Are you missing a property? or have a property mismatch? That is the most likely cause of your error. I suggest you debug the java side as much as you can and use something like firebug to see the request/response of the server.

implementing singleton class for Actionscript

I know actionscript does not allowed private contstructor at any time and But if i want to write a sinlgleton class in action script So how to implement it in actionscript.
Can anyone provide an sample example of a singleton pattern in actionscript?
I use something like this:
package singletons
{
[Bindable]
public class MySingleton
{
private static var _instance:MySingleton;
public function MySingleton(e:Enforcer) {
if(e == null) {
throw new Error("Hey! You can't do that! Call getInstance() instead!");
}
}
public static function getInstance():MySingleton {
if(_instance == null) {
_instance = new MySingleton (new Enforcer);
}
return _instance;
}
}
}
// an empty, private class, used to prevent outside sources from instantiating this locator
// directly, without using the getInstance() function....
class Enforcer{}
You need to alter Alxx's answer slightly as it doesn't stop new Singleton() from working...
public class Singleton {
private static var _instance : Singleton;
public function Singleton( newBlocker : ClassLock ) {
}
public static function getInstance() : Singleton {
if ( _instance == null ) {
_instance = new Singleton( new ClassLock() );
}
return _instance;
}
}
class ClassLock{}
The private class is used by the Singleton to stop other classes simply doing new Singleton() initially and then getting a second instance by doing getInstance().
Note that this still isn't watertight... If someone is determined to break it, they can get access to the private class, but this is about the best option for Singletons.
basically, all answers are right, those of reid and gregor provide more compile time safety. I suppose, the best thing is however, to declare an interface for the singleton and a private implementor exposed through a static class:
package {
interface IFoo {
function foo():void;
}
}
and then:
package Foo {
private static var _instance:IFoo;
public static function getInstance():IFoo {
if (_instance == null) _instance = new Impl();
return _instance;
}
}
class Impl implements IFoo {
public function foo():void {
trace("fooooooooooooooooooo");
}
}
this doesn't rely on runtime errors for safety. Also, it lowers coupling.
greetz
back2dos
public class Singleton {
private static var _instance:Singleton;
public **static** function get instance():Singleton
{
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
public function Singleton()
{
if (_instance != null) throw new Error("You can't create Singleton twice!");
}
}
Runtime check in lack of private constructor.
I use this approach ...
package
{
public class Main
{
private static var _instance:Main;
private static var _singletonLock:Boolean = false;
/**
* Creates a new instance of the class.
*/
public function Main()
{
if (!_singletonLock) throw new SingletonException(this);
}
/**
* Returns the singleton instance of the class.
*/
public static function get instance():Main
{
if (_instance == null)
{
_singletonLock = true;
_instance = new Main();
_singletonLock = false;
}
return _instance;
}
}
}
... not as terse as some other methods but it's absolutely safe and there's no need for an empty package-level class. Also note the shortcut with SingletonException which is a class that extends the AS3 Error class and saves typing some code when using more than one Singleton ...
package
{
public class SingletonException extends Error
{
public function SingletonException(object:Object)
{
super("Tried to instantiate the singleton " + object + " through it's constructor."
+ " Use the 'instance' property to get an instance of this singleton.");
}
}
}

Resources