1

I am creating one demo application to learn how to use repository pattern for performing Insert operation.I am using Nop Commerce**(http://www.nopcommerce.com) **code for repository pattern

Error:No parameterless constructor defined for this object

I have seen this link:MVC: No parameterless constructor defined for this object

This is my Structure:

My Repository interface:

public partial interface IRepository<T>
    {
        void Insert(T entity);
     }

My Service Layer:

 public partial interface IEmployeeService
    {
        void InsertCategory(EmployeeMaster employeeMaster);
    }

My Class which will implement that interface(service):

public partial class EmployeeService : IEmployeeService
    {
        #region Fields
        private readonly IRepository<EmployeeMaster> _employeeMasterRepository;
        #endregion

        #region Ctor
        public EmployeeService
            (
            IRepository<EmployeeMaster> employeeMasterRepository
            )
         {
             this._employeeMasterRepository = employeeMasterRepository;
         }

        #endregion



public virtual void InsertCategory(EmployeeMaster employeeMaster)
        {
            if (employeeMaster == null)
                throw new ArgumentNullException("employeeMaster");

            _employeeMasterRepository.Insert(employeeMaster);
}

This is my controller:

public class HomeController : Controller
    {
        #region Fields
        private readonly IEmployeeService  _employeeService;
        #endregion

 #region Constructors
        public HomeController
        (
            IEmployeeService employeeService
        )
        {
            this._employeeService = employeeService;
        }
        #endregion

Getting Error:No parameterless constructor defined for this object

I have studied regarding this error and all the sources are saying that use Dependency injection to solve this error.

Can anybody guide me how to use dependency injection to solve this error??

Community
  • 1
  • 1
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • 2
    use Dependency Injection (DI)/ Inversion of Control (IoC) like ninject or autofac similar. – sakir Feb 24 '15 at 11:28
  • i have already written in my question that how to use it – I Love Stackoverflow Feb 24 '15 at 11:29
  • 1
    That will depend on which DI framework you use. Ninject, Unity, Autofac, Castle Windsor, etc. This question is far too broad as it stands. –  Feb 24 '15 at 11:30
  • 1
    Why is everything in bold text? It's hurting my eyes! – DavidG Feb 24 '15 at 11:30
  • @DavidG:one of the user of stackoverflow told me once that if there is some important point that you want to address then make it bold – I Love Stackoverflow Feb 24 '15 at 11:31
  • 1
    You will have to override MVC's default `IControllerFactory`. See this for instance: http://develoq.net/2010/custom-controller-factory-in-asp-net-mvc/ – Steven Feb 24 '15 at 11:32
  • 1
    @MariaPithia: But that's no reason to make your complete question bold. – Steven Feb 24 '15 at 11:33
  • 1
    @MariaPithia But you have made virtually everything bold which makes it a bit pointless :) – DavidG Feb 24 '15 at 11:33
  • Downvoters please give me your reason for downvoting as i have clearly explain everything. – I Love Stackoverflow Feb 24 '15 at 11:34
  • @DavidG:ok let me edit the question for your convinience – I Love Stackoverflow Feb 24 '15 at 11:34
  • @StephenMuecke:is solution so complex to write here??? – I Love Stackoverflow Feb 24 '15 at 11:39
  • @MariaPithia, I mentioned only 4 (there are many more). Each have different requirements. You need to do some research and choose one. Then if you have problems, post another question with the code. –  Feb 24 '15 at 11:50
  • @StephenMuecke:Thanks i would do as you say but i have noticed that this kind of question always receive downvotes as solutions are complex and those question whose solutions are easy always receive upvotes – I Love Stackoverflow Feb 24 '15 at 11:54
  • 2
    @MariaPithia, It has nothing to do with being complex or not. Its because SO is for helping you to solve problems with your code, but you haven't shown any code (relating to configuring DI for your app). And it would take 10 pages to cover all the variations. You need to narrow down you question to a specific problem. –  Feb 24 '15 at 12:01
  • yaa I do agree with that SO has helped me alot whenever i have been stuck at any place and i found it best to post any programming problem. – I Love Stackoverflow Feb 24 '15 at 12:07
  • i am ready to send my project if anybody is interested to solve this error.its a vry small project with only one class and one method on controller side – I Love Stackoverflow Feb 24 '15 at 12:07
  • @DavidG:sir can you please give me any reference link to study good repository pattern which most programmers uses?? – I Love Stackoverflow Feb 25 '15 at 05:05
  • @StephenMuecke:sir can you please give me any reference link to study good repository pattern which most programmers uses?? – I Love Stackoverflow Feb 25 '15 at 05:07
  • @Steven:sir can you please give me any reference link to study good repository pattern which most programmers uses?? – I Love Stackoverflow Feb 25 '15 at 05:08
  • @sakir:sir can you please give me any reference link to study good repository pattern which most programmers uses?? – I Love Stackoverflow Feb 25 '15 at 05:08

2 Answers2

4

Maybe you're taking as a sample a project that is too complex for you at this moment. Nopcommerce is a big and full featured product that has a lot of elements, so it easy to get lost. Not the best sample to learn how the repository pattern works, but I certainly recommend you to check it again once you have the basic concepts clear to see them used in a real scenario.

NopCommerce uses Autofac for dependency injection, a very popular IoC container. You can look for a class called DependencyRegistrar in the project Nop.Web.Framework to see how it is used, if you're curious about it. You can get more examples on how to use dependency injection with Autofac in their repository and their getting started guide.

My recommendation is to look for an easier to follow example. For starters, any of the popular IoC containers will be OK until you have your own criteria to choose one. For instance you can follow the Autofac's guide for MVC

Marco Regueira
  • 1,045
  • 8
  • 17
3

You're correct - you'll need to use something like Unity, Ninject or Autofac to get this running.

Essentially - the steps are roughly the same regardless of the tech used.

Using unity as an example:

1) In your MVC project, use NuGet to add "Unity Bootstrapper for MVC". (right click references, manage nuget packages, search for unity online, select "Unity Bootstrapper for MVC"

This will add a few things to your project - the most interesting is the unityConfig.cs in App_start.

2) In unityConfig.cs - find RegisterTypes method

RegisterTypes is where you tell the unity container "when you need one of X, use implementation Y" - in your case "when you need an IEmployeeService, use EmployeeService"

3) In register types - add your type mappings - for example, in your case:

container.RegisterType<IEmployeeService, EmployeeService>();

AND

4) Bit more complicated: as you have IRepository and you need this to resolve your employeeService correctly you'll also have to register a generic type against a generic implementation (it's a little odd). Looking at your types, it's going to be something like:

container.RegisterType(typeof(IRepository<>), typeof(Repository<>));

(You'll need to resolve the namespaces according to your projects).

So - what this is saying is...

Right MVC - when you need an IEmployeeService use employeeService, and when you need an IRepository<>, use Repository<>

Now - because of the webActivator that was added during the NuGet installation (right next door to UnityConfig.cs), that should be it - the webActivator sets the DependencyResolver.Current to something that uses this unityContainer.

Beacause of this, MVC will now use this new unityContainer whenever it tries to instantiate your controllers, resolving the type mappings on the way.

The webActivator and unityConfig classes do alot of the heavy-lifting for you, just elaborate registerTypes and you should at least have a foot-hold in the solution.

HTH - good luck.

PS - DI itself is a massive subject - far too much for here, you'll bump into all sort of weird and wonderful things (supplying Ctor args for resolutions, lifetime management just for a start!) - it's fun, but not trivial, so you'll have to excuse me if this doesn't work "out of the box" (it's almost impractical to think it would) - this is merely just a vague attempt to give you a foothold in the subject!

For more info on unity - I suggest you read up here (Dev guide to using Unity on MSDN)

Andrew Hewitt
  • 331
  • 1
  • 6
  • Sir can you please give me any reference link for good repository pattern to learn??? – I Love Stackoverflow Feb 25 '15 at 05:04
  • Hi Maria. Its something I've implemented loads of times using a variety of articles as a good start then refining. Mainly using EF as the store. If you're using EF then an "ok" start is: http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle (it not quite how I'd do it, but its not far off). Hth! – Andrew Hewitt Feb 25 '15 at 07:15
  • Hi Andrew.Thanks for replying.you have any demo project where you have done basic operation using DI and repository pattern?? – I Love Stackoverflow Feb 25 '15 at 08:40
  • Sorry - not "to hand" that I can give you on here (client confidentiality etc, am sure you understand).. also I've got no idea how to send "private" messages on here (?!). 'fraid you'll just have to have a run at it yourself and see how far you get. If you follow my instructions re: DI (I did, step by step when writing your answer) then you should have a good start. Re: repository pattern - that article's "better answer" (towards the bottom) is close (but not quite) to what I've often settled on as the ideal solution for EF and Repository patterns. – Andrew Hewitt Feb 25 '15 at 08:43
  • Its ok Andrew i totally understand but can i send you my project so that what all mistakes i have done you can guide me to improve it atleast.its a very very small project. – I Love Stackoverflow Feb 25 '15 at 08:53
  • Erm.... sure, I can have a look - how do you want to share? Honestly - I think your best (quickest) option would be to have a crack at it following the above - it's really not that tough and learning is fun! :) – Andrew Hewitt Feb 25 '15 at 08:55
  • i am using Autofac and i have followed exactly what you have said but still it would be a good experience to learn that how professional programmer like you do the task as what in a foolish r bad way i must have done – I Love Stackoverflow Feb 25 '15 at 08:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71659/discussion-between-andrew-hewitt-and-maria-pithia). – Andrew Hewitt Feb 25 '15 at 09:17