C#Asp.Net MVC继承接口上的抽象类方法
我有一个抽象类、一个接口和一个继承抽象类和接口的类 我想从继承类的抽象类中公开该方法。我如何做到这一点C#Asp.Net MVC继承接口上的抽象类方法,c#,asp.net-mvc,inheritance,abstract-class,autofac,C#,Asp.net Mvc,Inheritance,Abstract Class,Autofac,我有一个抽象类、一个接口和一个继承抽象类和接口的类 我想从继承类的抽象类中公开该方法。我如何做到这一点 public abstract class CustomerServiceBase { public string GetName() { return "John Doe"; } } public interface ICustomerService { void DoOtherStuff(); } public class Custome
public abstract class CustomerServiceBase
{
public string GetName()
{
return "John Doe";
}
}
public interface ICustomerService
{
void DoOtherStuff();
}
public class CustomerService : CustomerServiceBase, ICustomerService
{
public void DoOtherStuff()
{
//Code removed for brevity
}
}
public class Start
{
public void Go()
{
ICustomerService _customerService = new CustomerService();
_customerService.DoOtherStuff();
var name = _customerService.GetName();//This won't compile
}
}
更新
抱歉,我添加了准确显示此场景的附加代码,即使用Autofac将依赖项注入Asp.Net MVC控制器,然后使用服务类执行任务
public abstract class CustomerServiceBase
{
public string GetName()
{
return "John Doe";
}
}
public interface ICustomerService
{
void DoOtherStuff();
}
public class CustomerService : CustomerServiceBase, ICustomerService
{
public void DoOtherStuff()
{
//Code removed for brevity
}
}
public static class AutofacConfig
{
public static IContainer Register(IAppBuilder app)
{
var builder = new ContainerBuilder();
ConfigureServices(builder);
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
SetBothDependencyResolvers(container);
return container;
}
public static void ConfigureServices(ContainerBuilder builder)
{
builder.RegisterType<CustomerService>().AsImplementedInterfaces().InstancePerLifetimeScope();
}
}
public class CustomerController : Controller
{
protected readonly ICustomerService _customerService;
public CustomerController(ICustomerService customerService)
{
this._customerService = customerService;
}
public ActionResult Index()
{
_customerService.DoOtherStuff();
var name = _customerService.GetName();
return View();
}
}
在
Go
方法中使用CustomerService
而不是icCustomerService
CustomerService _customerService = new CustomerService();
_customerService.DoOtherStuff();
var name = _customerService.GetName(); // Compiles now
只需更改变量的类型。您仍然可以调用ICCustomerService的成员,因为类实现了它
public void Go()
{
CustomerService _customerService = new CustomerService();
_customerService.DoOtherStuff();
var name = _customerService.GetName();
}
我想从继承类的抽象类中公开该方法。我如何做到这一点
public abstract class CustomerServiceBase
{
public string GetName()
{
return "John Doe";
}
}
public interface ICustomerService
{
void DoOtherStuff();
}
public class CustomerService : CustomerServiceBase, ICustomerService
{
public void DoOtherStuff()
{
//Code removed for brevity
}
}
public class Start
{
public void Go()
{
ICustomerService _customerService = new CustomerService();
_customerService.DoOtherStuff();
var name = _customerService.GetName();//This won't compile
}
}
这已经发生了。出现编译器错误的原因是,您的代码将\u customerService
指定为类型(接口)ICCustomerService
,并且此接口没有方法DoOtherStuff()
的定义。如果要使用类型CustomerService
,则可以调用在该类型继承自的抽象类(或其实现的接口)上定义的方法
至于问题的标题: 在接口上继承抽象类方法
这是不可能的(用c#)。接口可以扩展其他接口并由类实现,但接口不能从其他接口“继承”(尽管公认的术语是“扩展”接口)。您的问题源于
\u customerService
变量的类型为ICCustomerService
。您的接口对抽象类一无所知。如果希望该类型的内容具有特定的方法,则需要将其添加到接口:
public interface ICustomerService
{
void DoOtherStuff();
string GetName();
}
然后我将使抽象类实现接口,而不是CustomerService
类。您还需要在抽象类中添加一个abstract
方法,这样,从您的abstractCustomerServiceBase
继承的任何类都将被迫添加一个DoOtherStuff()
实现(您的抽象类不必添加自己的实现):
而客户服务
应该是:
public class CustomerService : CustomerServiceBase
一旦这样做,您的Index()
方法就会很好:
public ActionResult Index()
{
//DoOtherStuff() and GetName() are both part of the interface now
_customerService.DoOtherStuff();
var name = _customerService.GetName();
return View();
}
当您执行
icCustomerService\u customerService
您正在启动ICCustomerService合同,但其合同中没有任何名为GetName()
因此,您要么在合同中获得它,要么如上所述创建CustomerService
本身
此外,抽象类的整个概念是交互,因此基本上,当您将CustomerService
定义为非抽象类时,使用它是很好的
CustomerService CustomerService=新的CustomerService
但与其说是CustomerService
,不如说是PhoneService
或TextService
,因为这些都是客户在现实生活中直接互动的东西。并使用CustomerService
本身,而不是CustomerService Base
,因为人们将使用客户服务,但不是直接使用,而是以上述电话、文字或其他服务的形式使用
public abstract class CustomerService {} <-- Think in real life this doesnt have interaction
public class PhoneService() {} <-- This interacts directly
公共抽象类CustomerService{}GetName()
是CustomerServiceBase
的成员,但您的\u CustomerService
是接口的实例。您的接口对CustomerServiceBase
类一无所知,因此编译器会抱怨。您为什么不想将该方法移动到接口?一个icCustomerService
如何知道CustomerServiceBase
中的任何内容?它们根本不相关。//这不会编译
抱歉,请查看我关于对Autofac依赖性的更新,我应该先把它放进去!如果不打算使用接口,定义接口有什么意义?它可以在其他地方使用,而不是在Go方法中。现在GetName
编译,但不是DoOtherStuff
,因为CustomerServiceBase
没有实现ICCustomerService
@Igor true,我想我应该解决创建依赖关系的问题(new…()
)是个坏主意。你说的没错,未来没有太多的事情要做question@Goober根据您对问题的编辑,我做了一个小的编辑。现在我们应该同步:)我使用的是Autofac依赖项注入,因此在实例化我收集的服务时,我必须使用接口。@Goober如果您使用的是Autofac,那么为什么您要说new…
?@KennethK。我试图提供一个简单的例子,这显然是错误的。抱歉,请查看我的更新。“//但是更好的命名应该是PhoneService或TextService,而不是CustomerService”-谢谢,它实际上是Stripes DotNet Api的包装器,但我提供了一个简单的用例,特别是在示例中。。。
public ActionResult Index()
{
//DoOtherStuff() and GetName() are both part of the interface now
_customerService.DoOtherStuff();
var name = _customerService.GetName();
return View();
}
public abstract class CustomerService {} <-- Think in real life this doesnt have interaction
public class PhoneService() {} <-- This interacts directly