如何在项目之间使用模型类型?
How to use model types between projects?
我正在使用服务层 lattern,我的解决方案包含三个项目:
- 核心
- 服务
- UI
在我的控制器中在 UI-project 中,我有以下代码:
public ActionResult AddSpotifyAlbums(List<SpotifyAlbums> albums)
{
_profileService.AddSpotifyAlbums(albums);
return Json(new { data = albums });
}
List<SpotifyAlbums>
中的 SpotifyAlbums 是我的 UI 项目中的模型。它看起来像这样:
public class SpotifyAlbums
{
public string URI { get; set; }
public int profileId { get; set; }
}
如您所见,我在此处使用服务 _profileService
来调用处理相册保存的方法。
但是相册的类型是SpotifyAlbums
。相册模型类型为SpotifyAlbums
时我的服务怎么办?
我试图在我的服务项目中创建一个相同的模型,但它不起作用。我收到以下错误:
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from
'System.Collections.Generic.List'
to
'soundyladder.Service.Models.SpotifyAlbums'
您不能随意将不同类型的对象分配给彼此。即使你给他们起同样的名字,他们也是独立的实体。
假设服务和 UI 项目引用您的核心项目,您可以在核心项目中定义 SpotifyAlbums
class 并在其他层中引用 class .
Bryan,你需要决定使用哪个模块 "owns" SpotifyAlbum
。决策需要考虑数据封装、数据访问和数据操作方面。重要的是建立适当的服务引用并让所有模块使用 SpotifyAlbum
实例从模块 "owning" 中获取它 SpotifyAlbum
。然后所有其他模块都会有代理 类 从对 "owning" 模块的引用构建。
我使用相同的设计模式(基于资源的可信子系统)。我喜欢使用包含公共代码和共享模型的 .dll。然后所有项目都为模型定义引用该 .dll。
我正在使用服务层 lattern,我的解决方案包含三个项目:
- 核心
- 服务
- UI
在我的控制器中在 UI-project 中,我有以下代码:
public ActionResult AddSpotifyAlbums(List<SpotifyAlbums> albums)
{
_profileService.AddSpotifyAlbums(albums);
return Json(new { data = albums });
}
List<SpotifyAlbums>
中的 SpotifyAlbums 是我的 UI 项目中的模型。它看起来像这样:
public class SpotifyAlbums
{
public string URI { get; set; }
public int profileId { get; set; }
}
如您所见,我在此处使用服务 _profileService
来调用处理相册保存的方法。
但是相册的类型是SpotifyAlbums
。相册模型类型为SpotifyAlbums
时我的服务怎么办?
我试图在我的服务项目中创建一个相同的模型,但它不起作用。我收到以下错误:
Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List' to 'soundyladder.Service.Models.SpotifyAlbums'
您不能随意将不同类型的对象分配给彼此。即使你给他们起同样的名字,他们也是独立的实体。
假设服务和 UI 项目引用您的核心项目,您可以在核心项目中定义 SpotifyAlbums
class 并在其他层中引用 class .
Bryan,你需要决定使用哪个模块 "owns" SpotifyAlbum
。决策需要考虑数据封装、数据访问和数据操作方面。重要的是建立适当的服务引用并让所有模块使用 SpotifyAlbum
实例从模块 "owning" 中获取它 SpotifyAlbum
。然后所有其他模块都会有代理 类 从对 "owning" 模块的引用构建。
我使用相同的设计模式(基于资源的可信子系统)。我喜欢使用包含公共代码和共享模型的 .dll。然后所有项目都为模型定义引用该 .dll。