Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an abstract class work with StructureMap like an interface does?

I am a big fan of StructureMap and use it in just about everything I do. I have only ever used it with interfaces though. I was wondering if anyone had any experience using with abstract classes? or...does it not support that type of wiring? If you got this to work can you post an example?

Thanks!

like image 707
Andrew Siemer Avatar asked Sep 30 '09 20:09

Andrew Siemer


People also ask

Can abstract class be used as interface?

An abstract class is used if you want to provide a common, implemented functionality among all the implementations of the component. Abstract classes will allow you to partially implement your class, whereas interfaces would have no implementation for any members whatsoever.

Does an abstract class need an interface?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

Can we use abstract class instead of interface in dependency injection?

As always, the answer is: "It depends." You can't do these approaches with interfaces as interfaces only specify the contract and don't have implementation. If the contract is all you need, then go with an interface. If you need code to be shared by the subclasses, then go with an abstract class.

Which is better to use abstract class or interface?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes. If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

Can we replace interface with abstract class?

We can run an abstract class if it has main() method but we can't run an interface because they can't have main method implementation. Interfaces are used to define contract for the subclasses whereas abstract class also define contract but it can provide other methods implementations for subclasses to use.

What is diff between interface and abstract class?

Abstract Class Vs. Interface: Explore the Difference between Abstract Class and Interface in Java. The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.


1 Answers

Yes, abstract classes work exactly the same way as interfaces.

If WorkerBase is an abstract class, and RealWorker is an implementation, then:

var container = new Container(x => x.For<WorkerBase>().Use<RealWorker>());
var worker = container.GetInstance<WorkerBase>();
like image 59
Joshua Flanagan Avatar answered Sep 30 '22 19:09

Joshua Flanagan