开发者

How to choose a naming convention for WPF view-model and data-model classes?

I am trying to figure out a naming convention to use when naming my WPF view-model and data-model classes and would appreciate input from others who have already done this.

My problem is that the data-model and view-model class that I want to name have almost the same name.

As an example, I am working on an application that has a flow-chart editor. In my data-model I am going to have a Node class.

In my view-model I am also going to have a Node class that wraps the data-model class and adds view specific properties such as IsSelected.

What is the best way to actually differentiate these class names to avoid confusion?

Obviously they will be in different namespaces. Eg Flowchart.Node and FlowchartView.Node. So they don't really need to have different class names. That said I think that different class names would be better to help, as I mentioned, avoid confusi开发者_如何学运维on.

I had thought of naming them Node and NodeView which I suppose sounds reasonable but for some reason leaves a bad taste for me.

So this is a call out for advice on what naming conventions others are using. This is admittedly a simple problem, but then again finding good names seems to be a constant battle.


I suffix all my view models with "ViewModel", and all views with "View". Thus, you'd have NodeViewModel, NodeView, and Node (the data class). This is just a personal convention, though. I'm sure there are other equally valid one out there.


I'll keep the namespace and folders in synch as that's the expectation most .NET developers will have. Needless to say, each class in its own file. This will make it easy to find source in projects.

/Views
namespace: <Company>.(<Product>|<Technology>)[.<Feature>].Views
/ViewModels
namespace: <Company>.(<Product>|<Technology>)[.<Feature>].ViewModels
/Models
namespace: <Company>.(<Product>|<Technology>)[.<Feature>].Models

No need to add suffix to distinguish between classes. Namespace already does that.


Namespaces

  • System - contains the main system classes, a.k.a. the model.
  • System.ViewModels - contains the view-models.
  • System.Windows - contains anything used for presentation.

Simple project, I include all the namespaces.

For bigger projects, I create one project for each namespace. Avoid deep namespaces. See Framework Design Guidelines.

I do not append any suffixes because the namespace is clear enough. For instance, if I create a UserControl (a view) that displays a collection of "Alerts", then I name that user-control AlertsPanel or AlertsListBox or 'AlertsItemsControl`.

The exception is view-models. There I find it is better to append "ViewModel" because this convention avoids the natural naming conflicts that can occur when all three namespaces must be used.

As an alternative to appending "ViewModel", using VM = System.ViewModels; allows for a model and view-model class to share the same name, as in VM.Foo oFoo = new VM.Foo(new Foo());.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜