Created
April 25, 2017 14:49
-
-
Save franssu/e274abc669f2138bee90b2118bca6c56 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; | |
using System.Linq; | |
namespace ConsoleApplication2 | |
{ | |
class SomeStringObject | |
{ | |
public string S { get; private set; } | |
public SomeStringObject(string s) { this.S = s; } | |
} | |
class AnotherStringObject | |
{ | |
public string S { get; private set; } | |
public AnotherStringObject(string s) { this.S = s; } | |
} | |
class AnIntObject | |
{ | |
public int I { get; private set; } | |
public AnIntObject(int i) { this.I = i; } | |
} | |
enum CreatedObjectType { SomeStringObject, AnotherStringObject, AnIntObject }; | |
class Program | |
{ | |
static SomeStringObject GetOrCreateSomeStringObject(string s, List<SomeStringObject> someStringObjects) | |
{ | |
var existing = someStringObjects.FirstOrDefault(x => x.S == s); | |
if (existing != null) return existing; | |
var newObject = new SomeStringObject(s); | |
someStringObjects.Add(newObject); | |
return newObject; | |
} | |
static AnotherStringObject GetOrCreateAnotherStringObject(string s, List<AnotherStringObject> anotherStringObjects) | |
{ | |
var existing = anotherStringObjects.FirstOrDefault(x => x.S == s); | |
if (existing != null) return existing; | |
var newObject = new AnotherStringObject(s); | |
anotherStringObjects.Add(newObject); | |
return newObject; | |
} | |
static AnIntObject GetOrCreateAnIntObject(int i, List<AnIntObject> anIntObjects) | |
{ | |
var existing = anIntObjects.FirstOrDefault(x => x.I == i); | |
if (existing != null) return existing; | |
var newObject = new AnIntObject(i); | |
anIntObjects.Add(newObject); | |
return newObject; | |
} | |
static object GetOrCreate(CreatedObjectType t, object parameter, List<SomeStringObject> someStringObjects, List<AnotherStringObject> anotherStringObjects, List<AnIntObject> anIntObjects) | |
{ | |
switch (t) | |
{ | |
case CreatedObjectType.SomeStringObject: | |
return GetOrCreateSomeStringObject((string)parameter, someStringObjects); | |
case CreatedObjectType.AnotherStringObject: | |
return GetOrCreateAnotherStringObject((string)parameter, anotherStringObjects); | |
case CreatedObjectType.AnIntObject: | |
return GetOrCreateAnIntObject((int)parameter, anIntObjects); | |
default: | |
throw new Exception(); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
List<SomeStringObject> someStringObjects = new List<SomeStringObject>(); | |
List<AnotherStringObject> anotherStringObjects = new List<AnotherStringObject>(); | |
List<AnIntObject> anIntObjects = new List<AnIntObject>(); | |
var paramsList = new List<Tuple<CreatedObjectType, object>> | |
{ | |
Tuple.Create(CreatedObjectType.SomeStringObject, (object)"coucou"), | |
Tuple.Create(CreatedObjectType.AnotherStringObject, (object)"lol"), | |
Tuple.Create(CreatedObjectType.AnotherStringObject, (object)"lol"), | |
Tuple.Create(CreatedObjectType.AnotherStringObject, (object)"lol"), | |
Tuple.Create(CreatedObjectType.AnIntObject, (object)3), | |
Tuple.Create(CreatedObjectType.AnIntObject, (object)3), | |
Tuple.Create(CreatedObjectType.AnIntObject, (object)4) | |
}; | |
var items = paramsList.Select(x => GetOrCreate(x.Item1, x.Item2, someStringObjects, anotherStringObjects, anIntObjects)).Distinct().ToList(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment