LINQ to SQL Profile Provider

Latest post 08-24-2008 9:47 PM by Pat. 0 replies.
  • 08-24-2008 9:47 PM

    • Pat
    • Top 10 Contributor
      Male
    • Joined on 04-27-2008
    • Tempe, AZ
    • Posts 45

    LINQ to SQL Profile Provider

    Here's a ProfileProvider implemantion using LINQ to SQL and the DataContext.

     

    using System;

    using System.Data;

    using System.Configuration;

    using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.HtmlControls;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Xml.Linq;

    using System.Collections.Generic;

    using System.Collections.Specialized;

    using System.Web.Profile;

    using System.Runtime.Serialization.Formatters.Binary;

    using System.Runtime.Serialization;

    using System.Runtime.InteropServices;

    using System.IO;

     

    /// <summary>

    /// Summary description for DOPQProfileProvider

    /// </summary>

    namespace DOPQ.BusinessObjects

    {

        public class DOPQProfileProvider : ProfileProvider

        {

            public DOPQProfileProvider() { }

     

            public string UserName { get; set; }

     

            public override string ApplicationName

            {

                get { return ConfigurationManager.AppSettings["SystemName"]; }

                set { }

            }

     

            public override void Initialize(string name, NameValueCollection config)

            {

                base.Initialize(name, config);

            }

     

            private void GetUsername(SettingsContext context)

            {

                UserName = ((string)context["UserName"]).Remove(0, ((string)context["UserName"]).IndexOf(@"\") + 1);

            }

     

            private DOPQ_System _dopqSystem;

            public DOPQ_System DOPQSystem

            {

                get

                {

                    if (_dopqSystem == null)

                        _dopqSystem = SystemServices.GetSystem(ApplicationName);

                    return _dopqSystem;

                }

            }

     

            public EMS_Employee Employee

            {

                get

                {

                    return EmployeeServices.GetEmployeeByNtlogin(UserName);

                }

            }

     

            public List<DOPQ_Profile> Profiles

            {

                get

                {

                    var profs = from p in DataContextHelper.CurrentContext.DOPQ_Profiles

                                where p.SystemID == DOPQSystem.SystemID &&

                                p.EmployeeID == Employee.EmployeeID

                                select p;

                    return profs.ToList();

                }

            }

     

            public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)

            {

                GetUsername(context);

                SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();

     

                foreach (SettingsProperty property in collection)

                {

                    SettingsPropertyValue propValue = new SettingsPropertyValue(property);

                    List<DOPQ_Profile> profs = (from p in Profiles where p.PropertyName == property.Name select p).ToList();

                    if (profs.Count() < 1)

                    {

                        if (property.PropertyType.IsPrimitive || property.PropertyType == typeof(String))

                        {

                            propValue.PropertyValue = null;

                            propValue.SerializedValue = null;

                            propValue.Deserialized = false;

                        }

                    }

                    else

                    {

                        if (property.SerializeAs == SettingsSerializeAs.String)

                            propValue.SerializedValue = profs.First().StringValue;

     

                        else if (property.SerializeAs == SettingsSerializeAs.Binary)

                            propValue.SerializedValue = profs.First().BinaryValue;// (Byte[])row["BinaryValue"];

                    }

                    propValue.Deserialized = false;

                    values.Add(propValue);

                }

                return values;

            }

     

            public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)

            {

                GetUsername(context);

                UpdatePropertiesWrapper upw = new UpdatePropertiesWrapper(UserName, ApplicationName);

     

                foreach (SettingsPropertyValue PropertyValue in collection)

                {

                    if (PropertyValue.IsDirty)

                    {

                        upw.PropertyNames.Add(PropertyValue.Name);

                        if (PropertyValue.Deserialized && PropertyValue.Property.SerializeAs != SettingsSerializeAs.Binary)

                        {

                            upw.StringValues.Add(PropertyValue.PropertyValue.ToString());

                            upw.BinaryValues.Add(new Byte[0]);

                        }

                        else if (!PropertyValue.Deserialized && PropertyValue.Property.SerializeAs != SettingsSerializeAs.Binary)

                        {

                            upw.StringValues.Add(PropertyValue.PropertyValue.ToString());

                            upw.BinaryValues.Add(new Byte[0]);

                        }

                        else if (PropertyValue.Deserialized && PropertyValue.Property.SerializeAs == SettingsSerializeAs.Binary)

                        {

                            upw.BinaryValues.Add((Byte[])PropertyValue.SerializedValue);

                            upw.StringValues.Add(String.Empty);

                        }

                        else if (!PropertyValue.Deserialized && PropertyValue.Property.SerializeAs == SettingsSerializeAs.Binary)

                        {

                            MemoryStream strm = new System.IO.MemoryStream();

                            BinaryFormatter bin = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                            bin.Serialize(strm, PropertyValue.PropertyValue);

                            Byte[] PropertyData = new Byte[strm.Length];

                            strm.Seek(0, System.IO.SeekOrigin.Begin);

                            strm.Read(PropertyData, 0, PropertyData.Length);

                            upw.BinaryValues.Add(PropertyData);

                            upw.StringValues.Add(String.Empty);

                        }

                    }

                }

                for (int i = 0; i < upw.PropertyNames.Count; i++)

                {

                    List<DOPQ_Profile> profs = (from p in Profiles where p.PropertyName == upw.PropertyNames[i] select p).ToList();

                    if (profs.Count() < 1)

                    {

                        DOPQ_Profile profile = new DOPQ_Profile

                        {

                            SystemID = DOPQSystem.SystemID,

                            EmployeeID = Employee.EmployeeID,

                            PropertyName = upw.PropertyNames[i],

                            StringValue = upw.StringValues[i],

                            BinaryValue = upw.BinaryValues[i],

                            LastUpdated = DateTime.Now

     

                        };

                        DataContextHelper.CurrentContext.DOPQ_Profiles.InsertOnSubmit(profile);

                    }

                    else

                    {

                        profs.First().StringValue = upw.StringValues[i];

                        profs.First().BinaryValue = upw.BinaryValues[i];

                        profs.First().LastUpdated = DateTime.Now;

                    }

     

                }

                DataContextHelper.CurrentContext.SubmitChanges();

     

            }

     

            #region FUNCTIONS NOT IMPLEMENTED

            /*------------------------------------------------------------------------------*/

            /*------------------THE FOLLOWING FUNCTIONS ARE NOT IMPLEMENTED-----------------*/

            /*------------------------------------------------------------------------------*/

            public override int DeleteProfiles(ProfileInfoCollection profiles)

            {

                //This functionality is implemented database side through a DTS

                return 0;

            }

     

            public override int DeleteProfiles(string[] usernames)

            {

                //This functionality is implemented database side through a DTS

                return 0;

            }

     

            public override int DeleteInactiveProfiles(

            ProfileAuthenticationOption authenticationOption,

            DateTime userInactiveSinceDate)

            {

                //This functionality is implemented database side through a DTS

                return 0;

            }

     

            public override ProfileInfoCollection FindProfilesByUserName(

            ProfileAuthenticationOption authenticationOption,

            string usernameToMatch,

            int pageIndex,

            int pageSize,

            out int totalRecords)

            {

                totalRecords = 0;

                return new ProfileInfoCollection();

            }

     

            public override ProfileInfoCollection FindInactiveProfilesByUserName(

            ProfileAuthenticationOption authenticationOption,

            string usernameToMatch,

            DateTime userInactiveSinceDate,

            int pageIndex,

            int pageSize,

            out int totalRecords)

            {

                totalRecords = 0;

                return new ProfileInfoCollection();

            }

     

            public override ProfileInfoCollection GetAllProfiles(

            ProfileAuthenticationOption authenticationOption,

            int pageIndex,

            int pageSize,

            out int totalRecords)

            {

                totalRecords = 0;

                return new ProfileInfoCollection();

            }

     

            public override ProfileInfoCollection GetAllInactiveProfiles(

            ProfileAuthenticationOption authenticationOption,

            DateTime userInactiveSinceDate,

            int pageIndex,

            int pageSize,

            out int totalRecords)

            {

                totalRecords = 0;

                return new ProfileInfoCollection();

            }

     

            public override int GetNumberOfInactiveProfiles(

            ProfileAuthenticationOption authenticationOption,

            DateTime userInactiveSinceDate)

            {

                return 0;

            }

            #endregion

        }

     

        public class UpdatePropertiesWrapper

        {

            public String UserName { get; set; }

            public String ApplicationName { get; set; }

            public List<String> PropertyNames { get; set; }

            public List<String> StringValues { get; set; }

            public List<Byte[]> BinaryValues { get; set; }

     

            public UpdatePropertiesWrapper(String userName, String applicationName)

            {

                UserName = userName;

                ApplicationName = applicationName;

                PropertyNames = new List<String>();

                StringValues = new List<String>();

                BinaryValues = new List<Byte[]>();

            }

        }

     

        public static class ActiveUserCounter

        {

            private static DateTime LastTime = Convert.ToDateTime("1/1/1900");

            private static int UserCount = 0;

     

            public static int GetUserCount(int employeeID)

            {

                if (UserCount == 0

                    || DateTime.Now.AddMinutes(-1) > LastTime)

                {

                    var ps = (from p in DataContextHelper.CurrentContext.DOPQ_Profiles

                              join s in DataContextHelper.CurrentContext.DOPQ_Systems

                              on p.SystemID equals s.SystemID

                              where s.SystemName == ConfigurationManager.AppSettings["SystemName"]

                              && p.EmployeeID != employeeID

                              && p.LastUpdated > DateTime.Now.AddMinutes(-10)

                              select p.EmployeeID).Distinct();

                    UserCount = ps.Count() + 1;

                    LastTime = DateTime.Now;

                }

                return UserCount;

            }

        }

     

    }

     

     

    Patrick McNamara, BS-IS/CS, MBA, MAED
    ASP.NET Web Application Developer
    Asteryx, LLC.
    http://asteryx.com
    pat@asteryx.com

Page 1 of 1 (1 items) | RSS
Forums to discuss Microsoft ASP.Net Development and SQL
Powered by Community Server (Non-Commercial Edition), by Telligent Systems