Un DataSource per il Profile

Marco Leoncini

di , in asp.net,

se come me diventare sempre pià prigri e anche un FindControl diventa faticoso, apprezzere sicuramente i vari xxxDataSource.

ma quando avete a che fare con il Profile?

bhe realizziamone uno ad hoc, così non ci perdiamo nessun automatismo smile_teeth

using System;
using System.Web;
using System.Collections.ObjectModel;
using System.Web.UI;
using System.Web.Profile;
using System.Collections;
namespace MyCustomControl
{
    public class ProfileDataSourceControl : DataSourceControl
    {
        protected override DataSourceView GetView(string viewName)
        {
            return new ProfileView(this, viewName);
        }
    }
    public class ProfileView : DataSourceView
    {
        private ProfileBase _profile = null;
        private HttpContext _context = null;
        public ProfileView(IDataSource owner, string viewName): base(owner, viewName)
        {
            _context = HttpContext.Current;
            if (_context == null)
            {
                throw new ApplicationException("Impossibile ricavare il contesto della chiamata");
            }
            _profile = _context.Profile;
        }
        protected override System.Collections.IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            return new Collection<profilebase />() { _profile };
        }
        protected override int ExecuteInsert(IDictionary values)
        {
            _profile = ProfileBase.Create(_context.User.Identity.Name, _context.User.Identity.IsAuthenticated);
            foreach (DictionaryEntry item in values)
            {
                _profile[item.Key.ToString()] = item.Value;
            }
            _profile.Save();
            return 0;
        }
        protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
        {
            foreach (DictionaryEntry item in values)
            {
                _profile[item.Key.ToString()] = values[item.Key.ToString()];
            }
            _profile.Save();
            return 0;
        }
        protected override int ExecuteDelete(IDictionary keys, IDictionary oldValues)
        {
            foreach (DictionaryEntry item in oldValues)
            {
                _profile[item.Key.ToString()] = null;
            }
            _profile.Save();
            return 0;
        }
    }
}
il codice non è completo, ad esempio non prendo mai in cosiderazione mai i vecchi valori e le chiavi, ma per farlo ci vuole davvero poco

nel lab trovare altri xxxDataSource creati da Cristian

Commenti
Un DataSource per il Profile

Per inserire un commento, devi avere un account.

Fai il login e torna a questa pagina, oppure registrati alla nostra community.

Nella stessa categoria
I più letti del mese