Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesDependency InjectionDependency Injection
Previous
 
Next
New Post
6/22/2017 10:44 AM
 
Hi I use Autofac for Dependency injection with DNN (7.4+)
I use ChrisHammond Template.
https://github.com/ChrisHammond/DNNTe...

You need to manually Install Autofac and WebActivatorEx from Nuget


[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(purplecs.Modules.MedulawPodcast.Bindings), "RegisterServices")]
namespace purplecs.Modules.MedulawPodcast
{
public class Bindings
{//
public static IContainer Container { get; set; }

public static void RegisterServices()
{
var builder = new ContainerBuilder();

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where((x) => x.Name.EndsWith("Repository"))
.AsSelf();

Container = builder.Build();

}
}
}

I use the above Code to Registering My business logic Classes.

I also create a base Class for Portal Module Base

public classTModuleBase : PortalModuleBase
{

public TModuleBase()
{

Bindings.Container.InjectUnsetProperties(this);
// CheckSettings();

}

}

I my ascx file I Inherit my TModuleBase

public partial class TascxClass: TModuleBase
{
public TRepository tRepo {get;set;}
}

I use Property Injection method to inject the objects.

I suppose the same approach can be used for A MVC application too



Hope this helps





 
New Post
6/22/2017 10:45 AM
 
Hi I use Autofac for Dependency injection with DNN (7.4+)
I use ChrisHammond Template.
https://github.com/ChrisHammond/DNNTemplates

You need to  manually Install Autofac and WebActivatorEx  from Nuget


[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(purplecs.Modules.MedulawPodcast.Bindings), "RegisterServices")]
namespace purplecs.Modules.MedulawPodcast
{
    public class Bindings
    {//
        public static IContainer Container { get; set; }

        public static void RegisterServices()
        {
            var builder = new ContainerBuilder();

            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                .Where((x) => x.Name.EndsWith("Repository"))
                .AsSelf();
          
            Container = builder.Build();

        }
    }
}

I use the above Code to Registering My business logic Classes.

I also create a base Class for Portal Module Base 

 public classTModuleBase : PortalModuleBase
    {

        public TModuleBase()
        {

            Bindings.Container.InjectUnsetProperties(this);
        //    CheckSettings();

        }
  
    }

I my ascx file I Inherit my  TModuleBase

  public partial class TascxClass: TModuleBase
{
public TRepository tRepo {get;set;}
}

I use Property Injection method to inject the objects.

I suppose the same approach can be used for A MVC application too



Hope this helps





 
New Post
6/22/2017 4:39 PM
 
Thanks Phil,

It's definitely a start.

Allow me to reiterate the general concepts. You use WebActivatorEx to load and configure the container into your module. You then use AutoFac's property injection in your Base object for your module pages.

Thank you, this is a good work around for implementing dependency injection into my modules.

I am still looking for information related to the Dependency Injection built into DNN using the ComponentFactory, but this was very helpful.
 
Previous
 
Next
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesDependency InjectionDependency Injection


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out