Created
January 20, 2020 16:25
-
-
Save rhwy/7e26c213c9cd73815bb6f283209e2391 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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