我可以在 ASP.NET MVC3 应用程序中使用我的大部分代码吗?

发布于 2024-12-07 03:58:00 字数 1212 浏览 0 评论 0原文

我编写了一个简单的 WPF 应用程序作为示例:

模型:

public class User
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
}

ViewModel:

//ViewModelBase contains base code for INotifyPropertyChanged
public class UserViewModel : ViewModelBase
{
    public UserViewModel(User user)
    {
        _user = user ?? new User();
    }

    private User _user;

    public String FirstName
    {
        get { return _user.FirstName; }
        set
        {
            if (_user.FirstName != value)
            {
                _user.FirstName = value;
                RaisePropertyChanged("FirstName", "Full Name");
            }
        }
    }

    public String LastName
    {
        get { return _user.LastName; }
        set
        {
            if (_user.LastName!= value)
            {
                _user.LastName= value;
                RaisePropertyChanged("LastName", "Full Name");
            }
        }
    }

    public String FullName
    {
        get { return String.Format("{0}, {1}", LastName, FirstName); }
    }
}

我可以在 MVC3 应用程序中重用我的代码吗?我认为 INotifyPropertyChanged 的​​东西不会工作,但我不能仍然以某种方式重用 ViewModel 吗?

I wrote a simple WPF Application as an example:

Model:

public class User
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
}

ViewModel:

//ViewModelBase contains base code for INotifyPropertyChanged
public class UserViewModel : ViewModelBase
{
    public UserViewModel(User user)
    {
        _user = user ?? new User();
    }

    private User _user;

    public String FirstName
    {
        get { return _user.FirstName; }
        set
        {
            if (_user.FirstName != value)
            {
                _user.FirstName = value;
                RaisePropertyChanged("FirstName", "Full Name");
            }
        }
    }

    public String LastName
    {
        get { return _user.LastName; }
        set
        {
            if (_user.LastName!= value)
            {
                _user.LastName= value;
                RaisePropertyChanged("LastName", "Full Name");
            }
        }
    }

    public String FullName
    {
        get { return String.Format("{0}, {1}", LastName, FirstName); }
    }
}

Can I reuse my code in an MVC3 app? I figure the INotifyPropertyChanged stuff won't work, but can't I still reuse the ViewModel somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贵在坚持 2024-12-14 03:58:00

不,不是真的。 ViewModel 和 Controller 由完全不同的设计模式控制。

MVVM 和 MVC 的亮点在于它们有助于从代码隐藏中分离出业务逻辑。您很可能可以共享您的支持代码(从虚拟机和控制器调用的代码,但不涉及任何以平台为中心的设计)和模型(取决于它们的编码方式)。

我目前正在开发一个结合了 MVC 网站和 WPF 应用程序的应用程序,并且我确实在它们之间共享模型,以及从虚拟机和控制器中提取的尽可能多的代码。但两者之间存在太多差异,无法将虚拟机用作控制器,反之亦然。

No, not really. ViewModels and Controllers are governed by completely different design patterns.

Where MVVM and MVC shine is that they help break out business logic from your codebehind. You quite possibly can share your support code (code that you call from your VMs and Controllers but that isn't touched by any platform-centric designs) and models (depending on how they are coded).

I'm currently working on an app that combines an MVC website and a WPF application, and I do share models between them, and as much code as I can squeeze from my VMs and Controllers. But there is too much different between the two to use your VMs as Controllers and vice versa.

迷途知返 2024-12-14 03:58:00

您的 'firstname' 和 'lastname' 方法将直接移入模型中,因为它们只不过是稍微更花哨的 getter 和 setter。

你是 FullName 方法,我很想将其放入控制器中,因为它正在处理格式化,但它背后确实有一个基本且可测试的逻辑。

正如您所说,我不确定您是否可以使用 NotifyProperty gubbins,但一般方法应该可以愉快地移植。

Your 'firstname' and 'lastname' method would just move into the model, as they aren't too much more than slightly more fancy getters and setters.

You're FullName method I'd be tempted to put in the controller as it's dealing with formatting but it does kind of have a basic and testable logic behind it.

As you say, I'm not sure you could use the NotifyProperty gubbins, but the methods in general should port over happily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文