Search code examples
asp.netwcfviewmodelautomapperpresentation

WCF decoupling via ViewModel facade ASP.NET


I am consuming several ASP.NET 4, WCF services in C# and want to know the best way of decoupling my code from the WCF structure. If this is not too much work as the client probably doesn't care.

I created a facade layer so my WebForms UI can talk to it using flat simple types like string, int and datetime. The facade generates and read the Request/Response objects required by the WCF. I read the Response object and convert it into a simpler Presentation object for the client to use. However the Response object returns some complex object types, such as OrderHistory which contains OrderItem, which in itself contains for e.g. DeliveryAddress.

Now should I be recreating all these complex objects as Views? i.e. OrderPresentation contains IEnumerable-OrderHistoryView-, which contains IEnumerable-OrderItemView-, which contains DeliveryAddressView or am I misinterpreting this?

For some of the simpler objects I have been able to use AutoMapper but not sure how or if I should apply it to nested complex objects. Maybe I should not bother decoupling so much and just bind my facade and presentation objects to the WCF model?

Help please? any good tutorial examples on this?

I'm getting somewhere with:

        public static IList<OrderHistoryView> ConvertToOrderHistoryView(this OrderHistory[] orderHistory)
    {
        return Mapper.Map<OrderHistory[], IList<OrderHistoryView>>(orderHistory);
    }

and

            Mapper.CreateMap<OrderHistory, OrderHistoryView>()
            .ForMember(dest => dest.Items, opt => opt.MapFrom(src => src.Items));

Solution

  • If you write some DTO classes to dump the data from WCF into, then you should be able to use AutoMapper to handle the mapping. I'm not really sure why, though. Is there any realistic scenario where you'd ever be able to take this app, connect it to a different service, and NOT have to make significant changes to the client code?

    If the answer is no and your client is pretty well dependent on the services in question, then you're just wasting energy worrying about it. I guarantee that your users don't care.