This project has moved. For the latest updates, please go here.

Complete beginner, help with MVC & MongoRepository

Nov 24, 2013 at 2:33 PM
Hi, I'm probably doing something really stupid here, I figure the best thing to do is just post my code and explain a little.

In my class library I have the following two files

my Entities.cs file
    public class User : Entity
    {
        public string EmailAddress { get; set; }
        public string FullName { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string PasswordSalt { get; set; }
    }
my Users.cs file
public class Users
    {
        static MongoRepository<User> repo = new MongoRepository<User>();

        public static bool CreateUser(string emailAddress, string fullName, string username, string encryptedPass, string salt)
        {
            var pixelPushUser = new PixelPushData.User
            {
                EmailAddress = emailAddress,
                FullName = fullName,
                UserName = username,
                Password = encryptedPass,
                PasswordSalt = salt
            };

            repo.Add(pixelPushUser);

            return true;
        }
    }
Then in my MVC project I have the following in my controller
        [HttpPost]
        public ActionResult SignUp(PixelPush.Models.SignUpModel user)
        {
            if (ModelState.IsValid)
            {

                var crypto = new SimpleCrypto.PBKDF2();
                var encryptedPass = crypto.Compute(user.Password);

                PixelPushData.Users.CreateUser(user.EmailAddress, user.FullName, user.UserName, encryptedPass, crypto.Salt);

                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Please ensure you have filled out all fields.");
            }

            return View();
        }
I get the following error from the CreateUser method call

An exception of type 'System.TypeInitializationException' occurred in PixelPush.dll but was not handled in user code

Any help?
Coordinator
Nov 25, 2013 at 8:06 AM
I am unable to reproduce the problem; are you sure the error is on the CreateUser line? What if you replaced encryptedPass and crypto.Salt with constants "foo" and "bar" for example? I am not familiar with "SimpleCrypto"; are you sure both are of type string or otherwise serializable? Are you sure the problem is with MongoRepository and not SimpleCrypto?
Coordinator
Nov 25, 2013 at 8:17 AM
Edited Nov 25, 2013 at 12:47 PM
Here is my testcase trying to reproduce the problem. I didn't use MVC because that would be too much overhead on files/stuff (probably) not related to this problem. Could you let me know if this works for you? If it doesn't, please see previous post and try without SimpleCrypto and see if that helps.
Nov 25, 2013 at 12:20 PM
Hi RobIII,

I ran your testcase which worked perfectly and created the record and test database successfully. I replaced encryptedPass and crypto.Salt with "foo" and "bar" but no luck there, I still get the same exception.

Here is a zip file containing my solution, if you could take a look that would be great, if not no worries.
Coordinator
Nov 25, 2013 at 12:44 PM
Edited Nov 25, 2013 at 12:48 PM
You need to add the connectionstring to the projects' web.config. The app.config in de PixelPushData project/namespace is not used in your project since it is not the startup-project (nor do website projects use app.condigs (unless specifically coded to do so)). When you move or copy the connectionstring to web.config your problem is solved Image
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="MongoServerSettings" connectionString="mongodb://localhost/PixelPush" />
  </connectionStrings>
  <!-- The rest of your config here -->
</configuration>
Nov 25, 2013 at 2:10 PM
Rob,

many thanks for the help and the explanation, all works perfectly now. From what you saw of my code, do you have any suggestions for progression, i.e. IOC, interfaces, etc. This is all new to me and I want to do things properly.
Coordinator
Nov 25, 2013 at 5:41 PM
There's certainly room for improvement but, honestly:
  1. All your code is mostly boilerplate for an empty MVC project with just a few "blanks filled in" so there's not that much to go on
  2. This is not the place. I'm sorry but this is the discussionforum about MongoRepository, not 3rd party code Image
If you want to do things properly and get your code reviewed you might want to take a look at http://codereview.stackexchange.com.
Dec 4, 2013 at 4:12 PM
Rob,

understood. Many thanks for the help. I'll probably give codereview a go in the future.

Cheers,
Martin