Remove explicit only interface for IEnumerable, and IList for JArray #79

Closed
wants to merge 1 commit into
from

Projects

None yet

3 participants

@jbtule
jbtule commented Apr 29, 2013

Although for pretty much all standard usages, having an explicit IEnumerable interface is not an issue.

However, there are some dlr issues with this, and by removing this limitation it allows dynamic JObjects to be wrapped with an Interface using ImpromptuInterface similar to what Mark Rendle does with HOWTO: Dial up the static on Simple.Data

Also referenced on StackOverflow

@danderson00

It would be extremely useful to be able to deserialise JSON into DTOs constructed purely with interfaces:

JsonConvert.DeserializeObject<ImpromptuDictionary>(json).ActLike<TInterface>()

The pull request enables this to work with JSON arrays.

@jbtule
jbtule commented Apr 29, 2013

Actually @danderson00, ImpromptuDictionary isn't even needed here with this fix, as JObject.Parse returns a perfectly valid object graph of IDynamicMetaObjectProvider's

void Main()
{
    ICustomer customer = Impromptu.ActLike(JObject.Parse(@"
        {
            Id: 1,
            Name:'Test',
            Location:'Somewhere',
            Employees: [
                { Id:1, EmployerId:39421, Name:'Joe' },
                { Id:2, EmployerId:39421, Name:'Jane' },
            ]
        }
    "));
    // Without pull request, Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 
    //  'Newtonsoft.Json.Linq.JArray' does not contain a definition for 'GetEnumerator'
    foreach(var employee in customer.Employees){
        employee.Id.Dump();
        employee.Name.Dump();
    }
}

    public interface ICustomer
    {
        int Id { get; set; }
        string Name { get; set; }
        string Location { get; set; }
        IList<IEmployee> Employees { get; } 
    }

    public interface IEmployee
    {
        int Id { get; set; }
        int EmployerId { get; set; }
        string Name { get; set; }
    }
@JamesNK
Owner
JamesNK commented Apr 30, 2013

I made those changes but when I attempt to run the code above I get this error as soon as GetEnumerator is called:

Operator '==' cannot be applied to operands of type 'System.Collections.Generic.List<Newtonsoft.Json.Linq.JToken>.Enumerator' and '<null>'

I think it is because the enumerator is a struct.

I'm using version 6.0.6 of ImpromptuInterface. Why does it work for you and not for me? I don't want to change anything unless it actually fixes a problem.

@jbtule
jbtule commented Apr 30, 2013

Whoops, looks like I was sitting on a newer version of Impromptu, I'll get back to you.

@jbtule
jbtule commented Apr 30, 2013

Released ImpromptuInterface 6.1.4, which had a fix for the dynamic binding == operator issue. Now the above code should work for you too. Sorry about that!

@JamesNK
Owner
JamesNK commented May 1, 2013

Much better. ImpromptuInterface + LINQ to JSON is pretty cool.

@JamesNK JamesNK closed this May 1, 2013
@danderson00

This is sweet! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment