简体   繁体   English

您将如何在C#3.0中构建桌面应用程序

[英]How would you architect a desktop application in C# 3.0

I've created a simple desktop application in C# 3.0 to learn some C#, wpf and .Net 3.5. 我在C#3.0中创建了一个简单的桌面应用程序来学习一些C#,wpf和.Net 3.5。 My application essentially reads data from a csv file and stores it in a SQL server CE database. 我的应用程序基本上从csv文件中读取数据并将其存储在SQL Server CE数据库中。 I use sqlmetal to generate the ORM code for the database. 我使用sqlmetal为数据库生成ORM代码。 My first iteration of this app is ugly as hell and I'm in the process of refactoring it. 我对这个应用程序的第一次迭代是丑陋的,我正在重构它。

Which brings me to my question. 这让我想到了我的问题。 How would you architect a desktop database app in C#? 您将如何在C#中构建桌面数据库应用程序? What are the best practices? 什么是最佳做法?

Do you create a Database Abstraction Layer (DAL) which uses the sqlmetal generated code? 您是否创建了使用sqlmetal生成的代码的数据库抽象层(DAL)? Or is the generated code enough of an abstraction? 或者生成的代码是否足以构成抽象?

If you use DAL pattern, do you make it a singleton or a static member? 如果使用DAL模式,是将它设为单例还是静态成员? Do you use the View-Model-ModelView pattern with the DAL pattern? 您是否将View-Model-ModelView模式与DAL模式一起使用?

Apologies if this seems like a long open ended question, but I have been giving this a lot of thought recently. 如果这看起来像一个长期未解决的问题,请道歉,但我最近一直在考虑这个问题。 I see a lot of examples on how to architect an enterprise n-tier app in C# but not that many on architecting standalone desktop apps. 我看到很多关于如何在C#中构建企业n层应用程序的示例,但在构建独立桌面应用程序时却没有那么多。

I would start with the Composite Application Guidance for WPF ( cough PRISM cough ) from Microsoft's P&P team. 我将从微软P&P团队的WPF综合应用指南咳嗽 PRISM 咳嗽 )开始。 With the download comes a great reference application that is the starting point for most of my WPF development today. 随着下载提供了一个很棒的参考应用程序,它是我今天大多数WPF开发的起点。

The DotNetRocks crew just interviewed Glenn Block and Brian Noyes about this if you're interested in hearing more from them. DotNetRocks工作人员刚刚采访了Glenn BlockBrian Noyes ,如果你有兴趣听取他们的更多信息。

Even better, Prism is not nearly as heavy as the CAB was, if you're familiar at all with that from the WinForms days. 更好的是,Prism并不像CAB那么重,如果你对WinForms时代的那些熟悉的话。

The answer is "it depends" as always. 答案是“它取决于”一如既往。

A few things to think about: You may want to make this fat client app a web app (for example) at some point. 需要考虑的一些事项:您可能希望在某些时候将这个胖客户端应用程序设置为Web应用程序(例如)。 If so, you should be sure to keep separation between the business layer (and below) and the presentation. 如果是这样,您应确保在业务层(及以下)与演示文稿之间保持分离。 The simplest way to do this is to be sure all calls to the business logic go through an interface of some kind. 最简单的方法是确保对业务逻辑的所有调用都通过某种接口。 A more complex way is to implement a full MVC setup. 更复杂的方法是实现完整的MVC设置。

Another thing you may consider is making the data access layer independent of the business logic and user interface. 您可以考虑的另一件事是使数据访问层独立于业务逻辑和用户界面。 By this I mean that all calls from business logic into the DAL should be generic "get me this data" rather than "get me this data from SQL" or even worse "run this SQL statement". 我的意思是,从业务逻辑到DAL的所有调用应该是通用的“获取我的数据”,而不是“从SQL获取此数据”,甚至更糟糕的“运行此SQL语句”。 In this way, you can replace your DAL with one that accesses a different database, XML files, or even something icky like flat files. 通过这种方式,您可以将DAL替换为访问不同数据库,XML文件或甚至像平面文件之类的东西的DAL。

In short, separation of concerns. 简而言之,关注点分离。 This allows you to grow in the future by adding a different UI, segmenting all three areas into their own tier, or changing the relevant technology. 这使您可以通过添加不同的UI,将所有三个区域划分为自己的层或更改相关技术来增长。

Before architecting anything you should define requirements for your app. 在构建任何内容之前,您应该为应用程序定义需求。
It's a common error of beginner developers - starting writing code ahead of thinking about how it would perform. 这是初学者开发人员的一个常见错误 - 在考虑代码如何执行之前开始编写代码。 My advice will be to try to describe some feature of you application. 我的建议是尝试描述您的应用程序的一些功能。 It will help you to feel how it should be implemented. 它将帮助您了解应如何实施。

As for useful learning resources I would highly recommend you to take a look at CompositeWPF it's a project designed specifically to teach developers best practices of desktop app development. 至于有用的学习资源,我强烈建议您看看CompositeWPF这是一个专门用于教授开发人员桌面应用程序开发最佳实践的项目。

I'd start with Jeremy Miller's Build Your Own Cab series. 我将从Jeremy Miller的Build Your Own Cab系列开始。

I was an early CAB adopter. 我是CAB的早期采用者。 I learned a lot from digging into that technology and reading all the .NET blogs about application architecture. 我从挖掘技术和阅读有关应用程序架构的所有.NET博客中学到了很多东西。

But recently I had a chance to start a new project, and instead of using CAB I went with StructureMap & NHibernate and borrowed some of the patterns that Jeremy uses (in particular, his way of handling event aggregation). 但最近我有机会开始一个新项目,而不是使用CAB,我使用了StructureMap和NHibernate并借用了Jeremy使用的一些模式(特别是他处理事件聚合的方式)。 The result was a really simplified, hand-tooled framework that does everything I need and I love working with it. 结果是一个非常简化的手工框架,它可以完成我需要的一切,我喜欢使用它。

As to the specifics of your question: I use a Repository for data access. 至于你的问题的具体细节:我使用存储库进行数据访问。 I initially wrote some ADO.NET code and used data readers and mapped my objects. 我最初编写了一些ADO.NET代码并使用了数据读取器并映射了我的对象。 But that got old real fast, so I grabbed NHibernate and was really pleased. 但那真的很快,所以我抓住了NHibernate并且非常高兴。 The repositories use NHibernate for data access, and my data access needs are pretty simple in this particular app. 存储库使用NHibernate进行数据访问,在这个特定的应用程序中,我的数据访问需求非常简单。

I have a service layer (exposed via WCF, Duplex channels) that utilizes the repositories. 我有一个利用存储库的服务层(通过WCF,双工通道公开)。 My app is basically client-server with real time updating (and I know your question was just about clients, but I would use the same technologies and patterns). 我的应用程序基本上是客户端 - 服务器实时更新(我知道你的问题只是关于客户端,但我会使用相同的技术和模式)。 O Ø

n the client side I utilize MVP with StructureMap for IoC and some very simple event aggregation strategies for cross-class communications. 在客户端,我使用带有StructureMap的MVP进行IoC,并使用一些非常简单的事件聚合策略进行跨类通信。 I code to interfaces for just about everything. 我为几乎所有东西编写接口代码。 The only other thing I did was borrow from the CAB the idea of a flexible "Workspace" for dynamically displaying views. 我做的唯一另一件事就是从CAB借用一个灵活的“工作区”来动态显示视图。 I wrote my own Workspace interface though and implemented my own DeckWorkspace and TableWorkspace for use in my app (these were really simple things to write). 我编写了自己的Workspace接口并实现了自己的DeckWorkspace和TableWorkspace以便在我的应用程序中使用(这些都是非常简单的事情)。

A lot of my decisions in this most recent application were the result of experience and pain I felt using other frameworks and tools. 我在这个最近的应用程序中做出的许多决定都是我感觉使用其他框架和工具的经验和痛苦的结果。 I made different decisions this time around. 这次我做出了不同的决定。 Maybe the only way to really understand how to architect an application is to feel the pain of doing it wrong beforehand. 也许真正理解如何构建应用程序的唯一方法是事先感受到做错的痛苦。

I would say yes, it could easily be structured towards smaller applications. 我会说是的,它可以很容易地构建为更小的应用程序。 There is a learning curve towards getting started, but honestly, it helped me understand WPF better than attempting to start from scratch. 开始时有一个学习曲线,但老实说,它帮助我理解WPF比尝试从头开始更好。 After starting a project with CompositeWPF and then starting another project without it, I found myself attempting to duplicate features of CompositeWPF on my own because I missed those features! 在使用CompositeWPF启动项目然后在没有它的情况下启动另一个项目之后,我发现自己试图复制CompositeWPF的功能,因为我错过了这些功能! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM