Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Returning a generic interface from a factory

I've implemented a Vehicle service that is responsible for servicing vehicles such as cars and trucks:

public interface IVehicleService
{
    void ServiceVehicle(Vehicle vehicle);   
}

public class CarService : IVehicleService
{
    void ServiceVehicle(Vehicle vehicle)
    {
        if(!(vehicle is Car))
            throw new Exception("This service only services cars")

       //logic to service the car goes here
    }
}

I also have a vehicle service factory that is responsible for creating a vehicle service according to the type of vehicle passed in to the factory method:

public class VehicleServiceFactory 
{
    public IVehicleService GetVehicleService(Vehicle vehicle)
    {
        if(vehicle is Car)
        {
            return new CarService();
        }

        if(vehicle is Truck)
        {
            return new TruckService();
        }

        throw new NotSupportedException("Vehicle not supported");
    }

}

The main issue I have is specifically with CarService.ServiceVehicle method. It's accepting a Vehicle when ideally it should accept a Car instead, as it knows that it will only service cars. So I decided to update this implementation to use generics instead:

public interface IVehicleService<T> where T : Vehicle
{
    void ServiceVehicle(T vehicle); 
}

public class CarService : IVehicleService<Car>
{
    void ServiceVehicle(Car vehicle)
    {
        //this is better as we no longer need to check if vehicle is a car

        //logic to service the car goes here 
    }
}

The issue I'm having is how to update VehicleServiceFactory to return the generic version of the vehicle service. I've tried the following but it results in a compilation error as it's unable to cast CarService to the generic return type IVehicleService:

public class VehicleServiceFactory 
{
    public IVehicleService<T> GetVehicleService<T>(T vehicle) where T : Vehicle
    {
        if(vehicle is Car)
        {
            return new CarService();
        }

        if(vehicle is Truck)
        {
            return new TruckService();
        }

        throw new NotSupportedException("Vehicle not supported");
    }

}

Any suggestions would be appreciated.

like image 568
user8851548 Avatar asked Oct 29 '17 12:10

user8851548


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Simply cast the service to the interface:

return new CarService() as IVehicleService<T>;

You know T is car but the compiler doesn't, its not smart enough to follow the method logic nor is it meant to be; as far as the compiler knows, T can be anything as long as its a Vehicle. You need to tell the compiler, "Hey, I know what I'm doing, T and Car are in fact the same type."

like image 122
InBetween Avatar answered Sep 23 '22 19:09

InBetween