Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 922687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:04:28+00:00 2026-05-15T19:04:28+00:00

In my latest ASP.NET MVC 2 application I have been trying to put into

  • 0

In my latest ASP.NET MVC 2 application I have been trying to put into practice the concepts of Domain Driven Design (DDD), the Single Responsibility Principle (SRP), Inversion of Control (IoC), and Test Driven Development (TDD). As an architecture example I have been following Jeffery Palermo’s “Onion Architecture” which is expanded on greatly in ASP.NET MVC 2 in Action.

Onion Architecture Diagram

While, I have begun to successfully apply most (some?) of these principles I am missing a key piece of the puzzle. I am having trouble determining the best mechanism for auto-wiring a service layer to my domain entities.

As an example: each domain entity that needs the ability to send an email should depend on an IEmailService interface. From my reading, best practice to reveal this dependency would be to use constructor injection. In my UI layer I perform a similar injection for repository interface implementations using the StructureMapControllerFactory from ASP.NET MVC Contrib.

Where I am confused is what is the best mechanism for auto-wiring the injection of the necessary services into domain entities? Should the domain entities even be injected this way? How would I go about using IEmailService if I don’t inject it into the domain entities?

Additional Stack Overflow questions which are great DDD, SRP, IoC, TDD references:

  • IoC Containers and Domain Driven Design
  • How to avoid having very large objects with Domain Driven Design
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:04:28+00:00Added an answer on May 15, 2026 at 7:04 pm

    Unless I’m misunderstanding your intent and instead I’m choosing to focus on semantics I’m going to dissect this statement “As an example: each domain entity that needs the ability to send an email should depend on an IEmailService interface.”

    I would have to argue this is upon itself is an extreme bastardization of DDD. Why should a domain entity ever need to depend on an email service? IMO it shouldn’t. There is no justification for it.

    However there are business operations in conjunction with a domain entity that would require the need to send emails. You should have your IEmailService dependency contained in this class here, not the domain entity. This class would most likely fall into one of a few nearly synonymous names: Model, Service or Controller dependent upon which architecture/layer you’re in.

    At this point your StructureMapControllerFactory would then correctly auto wire everything that would use the IEmailService.

    While I might be minorly over generalizing it’s pretty much standard practice to have domain entities be POCOs or be nearly POCOs (to avoid violation of the SRP) however frequently SRP is violated in domain entities for sake of serialization and validation. Choosing to violate SRP for those types of cross cutting concerns is more of a personal belief stance as opposed to a “right” or “wrong” decision.

    As a final follow up if your question is on the portion of code that is truly operating in a stand alone service whether web or OS based and how to wire up the dependencies from that, a normal solution would be take over the service at a base level and apply IOC to it in the same similar fashion as the StructureMapControllerFactory does in MVC. How to achieve this would be entirely dependent upon the infrastructure you’re working with.

    Response:

    Lets say you have IOrderConfirmService which has a method EmailOrderConfirmation(Order order). You would end up with something like this:

    public class MyOrderConfirmService : IOrderConfirmService
    {    
        private readonly IEmailService _mailer;
    
        public MyOrderConfirmService(IEmailService mailer)
        {        
            _mailer = mailer;        
        }
    
        public void EmailOrderConfirmation(Order order)
        {        
            var msg = ConvertOrderToMessage(order); //good extension method candidite
            _mailer.Send(msg);        
        }    
    }
    

    You would then have your OrderController class that would be something like

    public class OrderController : Controller
    {    
        private readonly IOrderConfirmService _service;
    
        public OrderController(IOrderConfirmService service)
        {        
            _service= service;        
        }
    
        public ActionResult Confirm()
        {      
                _service.EmailOrderConfirmation(some order);
    
                return View();
        }    
    }
    

    StrucutreMap will inherently build up you’re entire architecture chain when you use constructor injection correctly. This is the fundamental difference between tight coupling and inversion of control. So when the StructureMapFactory goes to build up your controller the first thing it will see is that it needs IOrderConfirmService. At this point it will check if it can plug IOrderConfirmService directly which it can’t because it needs IEmailService. So it will check if it can plug IEmailService and for argumentsake lets say it can. So at this point it will build EmailService, which it will then build MyOrderConfirmService and plug in EmailService, and then finally build OrderController and plug in MyOrderConfirmService. This is where the term inversion of control comes from. StructureMap will build the EmailService first in the entire chain of dependencies and ending last with the Controller. In a tightly coupled setup this will be the opposite where the Controller will be built first and have to build the business service and then build the email service. Tightly coupled design is very brittle when compared to IOC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I upgraded a large ASP.NET MVC application I've been working on to the latest
I have a small ASP.NET MVC application developed using the latest beta version. What
I'm trying to implement MVC 2 RC version, the latest release of ASP.Net MVC
I'm trying to get client validation working on my asp.net mvc 2 web application
I have an ASP.NET MVC application that has a jQuery Treeview and a jQuery
I am trying to use the MvcMiniProfiler in my Asp.Net MVC application. I installed
I have a web application built in the asp.net 2.0 MVC pattern. Now the
I'm using the latest RC with asp.net mvc 3, and have turned on unobtrusive
I am trying to deploy an ASP.NET MVC 4 application with Entity Framework 4.4
I am on the way to build an ASP.NET MVC application with the latest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.