1

I have a WPF module that gets data in form of interfaces, e.g. ICustomer. The real Customer object is defined in another module, that I can't reference in the WPF module because of lose coupling.

Since we want design data in our projects, and I want to avoid to write Fakes for every entity, I thought that using Moq would help here, so I made a small example:

public MainVM()
{
    if (IsInDesignMode)
    {
        var c1 = new Mock<ICustomer>();
        c1.SetupAllProperties();
        c1.FirstName = "John";
        c1.LastName = "Doe";

        Customers = new List<ICustomer>
        {
            c1.Object,
        };
    }
}

In the view I bring in the datacontext and do:

d:DataContext="{d:DesignInstance vm:MainVM, IsDesignTimeCreatable=True}"

The problem now is, that the line in the view throws me an exception:

Unable to cast object of type 'Castle.Proxies.ICustomerProxy_5' to 'Prototype.Data.ICustomer'

How can I get rid of this error? Is there some workaround?

LueTm
  • 2,366
  • 21
  • 31
  • Interesting use for Moq. Also technically in MVVM you shouldn't expose your model anywhere. The list of `ICustomer` objects (if it is indeed a model entity) violates that; you should wrap them in customer view models. – Patrick Quirk Dec 11 '14 at 17:06
  • Well... as I read this is quite the debate, whether it is kosher to do that or not. We think it's OK, weighted against the immense work the view models would generate. Am I wrong? – LueTm Dec 11 '14 at 17:09
  • I don't think the work is immense, it's just a wrapper class. The VM would let you add actions/other properties to your entity for display purposes if needed, giving you current and future flexibility. But MVVM is a style, not a ruleset. Do what works for you! – Patrick Quirk Dec 11 '14 at 17:12
  • 2
    Does this help? http://stackoverflow.com/questions/20865810/unable-to-cast-object-of-type-castle-proxies-xproxy-to-type-x-in-wpf-designe – Erti-Chris Eelmaa Dec 11 '14 at 17:15
  • I saw that one, but I am clueless of what he says he's doing with the assemblies in his answer... :/ – LueTm Dec 11 '14 at 17:27
  • `in MVVM you shouldn't expose your model anywhere` uh.... And in the linked question, it seems like some assembly loading weirdness is the cause, and autoincrementing the assembly version helps fix this. So open up AssemblyInfo.cs and use the `*` thingie in your version number to auto increment the build/revision numbers, like `[assembly: AssemblyVersion("5.0.*")]` –  Dec 11 '14 at 18:02
  • You seem to be missing an `.Object` when you set the properties `FirstName` and `LastName`? – Jeppe Stig Nielsen Dec 15 '14 at 08:30

0 Answers0