icubilla / jsonexserializer

Automatically exported from code.google.com/p/jsonexserializer
0 stars 0 forks source link

Advanced Customization #12

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Allow for advanced customization scenarios such as using factories or
dependency injection to create instances of types as well as non-standard
type meta-data such as a getter/setter method pair instead of a standard
property.

1. Change Serializer/Deserializer code to depend on interfaces for
TypeHandler and PropertyHandler.
2. A TypeHandler factory will produce instances of ITypeHandler and will be
available on the SerializationContext.
3. The DefaultTypeHandlerFactory will cache TypeHandler instances by default.
4. IPropertyHandler will not expose PropertyInfo property, this will allow
for "properties" that are not bound to actual properties on an object such
as public fields, or getter/setter method pair.
5. Default TypeHandler should add support for public fields.
6. SerializationContext will expose an IDictionary property for custom
converters,typehandlers, etc to use.

Example Dependency Injection scenario:
public class DITypeHandlerFactory : TypeHandlerFactory {
   public DITypeHandlerFactory(SerializationContext ctx) : base(ctx) {
   }

   protected override ITypeHandler CreateNew(Type t) {
      return new DITypeHandler(t, _context);
   }

}

public class DITypeHandler : TypeHandler {

   public DITypeHandler(Type t, SerializationContext ctx) : base(t, ctx) {
   }

   public override object CreateInstance(object[] args) {
       MyFMyFavoriteDIImpl container = (MyFavoriteDIImpl)
ctx.Data["Container"];
       return container.Resolve(typeof(ForType));
   } 
}

// Setup Code
MyFavoriteDIImpl container = new MyFavoriteDIImpl("someconfig.xml");
Serializer s = Serializer.GetSerializer(typeof(MyClass));
s.SerializationContext.Data["Container"] = container;
s.SerializationContext.TypeHandlerFactory = new
DITypeHandlerFactory(s.SerializationContext);

Original issue reported on code.google.com by elliott....@gmail.com on 21 Jan 2008 at 3:09

GoogleCodeExporter commented 9 years ago
Should probably just add a default CustomTypeHandlerFactory that takes a Type
parameter that tells it which TypeHandler implementation class to use.  It would
replace the DITypeHandlerFactory above.  Usage:
s.SerializationContext.TypeHandlerFactory = new
CustomTypeHandlerFactory(s.SerializationContext, typeof(DITypeHandler));

Original comment by elliott....@gmail.com on 21 Jan 2008 at 3:13

GoogleCodeExporter commented 9 years ago

Original comment by elliott....@gmail.com on 25 Jan 2008 at 5:47

GoogleCodeExporter commented 9 years ago

Original comment by elliott....@gmail.com on 10 Jul 2008 at 5:01