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 7534837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:08:01+00:00 2026-05-30T06:08:01+00:00

So here’s the issue, my mvc3 project uses Dependency Injection and has a base

  • 0

So here’s the issue, my mvc3 project uses Dependency Injection and has a base Generic IRepository class from which other repositories derive.

So I can co ahead and do this in a controller:

public class SomethingController
{
    IOrderRepository repository;

    public SomethingController(IOrderRepository repo)
    {
        this.repository = repo;
    }

    public ActionResult SaveOrder(Order order)
    {
        repository.add(order)
        unitOfWork.CommitChanges();  // THIS works!
    }
}

But now i need to use one of those repositories in a custom static non-controller like this:

static class OrderParser
{
    private IOrderRepository repo;

    public static DoWork()
    {
        repo = DependencyResolver.Current.GetService<IOrderRepository>();

        var ordersInDB = repo.GetAllOrders(); //THIS works!

        //But!
        var ordersForInsertion = new List<Order>();


        //do some backgroundworker magic                     
        //fetch txt files from an ftp server
        var ordersForInsertion = ParseTextFilesIntoOrders();

        foreach order in ordersForInsertion 
             repo.add(order)
        unitOfWork.CommitChanges();
        // THIS doesnt commit anything into the database
        // It also doesnt throw any exceptions
        // and repo isnt null or any of that
    }
}

So, as a test, i tried doing:

repo = DependencyResolver.Current.GetService<IOrderRepository>();

inside a controller class like in the first example to see if it also didnt commit stuff, and it doesn’t. (Doing it the right way [injecting repositories and the unitOfWork trough the constructors] works!)

So it has to be something to do with the DependencyResolver, right?

Note: if there is any more code you need me to post, ask away and I’ll edit it in here in a flash!

Note2: Thanx!

EDIT1:

Regarding w0lf’s super fast answer
Here’s some more info:

My OrderParser class implments a backgroundWorker which is supposed to:

  • Sleep for an hour
  • List all the files (plain txt files) in an FTP server.
  • Discard the ones that are already parsed into the db.
  • Parse the new files into Order objects.
  • Commit the objects into db.
  • Start all over and over till the power goes out or something 🙂

All that has to happen without any user action, meaning, the action is not originated from a controller, hence all I do is:

in my bootstrapper class

Initialise()
{
    //Unrelated stuff
    OrderParser.DoWork()
}

And that’s also why I implemented it as a static class ( easily changable to a non-static )

EDIT2:

It would be something like:

class OrderParser
{
    private IOrderRepository repo;

    public OrderParser(IOrderRepository foo)
    {
        this.repo = foo;
    }
    public static DoWork()
    {
        //use repo var!
    }
}

But then when i instance it in the bootstrapper Initialize() method, how would i do that, e.g.:

class bootstrapper
{
    Initialize()
    {
        var parser = new OrderParser(/*how do i pass the dependency here?*/)
        parser.DoWork();
    }
}

EDIT3:

Here’s some more testing, please bear with me!

Here’s my OrderParser again:

class OrderParser
{
    public OrderParser(IOrderRepository foo, IContext unitOfWork)
    {
        foo.getall(); 

        foo.add(some_order);
        unitOfWork.commit(); 

    }
}

Test1:

public class SomeController
{
    IOrderRepository repository;

    public SomeController(IOrderRepository repo)
    {
        this.repository = repo;
    }

    public ActionResult SomeMethod(Order order)
    {
        repository.GetAll();    //WORKS

        repository.add(order)
        unitOfWork.CommitChanges();  // WORKS
    }
}

TEST2:

class bootstrapper
{
    Initialize()
    {
        //Build unity container..
        //set resolver..

        var parser = new OrderParser(container.Resolve<IOrderRepository>, container.Resolve<IContext>)
        //can getAll, cant commit.
    }
}

TEST3:

public class SomeController
{
    IOrderRepository controllers_repository;

    public SomeController(IOrderRepository repo)
    {
        this.controllers_repository = repo;
    }

    public ActionResult SomeMethod(Order order)
    {
        var parser = new OrderParser(DependencyResolver.Current.GetService<IOrderRepository>,
        DependencyResolver.Current.GetService<IContext>)   
        //can do getall, no commits


        var parser = new OrderParser(controllers_repository, controllers_icontext)
        // obviously works (can getall and commit)
    }
}

By the way, when i say “can’t commit” it’s not that i get an exception or the repositories are null, nope. the code runs as if it were okay, only the DB won’t change.

  • 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-30T06:08:02+00:00Added an answer on May 30, 2026 at 6:08 am

    One possible solution is to make the OrderParser class non-static and inject an instance of it in the constructor of the Controller that triggers the action (DoWork).

    Then make OrderParser‘s constructor take an IOrderRepository parameter and the IoC container will gladly take care of it.

    Also, beware of things like:

    DependencyResolver.Current.GetService<ISomeInterface>();
    

    This is called Service Locator and it’s considered to be an anti-pattern. Avoid it if possible.

    Basically, the only place where you should reference DependencyResolver.Current.GetService is your implementation of IControllerFactory that enables DI in the first place.

    Update:

    It would be best if you did this in another application than your MVC website. Two alternatives would be:

    • a Windows Service that performs that action based on a timer
    • a Console application that is run using Windows Task Scheduler every hour

    These, being separate applications would have their own Composition roots that would deal with the object instantiation / dependency injection issue.

    If, however, you are constrained to do this from your web app (for example – you have a hosting that only allows web apps), then you may find it acceptable to make an exception to the “Don’t use the Dependencey Resolver directly” rule and do somehing like this on the application startup:

    var runner = DependencyResolver.Current.GetService<OrderParsingRunner>();
    runner.StartWorking();
    

    Of course, the OrderParsingRunner class would look something like this:

    public class OrderParsingRunner
    {
        private readonly OrderParser orderParser;
    
        public OrderParsingRunner(OrderParser orderParser)
        {
            this.orderParser = orderParser;
        }
    
        public StartWorking()
        {
            TaskFactory.StartNew(() => 
                { 
                    DoWorkHourly();
                });
        }
    
        private DoWorkHourly()
        {
            while(true)
            {
                Thread.Sleep(TimeSpan.FromHours(1));
    
                orderParser.DoWork();
            }
        }
    }
    

    Disclaimer: I haven’t actually compiled/run this code, I just wrote it to illustrate the concept.

    Please note that this is a workaround rather than an actual solution. It’s recommended that you use another application for the background tasks if possible.

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

Sidebar

Related Questions

Here is an example: I have a file 1.js, which has some functions. I
Here is the issue I am having: I have a large query that needs
Here is my code, which takes two version identifiers in the form 1, 5,
Here is an example: I have the generic type called Account. I wish to
Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here a simple question : What do you think of code which use try
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here's a basic regex technique that I've never managed to remember. Let's say I'm

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.