.NET Framework Bookmark and Share   
 index > Managed Extensibility Framework > Extensibility and hiding
 

Extensibility and hiding

For asmall R&D project I'm thinking about creating some glue like MEF is.
Take this case:
I want to make modules, and allow them access to a resource such as a ConfigProvider. As my config might be accessed a lot, I want to make sure my config is only accessed once, until I release it because of a change(this might be indicated by the provider).
Could such a thing be done using MEF? Hiding and managing resources?
Alex de Groot
Hi Alex, thanks for your question! If I understood correctly your requirements are

1. Only read config once in the convient time before it is accessed.
2. Have thecondigprovider control the lifetime of the config.

I've created a small example that illustrates some MEF concepts, like ICompositionService that can be imported by a component (Provider) and be used to contribute to the pool of objects (release new configs). Another concept is subscribing for INotifyImport interface that can be used to execute code right after component is bound - that's the rime for Config component to initialize (be read).

Please let me know if that any close to what you were asking about...

[ContractType]
public interface IConfig { string Id { get; set; } }

[Export(typeof(IConfig))]
public class Config: INotifyImport
{
public string Id { get; set; }
public void ImportCompleted()
{
Console.WriteLine("Config is being read...");
}
}

public class ConfigProvider
{
ComponentBinder binder;

public void Provide()
{
if (binder != null)
{
CompositionService.RemoveValuesAndBinder(binder);
}
binder = new ReflectionBinder(new Config() { Id = Guid.NewGuid().ToString("N") });
CompositionService.AddBinder(binder);
CompositionService.Bind();
}

[Import]
public ICompositionService CompositionService { get; set; }

}

public class ConfigConsumer
{
[Import(IsOptional = true)]
public IConfig Config { get; set; }
}

class Program
{
static void Main(string[] args)
{
var container = new CompositionContainer();
var consumer = new ConfigConsumer();
var provider = new ConfigProvider();
container.AddComponent<ConfigProvider>(provider);
container.AddComponent<ConfigConsumer>(consumer);
container.Bind();

provider.Provide();
Console.WriteLine(consumer.Config.Id);
Console.WriteLine(consumer.Config.Id);
provider.Provide();
Console.WriteLine(consumer.Config.Id);
Console.ReadLine();
}
}


Alex Bulankou, Microsoft Corporation. This posting is provided "AS IS".
Alex Bulankou
Hi Alex, thanks for your question! If I understood correctly your requirements are

1. Only read config once in the convient time before it is accessed.
2. Have thecondigprovider control the lifetime of the config.

I've created a small example that illustrates some MEF concepts, like ICompositionService that can be imported by a component (Provider) and be used to contribute to the pool of objects (release new configs). Another concept is subscribing for INotifyImport interface that can be used to execute code right after component is bound - that's the rime for Config component to initialize (be read).

Please let me know if that any close to what you were asking about...

[ContractType]
public interface IConfig { string Id { get; set; } }

[Export(typeof(IConfig))]
public class Config: INotifyImport
{
public string Id { get; set; }
public void ImportCompleted()
{
Console.WriteLine("Config is being read...");
}
}

public class ConfigProvider
{
ComponentBinder binder;

public void Provide()
{
if (binder != null)
{
CompositionService.RemoveValuesAndBinder(binder);
}
binder = new ReflectionBinder(new Config() { Id = Guid.NewGuid().ToString("N") });
CompositionService.AddBinder(binder);
CompositionService.Bind();
}

[Import]
public ICompositionService CompositionService { get; set; }

}

public class ConfigConsumer
{
[Import(IsOptional = true)]
public IConfig Config { get; set; }
}

class Program
{
static void Main(string[] args)
{
var container = new CompositionContainer();
var consumer = new ConfigConsumer();
var provider = new ConfigProvider();
container.AddComponent<ConfigProvider>(provider);
container.AddComponent<ConfigConsumer>(consumer);
container.Bind();

provider.Provide();
Console.WriteLine(consumer.Config.Id);
Console.WriteLine(consumer.Config.Id);
provider.Provide();
Console.WriteLine(consumer.Config.Id);
Console.ReadLine();
}
}


Alex Bulankou, Microsoft Corporation. This posting is provided "AS IS".
Alex Bulankou

You can use google to search for other answers

Custom Search

More Threads

• Create a Ribbon Control for MS Project
• can we design forms with MAPI to have custom regions in folder view area
• One framework to rule them all?
• How to pass an Enum to a Functon
• Why multi ExportAttribute is not supportted by property?
• no microphone
• pinvoke query
• Looking for example of some usercontrol/tabitem exporting a ToolBar object
• Why Addin is not loaded?
• Order of ImportCompleted