nHydrate with asp.net MVC

Jan 3, 2011 at 6:34 PM

I am buidling a website using asp.net MVC together with nHydrate. But I run into a problem, which is related (IMHO) with nHydrate.
I do use the Direct method of nHydrate, so I only use the DAL.

When I use a class generated by nHydrate in the Edit View and post the entered values. I get the following error:

Server Error in '/' Application.

No parameterless constructor defined for this object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +97
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +264
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly) +16
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +68
   System.Activator.CreateInstance(Type type) +32

<snip/>

When I use a class, which I made in the folder Models manually, it goes without a problem.

Does anyone perhaps know, how to solve this ? Or does it require a change in nHydrate ?

If neccessary i can post code snippets, there are also posted on http://forums.asp.net/p/1638582/4236034.aspx
Jan 3, 2011 at 8:12 PM

Already solved it :-)

Dont know for sure if this is the correct way, but at the moment it works.
I added the following code to the partial class in x.x.Business.Objects.Persoon

        ///<summary>
        ///</summary>
        public Persoon()
        {
            var persoonCollection = new PersoonCollection();
            wrappedClass = persoonCollection.NewItem().wrappedClass;
        }
Jan 3, 2011 at 9:13 PM

I was too early happy. I do know have a problem when I execute the Persits method.

Coordinator
Jan 3, 2011 at 10:21 PM

If this framework requires you to have a parameterless constructor then you will not be able to use the NHDAL with it. You can however use the EFDAL but then you would be moving to Entity Framework. The NHDAL is built on the Factory pattern. The collection must create the object so there is no public parameterless constructor to call.

Jan 4, 2011 at 10:50 PM

Thanks for your reply.