I have been trying to learn dependency injection but I have 2 errors when I run my code:
DependencyInjection.Message.Sms()must declare a body because it is not marked abstract, extern, or partial.- Cannot implicitly convert type
DependencyInjection.IInterfacetoDependencyInjection.MyClass. An explicit conversion exists (are you missing a cast?)
Is the code below a good design for DI?
namespace DependencyInjection
{
public interface IInterface
{
}
public abstract class Message
{
public virtual void Sms();
}
public class MyClass : Message, IInterface
{
public override void Sms()
{
Console.WriteLine("Sms gönder.");
}
}
public class ClassManager
{
private IInterface _myinterface;
public MyClass Xyz
{
get { return _myinterface; }
set { _myinterface = value; }
}
public ClassManager(IInterface myinterface)
{
_myinterface = myinterface;
}
}
}
Add
abstractkeyword (and remove virtual) to method declaration:Xyzhas return-type of typeMyClassbut ingetyou are returning_myinterfacewhich is of typeIInterface.Change to following: