{"id":4828,"date":"2014-03-30T15:43:44","date_gmt":"2014-03-30T15:43:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/mef-and-abstract-factory-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:43:44","modified_gmt":"2014-03-30T15:43:44","slug":"mef-and-abstract-factory-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/mef-and-abstract-factory-collection-of-common-programming-errors\/","title":{"rendered":"MEF and ABSTRACT FACTORY-Collection of common programming errors"},"content":{"rendered":"<p><em>UPDATE: Should I try to handle it by DI container or it&#8217;s a wrong abstraction level here?<\/em><\/p>\n<p>I would like to implement ABSTRACT FACTORY using MEF (.NET 4.5).<\/p>\n<p>It doesn&#8217;t work for me&#8230;<\/p>\n<blockquote>\n<p>The composition remains unchanged. The changes were rejected because of the following error(s):<\/p>\n<p>The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.<\/p>\n<p>1) No exports were found that match the constraint:<\/p>\n<p>ContractName Mef3.Factory<\/p>\n<p>RequiredTypeIdentity Mef3.Factory Resulting in: Cannot set import &#8216;Mef3.Program._factory (ContractName=&#8221;Mef3.Factory&#8221;)&#8217; on part &#8216;Mef3.Program&#8217;.<\/p>\n<p>Element: Mef3.Program._factory (ContractName=&#8221;Mef3.Factory&#8221;) &#8211;&gt; Mef3.Program<\/p>\n<\/blockquote>\n<p>Is it the right way to do it in MEF? How can I forward id to Foo\/Bar ctors?<\/p>\n<p>The code:<\/p>\n<pre><code>class Program\n{\n    static void Main(string[] args)\n    {\n        var program = new Program();\n        program.Run();\n    }\n\n    readonly CompositionContainer _container;\n\n    public Program()\n    {\n        var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());\n        _container = new CompositionContainer(catalog);\n        _container.ComposeParts(this);\n    }\n\n    [Import]\n    public Factory _factory;\n\n    public void Run()\n    {\n        var foo = _factory.GetInstance(\"foo\", 123);\n        Console.WriteLine(foo is Foo);\n    }\n}\n\n[Export]\npublic class Factory\n{\n    private readonly ExportFactory _fooFactory;\n    private readonly ExportFactory _barFactory;\n\n    [ImportingConstructor]\n    public Factory(ExportFactory fooFactory, ExportFactory barFactory)\n    {\n        _fooFactory = fooFactory;\n        _barFactory = barFactory;\n    }\n\n    public Base GetInstance(string name, int id)\n    {\n        switch (name)\n        {\n            case \"foo\":\n                return _fooFactory.CreateExport().Value;\n\n            case \"bar\":\n                return _barFactory.CreateExport().Value;\n        }\n\n        throw new ArgumentException();\n    }\n}\n\npublic class Foo : Base\n{\n    [ImportingConstructor]\n    public Foo([Import(\"Id\")] int id)\n    {\n    }\n}\n\npublic class Bar : Base\n{\n    [ImportingConstructor]\n    public Bar([Import(\"Id\")] int id)\n    {\n    }\n}\n\n[InheritedExport]\npublic abstract class Base\n{\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: Should I try to handle it by DI container or it&#8217;s a wrong abstraction level here? I would like to implement ABSTRACT FACTORY using MEF (.NET 4.5). It doesn&#8217;t work for me&#8230; The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4828","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4828"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4828\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}