Can I avoid duplicating my business layer? (.NET)

前端 未结 1 1222
一向
一向 2021-01-27 02:24

I have a tech demo application to write, with multiple clients: web client (ASP MVC 3), desktop client (currently it\'ll be a WCF app, planning

相关标签:
1条回答
  • 2021-01-27 03:17

    I've built cross-platform apps before on Windows (specifically WPF / Silverlight clients, C# SQL Server back end) and what I've done is this.

    • Employed a messaging layer or middleware (RabbitMQ for asynchronous messaging, or webservices for request/response) and serialization technology (Protobuffers or JSON) that works across all three boundaries. Use a cross platform middleware where possible. This is why I mention RabbitMQ / protobuffers / JSON.
    • Moved all business logic to the server - the client cannot access the database at all and has to go through the middleware. Clients, including desktop, literally become like thin browsers accessing middleware services for all operations.
    • Created shared assemblies to hold messages, datacontracts and common classes between client(s) and server which can be referenced by all clients and the server.

    This last step (shared assemblies) involves creating 1 project which is full C# compatible (so works on WPF Desktop or Server) and additional projects for each web client (Silverlight, WP7). To dual deploy code on multiple clients you have one copy of the source in the desktop assemblie and then "Add as Link" the same source files to Silverlight / WP7 assemblies. You will have to have a few #if preprocessor statements but by and large it is possible to dual deploy large chunks of code using this method.

    Your stack becomes

    Clients:

    all clients have serialization, client side implementations of webservices/middleware and MVVM / MVC patterns

    Middleware:

    middleware implemetation on server / wpf desktop can be the same. Wp7 and monodroid etc will need to be different

    Messages/DataContracts:

    shared across all clients/server using the techniques I outlined above

    Server:

    All business logic, DB access and server-side middleware implementations. For DB Access I've used PetaPoco as an excellent MicroORM. I believe a microORM implementation powers stack overflow but I might be mistaken.

    I would also take a look at this article for inspiration. It is possible - I've written client/server apps with SL/WPF clients and C# servers, also WPF client/Java servers using the above methods.

    Best regards,

    0 讨论(0)
提交回复
热议问题