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

  • Home
  • SEARCH
  • 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 6874981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:15:47+00:00 2026-05-27T04:15:47+00:00

Introduction I’m using ASP.Net MVC3. My Controllers talk to a service layer, and the

  • 0

Introduction

I’m using ASP.Net MVC3. My Controllers talk to a service layer, and the service layer talks to a Data Acces layer which uses Entity Framework.
I get a specific entity using Entity Framework. This entity is converted into a DTO. Then I deliver this DTO to a MVC controller. Something like this:

pseudo code:

// This is inside my Service Layer
var entity = DataAccess.GetById(id);
var dto = createDtoWithValuesFrom(entity);
return dto; // Return dto to MVC controller

In this DTO I would like to use a dependency, to for example a Calculator. Let’s say my DTO looks like this:

public class Customer
{
  private ICalculator Calculator;

  public class Customer(ICalculator calculator)
  {
     Calculator = calculator;
  }

  public string Name { get; set; }
  public decimal Discount
  {
     get
     {
        return Calculator.Discount();
     }
  }
}

Problem

How do I instanciate my DTO, and let Autofac inject a calculator?
I can think of a way to do this:

var calculator = DependencyResolver.Current.GetService<ICalculator>;
var dto = new DTO(calculator );

But I don’t know if this is the best way to do it, since it smells of ServiceLocator, and I’ve read that it’s not prefered to use that.

  • 1 1 Answer
  • 0 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-27T04:15:48+00:00Added an answer on May 27, 2026 at 4:15 am

    DTOs normally have some properties and do not contain any logic.
    You should consider a design where your MVC-Controller does something like this:

    1. Get the customer from the service/dataaccess
    2. Calculate the discount by invoking the ICalculator which could be passed to the Controller using constructor injection (or call an extra service which does the calculation)
    3. Create a new model class which contains the customer and the calculated discount and pass this model to the view.
    public class Model
    {
      public Customer Customer { get; set; }
    
      public double Discount { get; set; }
    }
    
    public class SomeController : Controller
    {
      private readonly DataAccess dataAccess;
      private readonly ICalculator calculator;
    
      public SomeController(DataAccess dataAccess, ICalculator calculator)
      {
        this.dataAccess = dataAccess;
        this.calculator = calculator;
      }
    
      public ActionResult Index(int id)
      {
        var model = new Model();
        model.Customer = this.dataAccess.Get(id);
        model.Discount = this.calculator.Calculate(customer);
    
        return View(model);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I watched a little introduction into ASP.NET Dynamic Data, and I noticed this option
Introduction We have an OpenID Provider which we created using the DotNetOpenAuth component. Everything
Introduction I'm writing a web application (C#/ASP.NET MVC 3, .NET Framework 4, MS SQL
Introduction I have been so annoyed by applications that have a startup dialog which
INTRODUCTION I'm using excel downloads as a way of users downloading a score sheet,
Long introduction: Normally all data necessary for my web application are stored in session
Introduction I am attempting to replace an on-call cell phone which is carried by
Introduction After watching this video from LIDNUG, about .NET code protection http://secureteam.net/lidnug_recording/Untitled.swf (especially from
Short Introduction I am kind of new in the field of app development using
Has the introduction of the .net framework made raw programming in COM and DCOM

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.