接口在 Prism 实现中的重要性
Interface importance in Prism implementation
是否有必要在模块级别实现接口并在 prism 应用程序中使用 viewmodel?
我没有看到任何可用于多个 class 的界面。
不,prism 中的视图模型不需要实现任何接口。甚至 INotifyPropertyChanged
,如果您没有任何从逻辑端而不是从视图更改的数据。
在旧版本中,views 必须实现 IView
才能使用 ViewModelLocator
,但它已不存在了。
模块定义需要实现IModule
.
评论后编辑:
如果您希望能够在生产或测试中用另一个 class 替换它,您想要为单个 class 创建一个接口。
话虽如此,您通常不会为 class 创建接口,但反过来也是如此。接口排在第一位并指定消费者想要对实现执行的操作,然后一个或多个 classes 分别为一个或多个接口提供实现。
示例:
鉴于 class
internal class InventoryManager
{
public IEnumerable<string> ListItems() { ... }
public void AddItem( string item ) { ... }
public void RemoveItem( string item ) { ... }
}
你没有创建这个接口:
public interface IInventoryManager
{
IEnumerable<string> ListItems();
void AddItem( string item );
void RemoveItem( string item );
}
而是这两个:
public interface IItemList
{
IEnumerable<string> ListItems();
}
public interface IItemStorage
{
void AddItem( string item );
void RemoveItem( string item );
}
因为您界面的消费者可能想要查看库存中的物品或更改它。并且您希望选择以不同于可写清单的方式实施只读清单。
是否有必要在模块级别实现接口并在 prism 应用程序中使用 viewmodel?
我没有看到任何可用于多个 class 的界面。
不,prism 中的视图模型不需要实现任何接口。甚至 INotifyPropertyChanged
,如果您没有任何从逻辑端而不是从视图更改的数据。
在旧版本中,views 必须实现 IView
才能使用 ViewModelLocator
,但它已不存在了。
模块定义需要实现IModule
.
评论后编辑:
如果您希望能够在生产或测试中用另一个 class 替换它,您想要为单个 class 创建一个接口。
话虽如此,您通常不会为 class 创建接口,但反过来也是如此。接口排在第一位并指定消费者想要对实现执行的操作,然后一个或多个 classes 分别为一个或多个接口提供实现。
示例:
鉴于 class
internal class InventoryManager
{
public IEnumerable<string> ListItems() { ... }
public void AddItem( string item ) { ... }
public void RemoveItem( string item ) { ... }
}
你没有创建这个接口:
public interface IInventoryManager
{
IEnumerable<string> ListItems();
void AddItem( string item );
void RemoveItem( string item );
}
而是这两个:
public interface IItemList
{
IEnumerable<string> ListItems();
}
public interface IItemStorage
{
void AddItem( string item );
void RemoveItem( string item );
}
因为您界面的消费者可能想要查看库存中的物品或更改它。并且您希望选择以不同于可写清单的方式实施只读清单。