0

I am using VS'15 on asp.net 4.6.

I am also modeling my application after the N-Tier design found at:

Implementing a generic data access layer using Entity Framework

I am able to return data from a User table, however when using code based on the User code to return data from another table, I get a null return.

My BLL code that works:

using System;
using System.Collections.Generic;
using System.Linq;
using Library.DataAccessLayer;
using Library.Model;

namespace Library.BusinessLogicLayer
{
    public interface IBusinessLogicLayer_User
    {
        IList<UsersModel> GetAllUsers();
        UsersModel GetUserByAapNetUserID(string _UserID);
        void UpdateUser(params UsersModel[] _UserModel);
    }

    public class BusinessLogicLayer_User : IBusinessLogicLayer_User
    {
        private readonly IUsersRepository _UsersRepository;

        public BusinessLogicLayer_User()
        {
            _UsersRepository = new UsersRepository();
        }

        public BusinessLogicLayer_User(IUsersRepository userRepository)
        {
            _UsersRepository = userRepository;
        }

        public IList<UsersModel> GetAllUsers()
        {
            return _UsersRepository.GetAll();
        }

        public UsersModel GetUserByAapNetUserID(string _UserID)
        {
            return _UsersRepository.GetSingle(u => u.AspNetUserID.Equals(_UserID));
        }

        public void UpdateUser(params UsersModel[] _UsersModel)
        {
            _UsersRepository.Update(_UsersModel);
        }
    }
}

My BLL code that does not work:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Library.DataAccessLayer;
using Library.Model;

namespace Library.BusinessLogicLayer
{
    public interface IBusinessLogicLayer_AreaSecurity
    {
        AreaSecurityModel GetAreaSecurityByUserID(string _AspNetUserID);
        void AddAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel);
        void UpdateAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel);

        IList<AreaSecurityModel> GetAllAreaSecurity();
    }

    public class BusinessLogicLayer_AreaSecurity : IBusinessLogicLayer_AreaSecurity
    {
        private readonly IAreaSecurityRepository _AreaSecurityRepository;

        public BusinessLogicLayer_AreaSecurity()
        {
            _AreaSecurityRepository = new AreaSecurityRepository();
        }

        public BusinessLogicLayer_AreaSecurity(IAreaSecurityRepository areaSecurityRepository)
        {
            _AreaSecurityRepository = areaSecurityRepository;
        }

        public void AddAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel)
        {
            _AreaSecurityRepository.Add(_AreaSecurityModel);
        }

        public IList<AreaSecurityModel> GetAllAreaSecurity()
        {
            return _AreaSecurityRepository.GetAll();
        }

        public AreaSecurityModel GetAreaSecurityByUserID(string _AspNetUserID)
        {
            return _AreaSecurityRepository.GetSingle(r => r.AspNetUserID.Equals(_AspNetUserID));
        }

        public void UpdateAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel)
        {
            _AreaSecurityRepository.Update(_AreaSecurityModel);
        }
    }
}

And here is the code that is calling the stuff that works and does not work:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Library.BusinessLogicLayer;
using Library.Model;


namespace IdahoFalls9thWardBulletin.Controllers
{
    public class ApplicationBaseController : Controller
    {
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (User != null)
            {
                var context = new ApplicationDbContext();
                var username = User.Identity.Name;

                if (!string.IsNullOrEmpty(username))
                {
                    var user = context.Users.SingleOrDefault(u => u.UserName == username);

                    BusinessLogicLayer_User _BusinessLogicLayer_User = new BusinessLogicLayer_User();
                    var UserDetails = _BusinessLogicLayer_User.GetUserByAapNetUserID(user.Id);
                    ViewData.Add("FirstName", UserDetails.FirstName);

                    BusinessLogicLayer_AreaSecurity _BusinessLogicLayer_AreaSecurity = new BusinessLogicLayer_AreaSecurity();
                    AreaSecurityModel _AreaSecurityModel = _BusinessLogicLayer_AreaSecurity
                        .GetAllAreaSecurity()
                        .ToList()
                        .FirstOrDefault();
                    ViewData.Add("AreaSecurity", _AreaSecurityModel);
                }
            }
            base.OnActionExecuted(filterContext);
        }
        public ApplicationBaseController()
        {
        }
    }
}

What I do not understand is that when I do _BusinessLogicLayer_AreaSecurity.GetAllAreaSecurity().ToList().FirstOrDefault() It does in fact return the list of all records.

However when I do _BusinessLogicLayer_AreaSecurity.GetAllAreaSecurity().GetAreaSecurityByUserID(user.Id) It returns the null value.

I have no idea what I am missing here. This is basically my first attempt at MVC code first.

John Schultz
  • 672
  • 1
  • 10
  • 29
  • 6
    [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/q/4660142/447156) – Soner Gönül Dec 22 '15 at 07:43
  • @SonerGönül, Thats great for a `NullReferenceException`, however it does not explain why in one instance code that should return data does for one thing and not another. – John Schultz Dec 22 '15 at 07:46
  • It doesn't return anything because the linq expression matches nothing. That's where you have to check for an error, most likely a logic problem. – SBI Dec 22 '15 at 07:48

1 Answers1

1

I am ashamed to say that the problem is that my database for the security table is set as a Guid not a string.

John Schultz
  • 672
  • 1
  • 10
  • 29