<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Dependency Injection and command classes in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/6569847#M62844</link>
    <description>&lt;P&gt;I want to implement&amp;nbsp;constructor dependency injection, which is considered to be good practice in c# &amp;nbsp;development. I have managed to do it in AutoCad and it was really useful. &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 19 Sep 2016 05:59:49 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2016-09-19T05:59:49Z</dc:date>
    <item>
      <title>Dependency Injection and command classes</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/6569334#M62842</link>
      <description>&lt;P&gt;Hello. I want to use DI and IoC in my plugin. And I want to inject my dependencies in constructor. To do this, I need to get control of command class creation. Let assume we have such code in command class&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    [Transaction(TransactionMode.Manual)]
    class CmdTypeToMarkSeparateType : IExternalCommand
    {
        private ISomeService _service { get; set; }
        // some service should be passed to command.
        // In fact it is constructor injection
        public CmdTypeToMarkSeparateType(ISomeService service)
        {
            _service = service;
        }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            TaskDialog.Show("Test name", _service.message);
            return Result.Succeeded;
        }
    }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to make this possible, I should be able to control creation of command classes. In AutoCAD, I achieve this, by registrering all commands by myself and wrapping them in lambda, where I am extracting all constructor parameters by my self, using my DI container. How can I achieve this in Revit? Let assume, I have such code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;private void AddPushButton(RibbonPanel panel)
{
    PushButton pushButton = panel.AddItem(new PushButtonData("HelloWorld",
        "HelloWorld", @"D:\Sample\HelloWorld\bin\Debug\HelloWorld.dll", "HelloWorld.CsHelloWorld")) as PushButton;

    // Set ToolTip and contextual help
    pushButton.ToolTip = "Say Hello World";
    ContextualHelp contextHelp = new ContextualHelp(ContextualHelpType.Url,
        "http://www.autodesk.com");
    pushButton.SetContextualHelp(contextHelp);

    // Set the large image shown on button
    pushButton.LargeImage =
        new BitmapImage(new Uri(@"D:\Sample\HelloWorld\bin\Debug\39-Globe_32x32.png"));
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As we can see we pass only classname of command class. Maybe there is a way to do something like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        private void AddPushButton(RibbonPanel panel, IServiceContainer services)
        {
            // Add push button without command
            PushButton pushButton = panel.AddItemSomeWay();

            pushButton.onClick = () =&amp;gt; {
                // Create command class using my DI container and pass dependencoes
                var cmdClass = services.GetInstance&amp;lt;CmdTypeToMarkSeparateType&amp;gt;();
                // call execute method
                cmdClass.Execute();
            }
            
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or maybe there is a way to override some default Revit activator, where it creates command class instances? Or maybe there is a way to pass parameters to execute method (it is the worst method), and I will get my service container directly in Execute method?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Sep 2016 16:34:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/6569334#M62842</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-09-18T16:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: Dependency Injection and command classes</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/6569372#M62843</link>
      <description>&lt;P&gt;I don't know why you need to do so but try to create a static public variable/method in the external command and pass to it whatever you want the process it in the Execute method.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Sep 2016 17:23:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/6569372#M62843</guid>
      <dc:creator>Mustafa.Salaheldin</dc:creator>
      <dc:date>2016-09-18T17:23:16Z</dc:date>
    </item>
    <item>
      <title>Re: Dependency Injection and command classes</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/6569847#M62844</link>
      <description>&lt;P&gt;I want to implement&amp;nbsp;constructor dependency injection, which is considered to be good practice in c# &amp;nbsp;development. I have managed to do it in AutoCad and it was really useful. &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2016 05:59:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/6569847#M62844</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-09-19T05:59:49Z</dc:date>
    </item>
    <item>
      <title>Re: Dependency Injection and command classes</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/8046996#M62845</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Did you ever get a nicer solution for this?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jun 2018 09:27:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/8046996#M62845</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-06-05T09:27:58Z</dc:date>
    </item>
    <item>
      <title>Re: Dependency Injection and command classes</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/12419400#M62846</link>
      <description>&lt;P&gt;Testability, extensibility etc...all the standard arguments for programming to an interface.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 22:43:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/12419400#M62846</guid>
      <dc:creator>RichWhitfield</dc:creator>
      <dc:date>2023-12-04T22:43:36Z</dc:date>
    </item>
    <item>
      <title>Re: Dependency Injection and command classes</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/12423294#M62847</link>
      <description>&lt;P&gt;I have implemented a DI framework in my add-in. I borrowed the SimpleContainer implementation from an early version of the ASP.NET Blazor framework while I was reading through its source code. The SimpleContainer is a singleton class that provides the registration and injection. And I have a ContainerObject base class that handles the constructor injection and property injection.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Actually there was a similar reply last year in which I shared my implementation.&lt;/P&gt;&lt;P&gt;&lt;A href="https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11441765/highlight/true#M66134" target="_blank" rel="noopener"&gt;Re: Register Services (Dependency Injection) - Autodesk Community - Revit Products&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 14:06:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/dependency-injection-and-command-classes/m-p/12423294#M62847</guid>
      <dc:creator>Kennan.Chen</dc:creator>
      <dc:date>2023-12-06T14:06:09Z</dc:date>
    </item>
  </channel>
</rss>

