Created
June 7, 2010 15:33
-
-
Save rauhryan/428808 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public static class ValueObjectRegistry | |
{ | |
static ValueObjectRegistry() | |
{ | |
_valueObjectCache.OnMissing = (name) => | |
{ | |
return new ValueObjectHolder(name); | |
}; | |
} | |
private static readonly Cache<string, ValueObjectHolder> _valueObjectCache = new Cache<string, ValueObjectHolder>(); | |
public static readonly IList<ValueObjectStrategy> _builders = new List<ValueObjectStrategy>(); | |
public static ValueObject FindDefault(string listName) | |
{ | |
return _valueObjectCache[listName].Default(); | |
} | |
public static void AddValueObjects<T>(IEnumerable<T> objects) | |
{ | |
if(objects.Count() == 0) | |
{ | |
var methods = _builders | |
.Where(x => x.Key == typeof(T)); | |
methods.Each(x => AddValueObjects(new List<ValueObject>(), x.ListName)); | |
} | |
objects.Each(AddValueObject); | |
} | |
public static void AddValueObjects<T>(IEnumerable<ValueObject> objects) | |
{ | |
AddValueObjects(objects,typeof(T).Name); | |
} | |
public static void AddValueObjects(IEnumerable<ValueObject> objects, string name) | |
{ | |
var holder = GetValueObjectHolder(name); | |
objects.Each(holder.AddValue); | |
} | |
public static void AddValueObject<T>(T obj) | |
{ | |
var methods = _builders | |
.Where(x => x.Key == typeof(T)); | |
methods.Each(x => AddValueObjects(new[] {x.Builder(obj)}, x.ListName)); | |
} | |
public static void ConfigureFor<T>(string name, Func<T,ValueObject> callback) where T : class | |
{ | |
var token = new ValueObjectStrategy | |
{ | |
Key = typeof (T), | |
ListName = name, | |
Builder = (obj) => callback(obj as T) | |
}; | |
_builders.Add(token); | |
} | |
public static void RemoveObject(string key) | |
{ | |
_valueObjectCache.Remove(key); | |
} | |
public static ValueObjectHolder GetValueObjectHolder(string name) | |
{ | |
return _valueObjectCache[name]; | |
} | |
public class ValueObjectStrategy | |
{ | |
public Type Key { get; set; } | |
public string ListName { get; set; } | |
public Func<object, ValueObject> Builder { get; set; } | |
} | |
} | |
public class ValueObjectHolder | |
{ | |
private string _key; | |
private readonly IList<ValueObject> _values = new List<ValueObject>(); | |
public ValueObjectHolder(string key) | |
{ | |
_key = key; | |
} | |
public IEnumerable<ValueObject> Values | |
{ | |
get | |
{ | |
return _values; | |
} | |
} | |
public string GetKey() | |
{ | |
return _key; | |
} | |
public ValueObject Default() | |
{ | |
return Values.Where(x => x.IsDefault).FirstOrDefault() ?? Values.FirstOrDefault(); | |
} | |
public void AddValue(ValueObject value) | |
{ | |
_values.Add(value); | |
} | |
public void RemoveValue(ValueObject value) | |
{ | |
_values.Remove(value); | |
} | |
} |
This file contains hidden or 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
public class VendorListInitializer : IStartable | |
{ | |
private readonly IVendorService _service; | |
private readonly IUrlRegistry _urlRegistry; | |
public VendorListInitializer(IVendorService service, IUrlRegistry urlRegistry) | |
{ | |
_service = service; | |
_urlRegistry = urlRegistry; | |
} | |
public int Order | |
{ | |
get { return 0; } | |
} | |
public void Start() | |
{ | |
ValueObjectRegistry.ConfigureFor<Vendor>(ValueNames.Vendors, (x) => new ValueObject(x.Id.ToString(), x.Name)); | |
ValueObjectRegistry.ConfigureFor<Vendor>(ValueNames.VendorUrls, | |
(x) => | |
new ValueObject( | |
_urlRegistry.UrlFor(new GetVendorRequest<Vendor> {Id = x.Id}), | |
x.Name)); | |
var vendors = _service.ListAllVendors(); | |
ValueObjectRegistry.AddValueObjects(vendors); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment