0
\$\begingroup\$

I used an opportunity to learn about Dependency Injection and DDD, therefore, althought any feedback is welcome, I am interested in feedback about those software design principles.

I have a small project where I have to display some information queried from several similar databases (= DB that have the same purpose, but are assigned to different plants).

The back-end is coding using C#/Web API 2.

I have organized it in 3 projects (plus one test project per actual project), organized as follows (Only showing the file I believe are relevant to the review).

I am submitting for review the very first step of the program, reading the different servers that exist from configuration, and serve them to the front-end.

Backend.sln
|-> Backend.API (WebApi2 project)
 |-> Controllers/ServersController.cs
 |-> App_Start/UnityConfig.cs
 |-> Settings/Servers (a string, JSON-formatted)
|-> Backend.Domain (Class Library)
 |-> Server/AServer.cs
 |-> Server/SettingsServerRepository.cs
|-> Backend.Infra (Class Library)
 |-> Server/IServer.cs
 |-> Server/IServerRepository.cs

Backend.API

Controllers/ServersController.cs

namespace Backend.API.Controllers
{
    [EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*", SupportsCredentials = true)]
    public class ServersController : ApiController
    {
        Infra.Server.IServerRepository serverRepository;
        public ServersController(Infra.Server.IServerRepository serverRepo)
        {
            serverRepository = serverRepo;
        }

        public HttpResponseMessage Get()
        {
            IEnumerable<Infra.Server.IServer> servers = serverRepository.Servers;
            return Request.CreateResponse(HttpStatusCode.OK, servers);
        }
    }
}

App_Start/UnityConfig.cs

namespace Backend.API
{
    public static class UnityConfig
    {
        public static void RegisterComponents()
        {
            var servers = Properties.Settings.Default.Servers;

            var container = new UnityContainer();
            container.RegisterType<Infra.Server.IServerRepository,
                Domain.Server.SettingsServerRepository>(new InjectionConstructor(servers));

            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        }
    }
}

Settings/Servers

[
  {
    "$type": "Backend.Domain.Server.AServer, Backend.Domain",
    "HostName": "foo",
  },
  {
    "$type": "Backend.Domain.Server.AServer, Backend.Domain",
    "HostName": "bar",
  }
]

Backend.Domain

Server/AServer.cs

namespace Backend.Domain.Server
{
    public class AServer: Infra.Server.IServer
    {
        public string HostName { get; }

        public AServer (string HostName)
        {
            this.HostName = HostName;            
        }
    }
}

Server/SettingsServerRepository.cs

namespace Backend.Domain.Server
{
    public class SettingsServerRepository : Infra.Server.IServerRepository
    {
        public IEnumerable<Infra.Server.IServer> Servers {get;}

        public SettingsServerRepository(string jsonSettings)
        {
            Servers = JsonConvert.DeserializeObject<IEnumerable<Infra.Server.IServer>>(
                jsonSettings,
                new JsonSerializerSettings
                {
                    TypeNameHandling = TypeNameHandling.Auto
                });
        }
    }
}

Backend.Infra

Server/IServer.cs

namespace Backend.Infra.Server
{
    public interface IServer
    {
        string HostName { get; }
    }
}

Server/IServerRepository.cs

namespace Backend.Infra.Server
{
    public interface IServerRepository
    {
        IEnumerable<IServer> Servers { get; }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Please post your real code, do not edit it for the sake of the question. Some variables in your question don't match. Either this is not working or you have edited it. \$\endgroup\$ – t3chb0t Jul 3 '18 at 14:57
  • 1
    \$\begingroup\$ @t3chb0t Unfortunately, I can't do that. However, the actual code is very similar to the actual one, I just changed the service provider's name. I have created a new solution with the code I am asking a review for, and found a couple variables I missed. The code is now 100% working, sorry for the hiccup. \$\endgroup\$ – Maxime Jul 3 '18 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.