Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有函数的独立视图模型类与属性_C#_Oop - Fatal编程技术网

C# 带有函数的独立视图模型类与属性

C# 带有函数的独立视图模型类与属性,c#,oop,C#,Oop,我想知道是否应该为视图模型创建一个类,或者是否应该将属性和方法保存在一个类中。这方面有最佳实践吗 下面是显示具有单独视图模型类的示例: public class vmCustomer { public string FullName { get; set; } public string Address { get; set; } ... } public class Customer { public vmCustomer GetCustomer(decim

我想知道是否应该为视图模型创建一个类,或者是否应该将属性和方法保存在一个类中。这方面有最佳实践吗

下面是显示具有单独视图模型类的示例:

public class vmCustomer
{
    public string FullName { get; set; }
    public string Address { get; set; }
    ...
}

public class Customer
{
     public vmCustomer GetCustomer(decimal id)
     {
           ...
           return customer;
     }
}
以下示例显示在单个类中包含我的属性和方法:

public class Customer
{
     public string FullName { get; set; }
     public string Address { get; set; }

     public Customer GetCustomer(decimal id)
     {
           ...
           return customer;
     }
}
在OOP开发中,有没有更好的方法?坚实的原则会因为单一责任规则而说是一个独立的视图模型吗?

视图模型是视图和模型之间的“桥梁”。真实数据应该在模型中,在视图中,模型数据应该是格式的,这样可以绑定到视图

此外,按id选择customer应该在服务类中,由viewmodel调用

public class VmCustomerDetail
{
    private ICustomerService customerService;
    public Customer CustomerDetail {get; set;}

    public async void Refresh(int id){
        CustomerDetail = customerService.GetById(id);
    }
}

public class Customer
{
     public string FullName { get; set; }
     public string Address { get; set; }    
}

public interface ICustomerService{
    Customer GetById(int id);
}

public class CustomerService : ICustomerService{
    Customer GetById(int id){
       //...
    }
}

GetCustomer
做什么,它从数据库中读取数据吗?(无论如何,我猜应该是
静态的
)如果GetCustomer从somwhere(db、file、webservice等等)加载一个客户,我不会在customer类中插入该方法。让“responsibility”将customer加载到另一个类中。我是否应该将GetById、UpdateCustomer、InsertCustomer都放在一个服务类中?如果没有,我应该如何管理它?所有要放入ViewModel的数据都应该通过服务类加载。。。对于“更深层次”的内部功能来说,这是不必要的。如果没有接口,您可以做到这一点吗?使用该界面的优点是什么?谢谢libik@JHunt-用于设计时间数据。您可以编写DesignCustomerService和实现该接口的CustomerService类。然后,您可以配置在设计时间模式下选择DesignCustomerService。这样,即使不运行程序,也可以在设计器中看到真实的数据行为。通常,您不会在Design services中从数据库或服务器中“选择”,您只需手动创建一些数据。但这就足够了,便于定位等。