Resolving complex named type in Autofac

32 views Asked by At

//I am trying to resolve a complex named type in Autofac which is getting resolved properly and //easily in Unitycontainer. But facing issues

//I tried the following //I have a mail MailTopic class which looks like

class MailTopic
{
    public virtual bool IsEnabled { get; set; } = true;
    public virtual string TopicName { get; set; }
    public virtual string From { get; set; }
    public virtual string FromDisplayName { get; set; }
    public virtual string To { get; set; }
    public virtual bool CC { get; set; }
    public virtual List<MailTopicTemplate> Templates { get; set; }
}

// MailTopicTemplate class

class MailTopicTemplate
{
    public virtual string TemplateName { get; set; }
    public virtual string Subject { get; set; }
    public virtual string TemplateFile { get; set; }
    public virtual string Template { get; set; }
}

// Structure of mailtopic in appsettings file

"mailtopics": {
    "mailtopic": [
        {
            "topicName": "complianceCheckNotification",
            "from": "blabla",
            "fromDisplayName": "blablalbla",
            "to": "myself",
            "templates": [
                {
                    "templatename": "generic",
                    "subject": "important",
                    "templatefile": "Templates\\Compliance.html"
                }
            ]
        },
        {
            "topicName": "techReportnotification",
            "from": "blabla",
            "fromDisplayName": "blablalbla",
            "to": "myself",
            "templates": [
                {
                    "templatename": "technical",
                    "subject": "important",
                    "templatefile": "Templates\\Technical.html"
                }
            ]
        }
    ]
}

//in unitycontainer container.Resolve<MailTopic>("complianceCheckNotification") //is resolving the mailtopic properly

// Autofac registration and resolution

builder.RegisterType<MailTopic>().Named<MailTopic>("complianceCheckNotification");
    MailTopic topic = builder.ResolveNamed<MailTopic>("complianceCheckNotification");

I am getting null values in the topic except the IsEnabled Could someone please let me know how to achieve this in Autofac

1

There are 1 answers

2
Guru Stron On

Not sure why are you expecting Autofac to read the data from some JSON file. RegisterType will just register type hooking it with DI. Basically in your case you are registering new MailTopic() as provider factory, it knows nothing about settings, JSON files, etc.

Depending on the use case you can read the data from the config and use RegisterInstance to add it to the DI container. For example (assuming the JSON provided in the question):

var mailTopics = configuration.GetSection("mailtopics:mailtopic").Get<List<MailTopic>>();
var builder = new ContainerBuilder();
foreach (var mailTopic in mailTopics)
{
    builder.RegisterInstance(mailTopic).Named<MailTopic>(mailTopic.TopicName);
}

var container = builder.Build();
MailTopic topic = container.ResolveNamed<MailTopic>("complianceCheckNotification");

See also: