Skip to content

Instantly share code, notes, and snippets.

@rhwy
Created January 20, 2020 16:25
Show Gist options
  • Save rhwy/7e26c213c9cd73815bb6f283209e2391 to your computer and use it in GitHub Desktop.
Save rhwy/7e26c213c9cd73815bb6f283209e2391 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace DI2
{
using MonApp;
using MonApp.Adapters;
class Program
{
static void Main(string[] args)
{
Test();
var userAdapter = new UserFromDb();
var students = new Students(userAdapter);
var count = students.CountStudents();
Console.WriteLine($" {count} students");
var first = students.First();
Console.WriteLine($" le premier est {first}");
}
static void Test()
{
var fakeUsers = new FakeUser();
var students = new Students(fakeUsers);
var count = students.CountStudents();
var first = students.First();
Assert(count,5);
Assert(first,"a");
}
static void Assert<T>(T value, T expected)
{
if(value.Equals(expected)) Console.WriteLine("ok") ;
else {
Console.WriteLine("fail!");
}
}
}
public class FakeUser : IUser
{
public IEnumerable<string> AllUsers() => new[]{"a","b","c","d","e"};
}
}
namespace MonApp
{
using System.Linq;
using System.Collections.Generic;
public class Students
{
private IUser users;
public Students(IUser users)
{
this.users = users;
}
public int CountStudents()
{
var students = users.AllUsers();
return students.Count();
}
public string First()
{
var students = users.AllUsers();
var first = students.First();
return first;
}
}
public interface IUser
{
IEnumerable<string> AllUsers ();
}
}
namespace MonApp.Adapters
{
using MonApp;
using MaLibExterne;
public class UserFromDb : IUser
{
private UserDB dB;
public UserFromDb()
{
dB = new UserDB();
}
public IEnumerable<string> AllUsers ()
=> dB.AllUsers();
}
}
namespace MaLibExterne
{
public class UserDB
{
public IEnumerable<string> AllUsers()
=> new string[]{ "Jimmy","John","Paul"};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment