locked
linq to sql and textboxes RRS feed

  • Question

  • User1652530521 posted
    public class BusinessLogicLayer
    {
    
        #region "Private variable"
        string _Firstname;
        string _LastName;
        int _CityId;
        int _HouseNumber;
        string _CityName;
        int _PersonId;
        string _MobileNumber;
        string _Street;
        #endregion
        public string FName
        {
            get { return _Firstname; }
            set { _Firstname = value; }
        }
    
        public string LName
        {
            get { return _LastName; }
            set { _LastName = value; }
        }
    
        public string CityName
        {
            get { return _CityName; }
            //set { _CityName = value; }
        }
        public string MobileNumber
        {
            get { return _MobileNumber; }
            set { _MobileNumber = value; }
        }
    
        public string Street
        {
            get { return _Street; }
            set { _Street = value; }
        }
    
        public int PersonId
        {
            get { return _PersonId; }
            set { _PersonId = value; }
        }
    
        public int CityId
        {
            get { return _CityId; }
            set { _CityId = value; }
        }
    
         public void Update()
    {
    using (PersonsDataContext ps = new PersonsDataContext())
    {
    ps.spUpdatePersonById(_PersonId, _LastName, _Firstname, _Street, _HouseNumber, _MobileNumber, _CityId);
    }
    }
    public static List<BusinessLogicLayer> getPersonById () { using (PersonsDataContext ps = new PersonsDataContext()) { var user =( (from u in ps.Persons where u.PersonID== 1 select u).SingleOrDefault()); return user ; } } }

    Hi

    I am using Linq to SQL. I have a BLL where I want to populate a list and return it to the UI layer and populate textboxes. How can I do that?

    Also, is it better to put my select and insert functions in the DAL? My DAL is the dbml.

    Thank you

    Monday, May 14, 2018 3:00 PM

All replies

  • User1120430333 posted

    First off, you should learn how to use auto properties and eliminate the private backing variable for a publicly exposed property.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    I am using Linq to SQL. I have a BLL where I want to populate a list and return it to the UI layer and populate textboxes. How can I do that?

    You should learn how to use MVP.

    https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    https://msdn.microsoft.com/en-us/library/ff649571.aspx?f=255&MSPPError=-2147217396

    https://en.wikipedia.org/wiki/Model–view–presenter

    <copied>

    In MVP, the presenter assumes the functionality of the "middle-man". In MVP, all presentation logic is pushed to the presenter.

    <end>

    https://lostechies.com/derekgreer/2008/11/23/model-view-presenter-styles/

    Note: The tutorial has the Presenter in a classlib project. You don't need the classlib project, and just make folder for Presenters and Views and put classes presenters and Interfaces for IViews in their respective folders in the ASP.NET Web form project.

    http://polymorphicpodcast.com/shows/mv-patterns/

    You should learn how to use the DTO pattern. The DTO(s) are kept in a classlib project called Entities, and all projects that need to know about the DTO(s) have project reference to Entities.

    https://www.codeproject.com/articles/1050468/data-transfer-object-design-pattern-in-csharp

    Also, is it better to put my select and insert functions in the DAL? My DAL is the dbml.

    All CRUD operations (create, read, update and delete) need to be in the DAL.

    The example is using MVC where the Model is making calls to the DAL, the DTO(s) are being used to pass data between the layers or tiers., The model is controlling the data for the controls, like the presenter in MVP would be the one doing it.

    Do you need the BLL if all it amounts to is pass-through logic between the UI and the DAL? Maybe, the presenter just calls objects on the DAL. Maybe the supervising controller is controlling that action between the presenter and the DAL. Maybe you don't need a supervising controller.

    Maybe, you need to use the DAO pattern in the DAL. 

    https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm

    The example is using EF instead of Linq-2-SQL, but the concepts are the same.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    using DAL;
    using Entities;
    
    
    namespace MVC.Models
    {
        public class ProjectModels : IProjectModels
        {
            public ProjectViewModels GetProjectsByUserId(string userid)
            {
                var vm = new ProjectViewModels {Projects = new List<ProjectViewModels.Project>()};
                
                var dtos = new DaoProject().GetProjectsByUserId(userid);
    
                vm.Projects.AddRange(dtos.Select(dto => new ProjectViewModels.Project()
                {
                    ProjectId = dto.ProjectId,
                    ClientName = dto.ClientName,
                    ProjectName = dto.ProjectName,
                    Technology = dto.Technology,
                    ProjectType = dto.ProjectType,
                    StartDate = dto.StartDate,
                    EndDate = dto.EndDate,
                    Cost = dto.Cost
                }).ToList());
    
                return vm;
            }
    
            public ProjectViewModels.Project GetProjectById(int id)
            {
                var dto = new DaoProject().GetProjectById(id);
    
                var project = new ProjectViewModels.Project
                { 
                    ProjectId = dto.ProjectId,
                    ClientName = dto.ClientName,
                    ProjectName = dto.ProjectName,
                    Technology = dto.Technology,
                    ProjectType = dto.ProjectType,
                    StartDate = dto.StartDate,
                    EndDate = dto.EndDate,
                    Cost = dto.Cost
                };
       
                return project;
            }
    
            public ProjectViewModels.Project Create()
            {
                var project = new ProjectViewModels.Project();
                return PopulateSelectedList(project);
            }
    
            public void Create(ProjectViewModels.Project project, string userid)
            {
                var dto = new DtoProject
                {
                    ProjectId = project.ProjectId,
                    ClientName = project.ClientName,
                    ProjectName = project.ProjectName,
                    ProjectType = project.ProjectType,  
                    Technology = project.Technology,
                    UserId = userid,
                    StartDate = (DateTime) project.StartDate,
                    EndDate = (DateTime) project.EndDate,
                    Cost = (decimal) project.Cost
                };
    
                new DaoProject().CreateProject(dto);
            }
    
            public ProjectViewModels.Project Edit(int id)
            {
                var dto = new DaoProject().GetProjectById(id);
    
                var project = new ProjectViewModels.Project
                { 
                    ProjectId = dto.ProjectId,
                    ClientName = dto.ClientName,
                    ProjectName = dto.ProjectName,
                    Technology = dto.Technology,
                    ProjectType = dto.ProjectType,
                    StartDate = dto.StartDate,
                    EndDate = dto.EndDate,
                    Cost = dto.Cost
                };
    
                project = PopulateSelectedList(project);
    
                return project;
            }
    
            public void Edit(ProjectViewModels.Project project, string userid)
            {
                var dto = new DtoProject
                {
                    ProjectId = project.ProjectId,
                    ClientName = project.ClientName,
                    ProjectName = project.ProjectName,
                    ProjectType = project.ProjectType,
                    Technology = project.Technology,
                    UserId = userid,
                    StartDate = (DateTime) project.StartDate,
                    EndDate = (DateTime) project.EndDate,
                    Cost = (decimal) project.Cost
                };
    
                new DaoProject().UpdateProject(dto);
            }
    
            public void Delete(int id)
            {
                new DaoProject().DeleteProject(id);
            }
    
            public ProjectViewModels.Project PopulateSelectedList(ProjectViewModels.Project project)
            {
                project.ProjectTypes = new List<SelectListItem>
                {
                    new SelectListItem {Value = "1", Text = "Fixed Price"},
                    new SelectListItem {Value = "2", Text = "Time & Material"}
                };
    
                var selected = (from a in project.ProjectTypes.Where(a => a.Value == project.ProjectType) select a)
                    .SingleOrDefault();
    
                if (selected != null)
                    selected.Selected = true;
    
                return project;
            }
        }
    }
    
    ---------------------------------------------------------------------------
    
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Transactions;
    using Entities;
    using DAL.Model;
    
    namespace DAL
    {
        public class DaoProject : IDaoProject
        {
            public DtoProject GetProjectById(int id)
            {
                var dto = new DtoProject();
    
                using (var context = new ProjectMgmntEntities())
                {
                    var project = (context.Projects.Where(a => a.ProjectId == id)).SingleOrDefault();
    
                    if (project == null) return dto;
                    dto.ProjectId = project.ProjectId;
                    dto.ClientName = project.ClientName;
                    dto.ProjectName = project.ProjectName;
                    dto.Technology = project.Technology;
                    dto.ProjectType = project.ProjectType;
                    dto.UserId = project.UserID;
                    dto.StartDate = project.StartDate;
                    dto.EndDate = project.EndDate;
                    dto.Cost = project.Cost;
                }
    
                return dto;
            }
    
            public List<DtoProject> GetProjectsByUserId(string userid)
            {
                var dtos = new List<DtoProject>();
    
                using (var context = new ProjectMgmntEntities())
                {
    
                    dtos = (from a in context.Projects.Where(a => a.UserID.Contains(userid))
                        select new DtoProject
                        {
                            ProjectId = a.ProjectId,
                            ClientName = a.ClientName,
                            ProjectName = a.ProjectName,
                            Technology = a.Technology,
                            ProjectType = a.ProjectType,
                            UserId = a.UserID,
                            StartDate = a.StartDate,
                            EndDate = a.EndDate,
                            Cost = a.Cost
                        }).ToList();
                }
    
                return dtos;
            }
    
            public void CreateProject(DtoProject dto)
            {
                using (var context = new ProjectMgmntEntities())
                {
                    var project = new Project
                    {
                        ClientName = dto.ClientName,
                        ProjectName = dto.ProjectName,
                        Technology = dto.Technology,
                        ProjectType = dto.ProjectType,
                        UserID = dto.UserId,
                        StartDate = dto.StartDate,
                        EndDate = dto.EndDate,
                        Cost = dto.Cost
                    };
    
                    context.Projects.Add(project);
                    context.SaveChanges();
                }
            }
    
            public void UpdateProject(DtoProject dto)
            {
                var project = new Project();
    
                using (var context = new ProjectMgmntEntities())
                {
                    project = (context.Projects.Where(a => a.ProjectId == dto.ProjectId)).SingleOrDefault();
                }
    
                if (project != null)
                {
                    project.ClientName = dto.ClientName;
                    project.ProjectName = dto.ProjectName;
                    project.Technology = dto.Technology;
                    project.ProjectType = dto.ProjectType;
                    project.UserID = dto.UserId;
                    project.StartDate = dto.StartDate;
                    project.EndDate = dto.EndDate;
                    project.Cost = dto.Cost;
                }
    
                using (var dbcontext = new ProjectMgmntEntities())
                {
                    if (project == null) return;
                    dbcontext.Entry(project).State = EntityState.Modified;
                    dbcontext.SaveChanges();
                }
            }
    
            public void DeleteProject(int id)
            {
                Project project;
    
                using (var context = new ProjectMgmntEntities())
                {
                    project = (context.Projects.Where(a => a.ProjectId == id)).SingleOrDefault();
                }
    
                if (project == null) return;
    
                using (var newContext = new ProjectMgmntEntities())
                {
                    var tasks = new DaoTask().GetTasksByProjectId(project.ProjectId);
                    using (TransactionScope scope = new TransactionScope())
                    {
                        foreach (var task in tasks)
                        {
                            new DaoTask().DeleteTask(task.TaskId);
                        }
    
                        newContext.Entry(project).State = EntityState.Deleted;
                        newContext.SaveChanges();
    
                        scope.Complete();
                    }
                }
            }
        }
    }
    
    

    Monday, May 14, 2018 4:43 PM