服务没有实现接口成员noobie

时间:2012-09-06 12:22:45

标签: asp.net-mvc-3 architecture interface repository

我正在重新设计一个MVC3应用程序,将所有linq从控制器中取出并进入适当的层。

我的结构是SQL - > EF - >存储库 - >服务 - >控制器。我正在使用接口。

编译时我收到此错误:

gpc.data.service.roleService未实现接口成员gpc.data.interfaces.iroleservice.HolderNamesbyRoleID(int)。

我对正确的架构完全陌生如此,如果这是非常明显的道歉,请道歉。这是一些代码:

存储库:

namespace gpc.Data.Repositories
{
public class roleRepository :gpc.Data.Interfaces.IRoleRepository
{
private gpc.Models.gpcEntities _entities = new Models.gpcEntities();


public HolderNames HolderNamesbyRoleID(int roleid)
{
return (from i in _entities.HolderNames
where i.roleid == roleid select i).FirstOrDefault();              
}
}
}

然后我有一个界面:

namespace gpc.Data.Interfaces
{
public interface IRoleRepository
{
HolderNames HolderNamesbyRoleID(int roleid);
}
}

然后我有了服务:

namespace gpc.Data.Service
{
public class roleService : gpc.Data.Interfaces.IRoleService
{
    private ModelStateDictionary _modelState;
    private gpc.Data.Interfaces.IRoleRepository _repository;

    public roleService(ModelStateDictionary modelState)
    {
        _modelState = modelState;
        _repository = new gpc.Data.Repositories.roleRepository();
    }
    public roleService(ModelStateDictionary modelState,                                    
    gpc.Data.Repositories.roleRepository repository)
    {
        _modelState = modelState;
        _repository = repository;
    }


    public HolderNames HolderNames(int roleid)
    {
        return _repository.HolderNamesbyRoleID(roleid);
    }
}
}

然后我有另一个界面:

namespace gpc.Data.Interfaces
{
public interface IRoleService
{
    HolderNames HolderNamesbyRoleID(int roleid);
}
}

我在这个结构中创建了一个非常简单的ienumerable,我能够通过控制器获取数据,正如我所期望的那样。我想,因为这个有点复杂,所以选择一切并把它扔到一个视图我一定错过了一些东西。我不知道它是否有所作为,但" holdernames"是一个SQL视图而不是表。

任何帮助非常感谢

2 个答案:

答案 0 :(得分:0)

它基本上就是编译器错误所显示的内容。您的IRoleService接口定义了一个名为HolderNamesbyRoleID的方法,但在您的实现中,您只有一个名为HolderNames的方法。

我认为这只是你的错误。

答案 1 :(得分:0)

接口仅包含签名。您必须在已实现接口的类中编写实际实现。在您的情况下,您已在 IRoleRepository 中定义方法 HolderNamesbyRoleID ,但您尚未在 roleService 类中实现此方法。您必须在 roleService 类中实施 HolderNamesbyRoleID

您的 roleService 类代码如下所示。

namespace gpc.Data.Service
{
public class roleService : gpc.Data.Interfaces.IRoleService
{
    private ModelStateDictionary _modelState;
    private gpc.Data.Interfaces.IRoleRepository _repository;

    public roleService(ModelStateDictionary modelState)
    {
        _modelState = modelState;
        _repository = new gpc.Data.Repositories.roleRepository();
    }
    public roleService(ModelStateDictionary modelState,                                    
    gpc.Data.Repositories.roleRepository repository)
    {
        _modelState = modelState;
        _repository = repository;
    }


    public HolderNames HolderNamesbyRoleID(int roleid)
    {
        return _repository.HolderNamesbyRoleID(roleid);
    }
}
}

有关详细信息,请参阅interface