Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding to Type in C#

Tags:

c#

I tried to use C# DI method to implement something. following is my code snippet.

public interface IMessageService
{
    void Send(string uid, string password);
}

public class MessageService : IMessageService
{
    public void Send(string uid, string password)
    {
    }
}

public class EmailService : IMessageService
{
    public void Send(string uid, string password)
    { 
    }
}

and code that creates a ServiceLocator:

public static class ServiceLocator
{

    public static object GetService(Type requestedType)
    {
        if (requestedType is IMessageService)
        {
            return new EmailService();
        }
        else
        {
            return null;
        }
    }
}

now, I create a test code with

public class AuthenticationService
{
    private IMessageService msgService;
    public AuthenticationService()
    {
        this.msgService = ServiceLocator
          .GetService(typeof(IMessageService)) as IMessageService;
    }
}

but, it looks like, I always get null returned by GetService() function. Instead I expect to get EmailService object via GetService() function, so how to do it correctly?

like image 523
Terence Liu Avatar asked Jun 10 '18 06:06

Terence Liu


People also ask

When %s is used in C?

Note: The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to print and read strings directly.

What are the 4 data types in C?

Main types. The C language provides the four basic arithmetic type specifiers char, int, float and double, and the modifiers signed, unsigned, short, and long.

For what %D is used in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What are the 5 data types in C?

Most of the time, for small programs, we use the basic fundamental data types in C – int, char, float, and double. For more complex and huge amounts of data, we use derived types – array, structure, union, and pointer.


2 Answers

What you are passing in is an instance of Type.

So this condition requestedType is IMessageService is never true.

What you need to do is

public static object GetService(Type requestedType)
{
    if (requestedType == typeof(IMessageService))
    {
        return new EmailService();
    }
    else
    {
        return null;
    }
}

As a side note, this is quite a bad pattern - your so-called service locator has concrete knowledge of concrete types. You're better off using reflection or some traditional registration pattern for IoC to make this generic.

like image 71
zaitsman Avatar answered Oct 13 '22 08:10

zaitsman


I tried to use C# DI method to implement something. following is my code snippet

There is no such pattern called "C# DI method". I presume that our task here is to use a ServiceLocator pattern for DI. Don't do that!

The ServiceLocator is arguably an anti-pattern and leads to maintenance nightmare because class dependencies are hidden. In most real-world scenarios we should avoid using it.

With help of some DI framework such as SimpleInjector (it could be any other well-known DI framework though) you could achieve the same result. However this time the code will be more maintainable and a lot easier to test. For that we could create a Mock<IMessageService> and pass its object to a constructor of EmailService.

But let's get back to the subject and have a look into how we could use Simpleinjector here:

public class AuthenticationService
{
    private readonly IMessageService _msgService;

    public AuthenticationService(IMessageService msgService)
    {
        this._msgService = msgService;
    }
}

In order to use that somewhere in code we need to register this dependency. A minimal code example would be:

var container = new SimpleInjector.Container();
container.Register<IMessageService, EmailService>();
container.Verify();

And that's all it requires!

P.S. This is not an ad of this particular DI framework. Feel free to use any other framework, I've used it in this example because I'm more familiar with it

like image 4
Fabjan Avatar answered Oct 13 '22 08:10

Fabjan