1

I'm finding myself in need of "combining" several instances of the same type. This type has several IList properties on it. I want to take each instance and combine the values of those IList properties across the instances, so my code only needs one of those instances.

I'm thinking of creating an ICombinable interface, but I'm wonder if there's something already out there that's suited to this?

 public interface ICombinable<T> { void CombineWith(T instance); } 
6
  • sounds like a code smell to me.....? Commented Sep 28, 2010 at 3:18
  • More info, please. How would you use this interface? Commented Sep 28, 2010 at 3:18
  • @ Mitch: Code smell? What's that? Commented Sep 28, 2010 at 3:18
  • More info: I have a MyConfigurationInfo object that I get from 1. A file, 2. A database. Each MyConfigurationInfo has a List<string> They can each exist independently, but if both exist, I don't want to have to look in both instances for some string. I just want one instance with all the info I need. So, let's say I want just one static property I can use like MyProgram.MyConfigurationInfo, which would have the info combined from the two. Commented Sep 28, 2010 at 3:21
  • What do you do if a key exists in both places with different values? Commented Sep 28, 2010 at 3:25

3 Answers 3

1

Have you tried looking at System.Collections.Generic.HashSet<T>? If you add the same thing multiple times, only 1 item exists.

Sign up to request clarification or add additional context in comments.

1 Comment

The question wasn't so much about how to distinct the values, but rather how to aggregate them and access them in a fluent way. Though +1, as incorporate a unique-ing HashSet into the Select/SelectMany solution below is a good idea!
0

Sounds like you need Concat

var configs = dbConfig.configList.Concat(fileConfig.configList); 

3 Comments

But I have several of these configLists on fileConfig and dbConfig that need concatanating...and then I want them all accessible through a single instance of MyConfigurationInfo
There is definitely not enough information here for a comparison to an existing solution, but it sounds like you are trying to oversimplify your configuration. I'd recommend that you take a step back and analyze your design. Why do you have multiple sources of configuration info? If there is a valid reason, then you should be able to build a provider based on that logic that can expose your configuration coherently. But from your description I'm having a hard time working backwards to the design.
I have two different configuration sources of the same structure. One is for installation specific configuration information (this is stored in an xml config file). The second is application wide configuration settings that are pulled from a DB over WCF. Does that help?
0

I ended up using SelectMany and Select for this. IConfiguration is an interface for the MyConfigurationInfo class. GetMyConfigurationSources returns all the different IConfigurations (from files, DB, etc).

 // accumulates an enumerable property on IConfiguration public static IEnumerable<TValue> GetConfigurationValues<TValue>(Func<IConfiguration, IEnumerable<TValue>> selector) { // cast included for clarification only return (GetMyConfigurationSources() as IEnumerable<IConfiguration>) .Where(c => selector(c) != null) .SelectMany(selector); } // accumulates a non enumerable property on IConfiguration public static IEnumerable<TValue> GetConfigurationValues<TValue>(Func<IConfiguration, TValue> selector) { // cast included for clarification only return (GetMyConfigurationSources() as IEnumerable<IConfiguration>) .Where(c => selector(c) != null) .Select(selector); } // Example usage: static void Main() { string[] allEnumerableValues = GetConfigurationValues(c => c.SomeEnumerableConfigPropertyOfStrings); string[] allNonEnumerableValues = GetConfigurationValues(c => c.SomeNonEnumerableConfigPropertyString); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.