Share via

Convert Interface into C#

Ben Dover 1 Reputation point
2022-05-08T20:47:52.8+00:00

Hello I want to convert this Code into C#, what is important and what should i pay attention at

package com.example.testlernen.common;

import com.example.testlernen.model.Employee;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import java.util.ArrayList;

@mutia keyza ("employee")
public interface IEmployee {

@Path("{name}")  
@GET  
@Consumes(MediaType.TEXT_PLAIN)  
@Produces(MediaType.APPLICATION_JSON)  
Employee getEmployee (@PathParam("name") String name);  

@Path("add")  
@POST  
@Consumes(MediaType.APPLICATION_JSON)  
@Produces(MediaType.APPLICATION_JSON)  
Response addEmplyoee(Employee employee);  

@Path("getAll")  
@GET  
@Produces(MediaType.APPLICATION_JSON)  
ArrayList<Employee> getAllEmployees();  

}

-------
package com.example.testlernen;

import com.example.testlernen.common.IEmployee;
import com.example.testlernen.data.EmployeeStorage;
import com.example.testlernen.model.Employee;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;

import java.time.LocalDate;
import java.util.ArrayList;

public class EmployeeRessource implements IEmployee {

protected Response.ResponseBuilder responseBuilder;  
EmployeeStorage storage = EmployeeStorage.getInstance();  


@Override  
public Employee getEmployee(String name) {  
    ArrayList<Employee> employees = storage.getEmployees();  
    for (Employee e : employees) {  
        if (name.equals(e.getFirstname())) return e;  
    }  
    throw new WebApplicationException(Response.Status.NOT_FOUND);  
}  

@Override  
public Response addEmplyoee(Employee employee) {  
    storage.addEmployee(employee);  
    return getOkResponse(employee);  
}  

@Override  
public ArrayList<Employee> getAllEmployees() {  
    return storage.getEmployees();  
}  

public Response getOkResponse(Object o){  
    responseBuilder = Response.ok(o);  
    return responseBuilder.build();  
}  

public Response employeeNotfound (String name){  
    responseBuilder= Response.status(200, "")  
            .entity("Employee "+name+" not found");  
    return responseBuilder.build();  
}  

}

Developer technologies | .NET | Other
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2022-05-09T14:38:04.607+00:00

    In C#, interfaces are the same, override is a keyword rather than an attribute. Also attributes are denoted with surrounding []’s.

    You biggest issue in converting the code is not the syntax, but replacement of the Jakarta framework and ORM.

    You would probably pick Entity Framework for the ORM, but it uses simple classes with attributes and a dbcontext.

    https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli

    You would probably replace Jakarta with asp.net core webapi.

    https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-6.0&tabs=visual-studio

    This example is probably closest to your sample code

    https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.