15

What architecture and patterns can I use to share the most model and logic code between a WPF and an ASP.NET MVC application?

I am trying to achieve a bit more here than just separating my data entities from the two presentation projects. There is a lot more in common e.g. UI logic on what gets displayed under what conditions, when is something required, etc. that I would like to keep in the shared code.

ADDED: I am just beginning to really like the concept of view models independent of my entity model driving my presentation. While some of the annotations used in these are located in assemblies specific to MVC, none of the metadata provided is actually web specific. I would very much like to explore using my MVC view models as data sources for binding to WPF views. Any suggestions on this front will be most appreciated.

1
  • I asked similar question here. Did you succeed reusing ViewModels? Commented Jun 15, 2017 at 14:45

7 Answers 7

6

My personal favorite configuration is similar to the one Adam King suggested above but I like to keep the logic DLL as part of the web project. I run a project called CT Terminal that follows this pattern. My Terminal.Domain project contains all the application logic and simply returns a CommandResult object with properties that act as instructions to tell the UI project what to do. The UI is completely dumb and only processes what it's told to by the Domain project.

Now, following Adam King's approach I would then slap that Domain DLL into a WPF app and then code the UI to follow the instructions in my returned CommandResult object. However, I prefer a different approach. I wrote the MVC 3 UI to expose a JSON API. This API can be consumed by any application. The JSON API was simple because it was basically a wrapper around my Terminal.Domain project CommandResult object. The JSON returned would have the same basic properties. In this way I would write the WPF app to consume this API rather than the DLL. Now if I make minor changes to internal application logic I just deploy the Web project to the live server. All clients using the API automatically get this new logic.

Obviously if the changes being made affect the properties being returned from the API then that would require a release of new client code, but at least for internal logic you wouldn't have to do that.

Sign up to request clarification or add additional context in comments.

2 Comments

I kind of like this approach. It offers more than just sharing data entities between the different presentation projects.
Sorry, I'm not sure what happened to that project but the concept was duplicated in a newer node.js project called shotgun. Instead of a class library it's a node module.
3

One of the most widely used patterns seems to be having the Entities in a seperate DLL assembly, then having this referenced from each of the other projects.

MVC 3 suits the repository pattern very nicely, which can be a clean route to take in the first instance, and will work for both WPF and ASP.net

Comments

2

I actually found Rocky Lhotka's books, software, and videos on this topic very helpful. Here's a few links to his content:

http://www.lhotka.net/

http://channel9.msdn.com/Events/Speakers/Rockford-Lhotka

http://www.amazon.com/Expert-C-2008-Business-Objects/dp/1430210192/ref=sr_1_2?s=books&ie=UTF8&qid=1331834548&sr=1-2

3 Comments

what specifically applies to the question asked here?
Rocky has a .NET library for models and view models that works quite well. I think its design, as explained in his videos, lends itself to very reusable code. He discusses the WPF DependencyObject property model and implements a similar thing in his library. I think that is a useful feature to take to MSMVC.
Just an aside: Lhotka's Professional VB6 Business Objects was the bible on which I founded my career as an independent developer.
1

Create a service layer for your application by specifying interfaces with methods that represent all of the operations you need to perform. Also, in this service layer, define all of the data types used by the application. Those data type classes should contain only properties, not operations. Put these interfaces and classes in an assembly all by itself. This assembly should be shared between your web app, WPF app, and the code that implements it.

Finally once you have this separation, you can freely develop the application's internal structure, and leave the responsibility of UI operations (e.g. what happens when you click xyz button) to the respective UI.

As an aside, you can expose your service layer, via WCF and web services. You can use this to make call from the web browser via javascript. You could do things like client-side validation or even look up values on the fly for drop down population. all while reusing it between your two application.

Comments

1

Starting with the obvious. Encapsulate your business logic and domain model in a separate assembly.

In terms of Presentation Layers and shared UI Behaviour, the closest you will get is the MVVM design paradigm, implementation will be C# in WPF/XAML and Javascript for your ASP.NET MVC web frontend.

For the web frontend you can get close to the WPF (MVVM) way of doing things with http://knockoutjs.com/ written by Steve Sanderson of Microsoft. Its MVVM for the browser. Also checkout http://www.asp.net/mvc/mvc4 for more info.

Comments

1

Use Web Api, let both the WPF and the Web application consume the services from Web Api. Done.

1 Comment

I like this approach, it seems so simple and elegant, but how to you share the main data model entities to the WPF app, especially if you want to enforce validation and serialization with data annotations and attributes? do I have to redefine the model classes specifically for each platform? this seems to almost defeat the entire benefit of using WebAPI since I'll have to keep the models in the different platforms in sync...
1

Did you try using Portable class libraries. With this you can make the data layer and use it in ASP.Net MVC, WPF, Windows Phone, Silverlight.

1 Comment

can you (or anyone else reading this) point to any resources specifically discussing using Portable class libraries to target both the Web and Win8/WinPhone/WPF? I like NPehrsson's suggestion of using the WebAPI to feed from the web to other platforms, and would appreciate any additional insight on my comments there as well! many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.