简体   繁体   English

ASP.NET CORE MVC 依赖反转错误与 CRUD

[英]ASP.NET CORE MVC dependicy inversion error with CRUD

Today i started on dependicy inversion.今天我开始依赖倒置。 Before i started implenting this i had a nice CRUD system but now i got some errors.在我开始实施之前,我有一个不错的 CRUD 系统,但现在我遇到了一些错误。 It says "Cannot implicity convert type int to ContractLayer.UsersDto" On the image you can see my controller in the middle and i think its all fine there.它说“不能将类型 int 隐式转换为 ContractLayer.UsersDto” 在图像上,您可以在中间看到我的 controller,我认为那里一切正常。 The error is not that clear to me.这个错误对我来说不是那么清楚。 Maybe i forgot to pass an int with the function?也许我忘了通过 function 传递一个 int? Maybe you guys can help me out with this.也许你们可以帮我解决这个问题。

Kind regards,亲切的问候,

Sem扫描电镜

Usercollection,UserController,IUserDal用户集合、用户控制器、IUserDal

Both your errors are simply due to type mismatches - not dependency inversion.您的两个错误都只是由于类型不匹配 - 而不是依赖倒置。

The signatures of the errors are错误的签名是

public int DeleteUser(int id);
public int CreateUser(UserDto dto);

In both cases they return an int , but you try to build an UsersDto :在这两种情况下,它们都返回一个int ,但您尝试构建一个UsersDto

UsersDto = FactoryLayer.UserDalFactory.GetUserDal().DeleteUser(Id);

You either have to change to signature of the corresponding methods in your UserCollection or your IUsersDal to the correct type.您必须将UserCollectionIUsersDal中相应方法的签名更改为正确的类型。

I'd suggest you change the Delete method to public void DeleteUser(int id);我建议您将 Delete 方法更改为public void DeleteUser(int id); and the consuming method to:和消费方法:

public void Delete(int id) { ... }

When it comes to the create method I would change the signature in your IUserDal to public UsersDto Create(UsersDto dto);当涉及到 create 方法时,我会将IUserDal中的签名更改为public UsersDto Create(UsersDto dto); and then also adapt the concreate class that is implementing that interface accordingly.然后还相应地调整实现该接口的concreate class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM