Мое требование - создать среду, которая может использовать стандартный поставщик профилей ASP.NET.Приложение содержит две библиотеки: одну для доступа к профилю (сохранение и извлечение профиля с использованием класса ProfileBase) - библиотеку классов инфраструктуры, а другую - для определения свойств клиентской библиотеки классов Profile - Developer, которая использует вышеуказанную библиотеку классов инфраструктуры.
Идея в том, что разработчикам не нужно знать о реализации базового профиля (в библиотеке классов инфраструктуры), и все, что им нужно сделать, - это предоставить свойства в классе, чтобы профиль мог бытьустановить и получить, как ожидалось.
Моя реализация ниже.(Обратите внимание, что я успешно настроил аутентификацию, строки подключения и поставщиков ролей)
Web.config ->
<profile inherits="MyCompany.FrameworkLib.ProfileSettingsService, MyCompany.FrameworkLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=12ac5ebb7ed144" >
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
*Our design is not to specify profile properties in the web.config.
Реализация MyCompany.FrameworkLib.ProfileSettingsService ->
public class ProfileSettingsService : ProfileBase, IProfileSettingsService
{
"**Note that if I un-comment the below code Profile works as expected. I do not want this property to be here, but somehow discover it dynamically based on the values being passed to this class. How can I do this?.**"
//[SettingsAllowAnonymous(false)]
//public string HideTransactionButton
//{
// get { return base["HideTransactionButton"] as string; }
// set { base["HideTransactionButton"] = value; }
//}
#region IProfileSettingsService Members
public T Get<T>(string key, T defaultValue)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
object profileValue = null;
try
{
profileValue = GetUserProfile().GetPropertyValue(key);
}
catch { }
if (profileValue is T)
{
return (T)profileValue;
}
return defaultValue;
}
public void Set<T>(string key, T value)
{
GetUserProfile().SetPropertyValue(key, value);
GetUserProfile().Save();
}
public ProfileSettingsService GetUserProfile(string username)
{
return Create(username) as ProfileSettingsService;
}
public ProfileSettingsService GetUserProfile()
{
var userName = HttpContext.User.Identity.Name;
if (userName != null)
{
return Create(userName) as ProfileSettingsService;
}
return null;
}
#endregion
}
реализация MyCompany.ConsumeLib.ProfileContext ->
public class ProfileContext : IProfileContext
{
#region IProfileContext Members
[Dependency] //Note that I use Unity for DI
public IProfileSettingsService ProfileSettingsService { get; set; }
public string HideTransactionButton
{
get { return this.ProfileSettingsService.Get<string>("HideTransactionButton", "false"); }
set { this.ProfileSettingsService.Set("HideTransactionButton", value); }
}
#endregion
}
Так что вопрос в том, как заставить работать профиль без необходимости раскомментировать
//[SettingsAllowAnonymous(false)]
//public string HideTransactionButton
//{
// get { return base["HideTransactionButton"] as string; }
// set { base["HideTransactionButton"] = value; }
//}
в MyCompany.FrameworkLib.ProfileSettingsService
Мне нужно иметь возможность динамически обнаруживать свойства в ProfileSettingsService без необходимости явного указания свойств.Таким образом, разработчику не нужно беспокоиться о сохранении свойств в двух библиотеках - (одна в frameworkLib, а другая в ConsumeLib.)
Любые идеи приветствуются.