views:

90

answers:

1

Hello stack,

there have been quite some posts about this, all trying to serialize a Func delegate.

But could someone think of an alternative, when the use of the delegate is always clear?

We have a generic create command, which takes a delegate as paramater in the constructor. This delegate will create the Item for the create command:

public class CreateCommand<T> : Command
{
    public T Item;
    protected Func<T> Constructor;

    public ClientCreateCommand(Func<T> constructor)
    {
        Constructor = constructor;
    }

    public override void Execute()
    {
        Item = Constructor();
    }
}

The command is used like this:

var c = new CreateCommand<MyType>( () => Factory.CreateMyType(param1, param2, ...) );
History.Insert(c);

Then the History serializes the command and sends it to the server. ofc the delegate can't be serialized as is and we get an exception.

Now could someone think of a very simple Constructor class that can be serialized and does the same job than the lambda expresseion? Means it takes a list of paramters and returns an instance of type T, that we then can write somethink like this:

var constructor = new Constructor<MyType>(param1, param2, ...);
var c = new CreateCommand<MyType>(constructor);
History.Insert(c);

How would the Constructor class look like? Thanks for any ideas!

+1  A: 

EDIT(2): I've provided a couple of complete example implementations. They are categorized below as "Implementation 1" and "Implementation 2".

Your delegate is essentially a factory. You could define a factory interface and create a class that implements that interface for your Item class. Below is an example:

public interface IFactory<T>
{
    T Create();
}

[Serializable]
public class ExampleItemFactory : IFactory<T>
{
    public int Param1 { get; set; }

    public string Param2 { get; set; }

    #region IFactory<T> Members

    public Item Create()
    {
        return new Item(this.Param1, this.Param2);
    }

    #endregion
}

public class CreateCommand<T> : Command
{
    public T Item;
    protected IFactory<T> _ItemFactory;

    public CreateCommand(IFactory<T> factory)
    {
        _ItemFactory = factory;
    }

    public override void Execute()
    {
        Item = _ItemFactory.Create();
    }
}

You would utilize this code in the following manner:

        IFactory<Item> itemFactory = new ExampleItemFactory { Param1 = 5, Param2 = "Example!" };
        CreateCommand<Item> command = new CreateCommand<Item>(itemFactory);
        command.Execute();

EDIT(1): The specific implementations of IFactory<T> that your application needs will be up to you. You could create specific factory classes for each class that you need, or you could create some kind of factory that dynamically creates an instance using, for example, the Activator.CreateInstance fucntion or perhaps using some kind of Inversion of Control framework such as Spring or StructureMap.

Below is a complete example implementation that uses two factory implementations. One implementation can create any type given an array of arguments using that type's constructor with matching parameters. Another implementation creates any type that has been registered with my "Factory" class.

The Debug.Assert statements ensure that everything is behaving as intended. I ran this application without error.

Implementation 1

[Serializable]
public abstract class Command
{
    public abstract void Execute();
}

public class Factory
{
    static Dictionary<Type, Func<object[], object>> _DelegateCache = new Dictionary<Type, Func<object[], object>>();

    public static void Register<T>(Func<object[], object> @delegate)
    {
        _DelegateCache[typeof(T)] = @delegate;
    }

    public static T CreateMyType<T>(params object[] args)
    {
        return (T)_DelegateCache[typeof(T)](args);
    }
}

public interface IFactory<T>
{
    T Create();
}

[Serializable]
public class CreateCommand<T> : Command
{
    public T Item { get; protected set; }
    protected IFactory<T> _ItemFactory;

    public CreateCommand(IFactory<T> itemFactory)
    {
        this._ItemFactory = itemFactory;
    }

    public override void Execute()
    {
        this.Item = this._ItemFactory.Create();
    }
}

// This class is a base class that represents a factory capable of creating an instance using a dynamic set of arguments.
[Serializable]
public abstract class DynamicFactory<T> : IFactory<T>
{
    public object[] Args { get; protected set; }

    public DynamicFactory(params object[] args)
    {
        this.Args = args;
    }

    public DynamicFactory(int numberOfArgs)
    {
        if (numberOfArgs < 0)
            throw new ArgumentOutOfRangeException("numberOfArgs", "The numberOfArgs parameter must be greater than or equal to zero.");

        this.Args = new object[numberOfArgs];
    }

    #region IFactory<T> Members

    public abstract T Create();

    #endregion
}

// This implementation uses the Activator.CreateInstance function to create an instance
[Serializable]
public class DynamicConstructorFactory<T> : DynamicFactory<T>
{
    public DynamicConstructorFactory(params object[] args) : base(args) { }

    public DynamicConstructorFactory(int numberOfArgs) : base(numberOfArgs) { }

    public override T Create()
    {
        return (T)Activator.CreateInstance(typeof(T), this.Args);
    }
}

// This implementation uses the Factory.CreateMyType function to create an instance
[Serializable]
public class MyTypeFactory<T> : DynamicFactory<T>
{
    public MyTypeFactory(params object[] args) : base(args) { }

    public MyTypeFactory(int numberOfArgs) : base(numberOfArgs) { }

    public override T Create()
    {
        return Factory.CreateMyType<T>(this.Args);
    }
}

[Serializable]
class DefaultConstructorExample
{
    public DefaultConstructorExample()
    {
    }
}

[Serializable]
class NoDefaultConstructorExample
{
    public NoDefaultConstructorExample(int a, string b, float c)
    {
    }
}

[Serializable]
class PrivateConstructorExample
{
    private int _A;
    private string _B;
    private float _C;

    private PrivateConstructorExample()
    {
    }

    public static void Register()
    {
        // register a delegate with the Factory class that will construct an instance of this class using an array of arguments
        Factory.Register<PrivateConstructorExample>((args) =>
            {
                if (args == null || args.Length != 3)
                    throw new ArgumentException("Expected 3 arguments.", "args");

                if (!(args[0] is int))
                    throw new ArgumentException("First argument must be of type System.Int32.", "args[0]");

                if (!(args[1] is string))
                    throw new ArgumentException("Second argument must be of type System.String.", "args[1]");

                if (!(args[2] is float))
                    throw new ArgumentException("Third argument must be of type System.Single.", "args[2]");

                var instance = new PrivateConstructorExample();

                instance._A = (int)args[0];
                instance._B = (string)args[1];
                instance._C = (float)args[2];

                return instance;
            });
    }
}

class Program
{
    static void Main(string[] args)
    {
        var factory1 = new DynamicConstructorFactory<DefaultConstructorExample>(null);
        var command1 = new CreateCommand<DefaultConstructorExample>(factory1);

        var factory2 = new DynamicConstructorFactory<NoDefaultConstructorExample>(3);
        factory2.Args[0] = 5;
        factory2.Args[1] = "ABC";
        factory2.Args[2] = 7.1f;
        var command2 = new CreateCommand<NoDefaultConstructorExample>(factory2);

        PrivateConstructorExample.Register(); // register this class so that it can be created by the Factory.CreateMyType function
        var factory3 = new MyTypeFactory<PrivateConstructorExample>(3);
        factory3.Args[0] = 5;
        factory3.Args[1] = "ABC";
        factory3.Args[2] = 7.1f;
        var command3 = new CreateCommand<PrivateConstructorExample>(factory3);

        VerifySerializability<DefaultConstructorExample>(command1);
        VerifySerializability<NoDefaultConstructorExample>(command2);
        VerifySerializability<PrivateConstructorExample>(command3);
    }

    static void VerifySerializability<T>(CreateCommand<T> originalCommand)
    {
        var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        using (var stream = new System.IO.MemoryStream())
        {
            System.Diagnostics.Debug.Assert(originalCommand.Item == null); // assert that originalCommand does not yet have a value for Item
            serializer.Serialize(stream, originalCommand); // serialize the originalCommand object

            stream.Seek(0, System.IO.SeekOrigin.Begin); // reset the stream position to the beginning for deserialization

            // deserialize
            var deserializedCommand = serializer.Deserialize(stream) as CreateCommand<T>;
            System.Diagnostics.Debug.Assert(deserializedCommand.Item == null); // assert that deserializedCommand still does not have a value for Item
            deserializedCommand.Execute();
            System.Diagnostics.Debug.Assert(deserializedCommand.Item != null); // assert that deserializedCommand now has a value for Item
        }
    }
}

EDIT(2): After re-reading the question, I think I got a better idea of what the asker was really trying to get at. Essentially, we still want to take advantage of the flexibility offered by lambda expressions / anonymous delegates, but avoid the serialization issues.

Below is another example implementation that utilizes a Factory<T> class to store delegates used to return instances of type T.

Implementation 2

[Serializable]
public abstract class Command
{
    public abstract void Execute();
}

[Serializable]
public abstract class CreateCommand<T> : Command
{
    public T Item { get; protected set; }
}

public class Factory<T>
{
    private static readonly object _SyncLock = new object();
    private static Func<T> _CreateFunc;
    private static Dictionary<string, Func<T>> _CreateFuncDictionary;

    /// <summary>
    /// Registers a default Create Func delegate for type <typeparamref name="T"/>.
    /// </summary>
    public static void Register(Func<T> createFunc)
    {
        lock (_SyncLock)
        {
            _CreateFunc = createFunc;
        }
    }

    public static T Create()
    {
        lock (_SyncLock)
        {
            if(_CreateFunc == null)
                throw new InvalidOperationException(string.Format("A [{0}] delegate must be registered as the default delegate for type [{1}]..", typeof(Func<T>).FullName, typeof(T).FullName));

            return _CreateFunc();
        }
    }

    /// <summary>
    /// Registers a Create Func delegate for type <typeparamref name="T"/> using the given key.
    /// </summary>
    /// <param name="key"></param>
    /// <param name="createFunc"></param>
    public static void Register(string key, Func<T> createFunc)
    {
        lock (_SyncLock)
        {
            if (_CreateFuncDictionary == null)
                _CreateFuncDictionary = new Dictionary<string, Func<T>>();

            _CreateFuncDictionary[key] = createFunc;
        }
    }

    public static T Create(string key)
    {
        lock (_SyncLock)
        {
            Func<T> createFunc;
            if (_CreateFuncDictionary != null && _CreateFuncDictionary.TryGetValue(key, out createFunc))
                return createFunc();
            else
                throw new InvalidOperationException(string.Format("A [{0}] delegate must be registered with the given key \"{1}\".", typeof(Func<T>).FullName, key));
        }
    }
}

[Serializable]
public class CreateCommandWithDefaultDelegate<T> : CreateCommand<T>
{
    public override void Execute()
    {
        this.Item = Factory<T>.Create();
    }
}

[Serializable]
public class CreateCommandWithKeyedDelegate<T> : CreateCommand<T>
{
    public string CreateKey { get; set; }

    public CreateCommandWithKeyedDelegate(string createKey)
    {
        this.CreateKey = createKey;
    }

    public override void Execute()
    {
        this.Item = Factory<T>.Create(this.CreateKey);
    }
}

[Serializable]
class DefaultConstructorExample
{
    public DefaultConstructorExample()
    {
    }
}

[Serializable]
class NoDefaultConstructorExample
{
    public NoDefaultConstructorExample(int a, string b, float c)
    {
    }
}

[Serializable]
class PublicPropertiesExample
{
    public int A { get; set; }
    public string B { get; set; }
    public float C { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        // register delegates for each type
        Factory<DefaultConstructorExample>.Register(() => new DefaultConstructorExample());
        Factory<NoDefaultConstructorExample>.Register(() => new NoDefaultConstructorExample(5, "ABC", 7.1f));
        Factory<PublicPropertiesExample>.Register(() => new PublicPropertiesExample() { A = 5, B = "ABC", C = 7.1f });

        // create commands
        var command1 = new CreateCommandWithDefaultDelegate<DefaultConstructorExample>();
        var command2 = new CreateCommandWithDefaultDelegate<DefaultConstructorExample>();
        var command3 = new CreateCommandWithDefaultDelegate<DefaultConstructorExample>();

        // verify that each command can be serialized/deserialized and that the creation logic works
        VerifySerializability<DefaultConstructorExample>(command1);
        VerifySerializability<DefaultConstructorExample>(command2);
        VerifySerializability<DefaultConstructorExample>(command3);


        // register additional delegates for each type, distinguished by key
        Factory<DefaultConstructorExample>.Register("CreateCommand", () => new DefaultConstructorExample());
        Factory<NoDefaultConstructorExample>.Register("CreateCommand", () => new NoDefaultConstructorExample(5, "ABC", 7.1f));
        Factory<PublicPropertiesExample>.Register("CreateCommand", () => new PublicPropertiesExample() { A = 5, B = "ABC", C = 7.1f });

        // create commands, passing in the create key to the constructor
        var command4 = new CreateCommandWithKeyedDelegate<DefaultConstructorExample>("CreateCommand");
        var command5 = new CreateCommandWithKeyedDelegate<DefaultConstructorExample>("CreateCommand");
        var command6 = new CreateCommandWithKeyedDelegate<DefaultConstructorExample>("CreateCommand");

        // verify that each command can be serialized/deserialized and that the creation logic works
        VerifySerializability<DefaultConstructorExample>(command4);
        VerifySerializability<DefaultConstructorExample>(command5);
        VerifySerializability<DefaultConstructorExample>(command6);
    }

    static void VerifySerializability<T>(CreateCommand<T> originalCommand)
    {
        var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        using (var stream = new System.IO.MemoryStream())
        {
            System.Diagnostics.Debug.Assert(originalCommand.Item == null); // assert that originalCommand does not yet have a value for Item
            serializer.Serialize(stream, originalCommand); // serialize the originalCommand object

            stream.Seek(0, System.IO.SeekOrigin.Begin); // reset the stream position to the beginning for deserialization

            // deserialize
            var deserializedCommand = serializer.Deserialize(stream) as CreateCommand<T>;
            System.Diagnostics.Debug.Assert(deserializedCommand.Item == null); // assert that deserializedCommand still does not have a value for Item
            deserializedCommand.Execute();
            System.Diagnostics.Debug.Assert(deserializedCommand.Item != null); // assert that deserializedCommand now has a value for Item
        }
    }
}
Dr. Wily's Apprentice
yes, but thats basically the same as writing a create command for each type as we would have to write a factory for each type. the goal is to have one class which can create all types with a dynamic set of parameters... sometimes we don't call a factory to create the instance, but pass the constructor directly like: () => new OtherType(param)
thalm
I see, but you should be able to provide whatever factory implementation you want, whether it's specific to a particular class or general enough to dynamically create any type. I've updated my answer with examples of the latter.
Dr. Wily's Apprentice
I re-read your question and comment, and I think I understand now that what you'd like to do is still use delegates (or an equivalently "nice" mechanism) to define the creation logic but to somehow circumvent the serialization issue. I think I accidentally came up with an alternative idea in my example; see the "Factory" class and the PrivateConstructorExample class. Basically, there is a static dictionary that stores the delegates, so they avoid getting serialized. I'll update with a cleaner example of just that.
Dr. Wily's Apprentice
thank you very much for this detailed example.. saw it right now, as i dont get noticed when there are comments.. i will try that tomorrow, looks very promising!
thalm