SqlTableProfileProvider,定制ProfileBase类,添加额外的属性特性 [英] SqlTableProfileProvider, custom ProfileBase class, add extra attributes to properties

查看:90
本文介绍了SqlTableProfileProvider,定制ProfileBase类,添加额外的属性特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SqlTableProfileProvider作为我的个人资料提供者,并呼吁的ProfileCommon从System.Web.Profile.ProfileBase继承的自定义类。这再次present列在我的个人资料表我ProfileCommon类的属性分别获得的属性[CustomProviderData(COLUMNNAME; DBTYPE)。我想一个新的属性添加到特定列,然后我要拉从SqlTableProfileProvider类的信息。

团队和我正在寻找一个外键(表)与我们的个人资料表进行关联。目前,我们的个人资料表店,基本上,键值对,名字,姓氏,年龄等;然而,我们正​​计划书签,链接存储最喜欢的文章,什么不是,那将是我们的仪表盘页面上的列表psented $ P $。我们喜欢用SqlTableProfileProvider和我创造了的ProfileCommon对象。我们所有的asp.net页面从的BasePage继承和一个名为Profile属性获取配置文件通用对象。

这将是很好,只是能够做到:

  Profile.Bookmarks.Count; //知道是否还有书签//也只能够通过他们的foreach
的foreach(在Profile.Bookmarks收藏BK){...}

例如:

 公共类的ProfileCommon:System.Web.Profile.ProfileBase
{
    公共静态的ProfileCommon GetProfile(){....}    [CustomProviderData(名字; VARCHAR)]
    公共虚拟字符串名字
    {
        得到
        {
            回报((字符串)(this.GetPropertyValue(名字)));
        }
        组
        {
            this.SetPropertyValue(名字,值);
        }
    }    [CustomProviderData(姓氏; VARCHAR)]
    公共虚拟字符串姓氏
    {
        得到
        {
            回报((字符串)(this.GetPropertyValue(名字)));
        }
        组
        {
            this.SetPropertyValue(姓氏,值);
        }
    }    [CustomProviderData(OtherColumn; INT)]
    [TableNameData(OtherTable)]
    公共虚拟INT OtherColumn
    {
        得到...
        设置...
    }
}//我的新属性
[AttributeUsage(AttributeTargets.Property)
公共类TableNameData:属性
{
    私人字符串_tableName;    公共TableNameData(字符串tableName值)
    {
        _tableName = tableName值;
    }    公共字符串表名
    {
        得到
        {
            返回_tableName;
        }
    }
}//不是我实现,但寻求增强它。
公共类SqlTableProfileProvider:ProfileProvider
{
    公众覆盖SettingsPropertyValueCollection GetPropertyValues​​(SettingsContext背景下,SettingsPropertyValueCollection SVC,用户名字符串,SqlConnection的康涅狄格州)
    {
        ...
        的foreach(在性能SettingsProperty道具)
        {
            ...
            //这里,获取CustomProviderData
            字符串persistenceData = prop.Attributes [CustomProviderData]作为字符串。            //我如何得到我的吗?
        }
    }
}

该SqlTableProfileProvider由郝酷嗯实施。它继承ProfileProvider。

之一GetPropertyValues​​返回SettingsPropertyValueCollection的方法。有一个叫GetProfileDataFromTable私有方法。在那里,我希望访问,我建立了我的自定义属性。

问:我如何访问我的属性,我对我的财产指定

更新:07162011:1517,后7天问问题,

我没有找到一个方法来做到这一点。下面是我做的:

  //在默认的构造函数添加以下
公众的ProfileCommon()
{
    //获取所有属性此类'的ProfileCommon
    的PropertyInfo [] = propertyInfos typeof运算(的ProfileCommon).GetProperties();    //该ProfileBase,基类,有一​​个属性称为属性和
    //可以得到该财产的所有属性,但只有
    //这ProfileBase查找的一些属性。如果显影剂的愿望
    //自定义使用属性上的属性,它不会出现在
    // ProfileCommon.Properties.Attributes列表。
    //
    //那么,什么是我们该怎么办,好了,我们要拿出一个黑客和解决这个问题
    //
    的foreach(在ProfileCommon.Properties SettingsProperty settingsProperty)
    {
        的foreach(的PropertyInfo的PropertyInfo在propertyInfos)
        {
            如果(settingsProperty.Name == propertyInfo.Name)
            {
                //从关联的属性的所有属性,但我们是从的PropertyInfo变量得到它
                //这是通过反射检索,并列出所有属性。
                    [对象]属性= propertyInfo.GetCustomAttributes(假);                的for(int i = 0; I< attributes.Count();我++)
                {
                    类型type =属性[I] .GetType();                    的PropertyInfo [] = attributeClassProperities type.GetProperties();                    的foreach(的PropertyInfo attributeClassProperty在attributeClassProperities)
                    {
                        //在typeid的财产不intested的对象
                        如果(!attributeClassProperty.Name.Equals(TYPEID))
                        {
                            //如果settingsProperty.Attributes不包含我们的键值对,然后将其添加。
                            如果(settingsProperty.Attributes [attributeClassProperty.Name] == NULL)
                            {
                                    settingsProperty.Attributes.Add(attributeClassProperty.Name,属性[I]);
                            }
                        }
                    }
                }
            }
        }
    }
}


解决方案

您只需要为你必须要实施您的ProfileCommon照顾的例子,并确保属性是正确的。

有关专业的配置文件属性收藏你也写自己的个人资料类,就像你在这个例子。在那里,你必须执行,将在数据库中查询该用户的书签的数量Count属性。

在数据库中的个人资料表,你将有一个合适的FK类型,如的Guid,智力或BigInt有这将是没有什么比一个外键到另一个表你能说出[DBO]更多的是书签栏。[书签]这实际上将包含每个用户的书签。此书签表应该有一个像用户ID列,这将是一个FK到ASP.NET用户标识。

I am using SqlTableProfileProvider as my profile provider and a custom class called 'ProfileCommon' inheriting from System.Web.Profile.ProfileBase. My ProfileCommon class properties that represent the columns in my profile table each get an attribute of [CustomProviderData("columnName;dbType")]. I am trying to add a new attribute to specific column and then am going to pull that info from the SqlTableProfileProvider class.

The team and I are looking to associate a foreign key (table) with our Profile table. Right now, our Profile table stores, basically, key value pairs, FirstName, LastName, Age, etc; however, we are planning to store bookmarks, links to favorite articles and what not, that will be presented in a list on our dashboard page. We like using the SqlTableProfileProvider and the ProfileCommon object I created. All our asp.net pages inherit from a BasePage and a property called Profile gets the profile common object.

It would be nice to just be able to do:

Profile.Bookmarks.Count; // to know if there are bookmarks

// to also just be able to foreach through them
foreach (Bookmark bk in Profile.Bookmarks) { ... }

Ex:

public class ProfileCommon : System.Web.Profile.ProfileBase
{
    public static ProfileCommon GetProfile() { .... }

    [CustomProviderData("FirstName;varchar")]
    public virtual string FirstName
    {
        get
        {
            return ((string)(this.GetPropertyValue("FirstName")));
        }
        set
        {
            this.SetPropertyValue("FirstName", value);
        }
    }

    [CustomProviderData("LastName;varchar")]
    public virtual string LastName
    {
        get
        {
            return ((string)(this.GetPropertyValue("LastName")));
        }
        set
        {
            this.SetPropertyValue("LastName", value);
        }
    }

    [CustomProviderData("OtherColumn;int")]
    [TableNameData("OtherTable")]
    public virtual int OtherColumn
    {
        get ...
        set ...
    }
}

// My new attribute
[AttributeUsage(AttributeTargets.Property)]
public class TableNameData : Attribute
{
    private string _tableName;

    public TableNameData(string tableName)
    {
        _tableName = tableName;
    }

    public string TableName
    {
        get
        {
            return _tableName;
        }
    }
}

// Not my implementation, but looking to enhance it.
public class SqlTableProfileProvider : ProfileProvider
{
    public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyValueCollection svc, string username, SqlConnection conn)
    {
        ...
        foreach (SettingsProperty prop in properties)
        {
            ...
            // in here, gets CustomProviderData
            string persistenceData = prop.Attributes["CustomProviderData"] as string.

            // how do i get to mine?
        }
    }
}

The SqlTableProfileProvider was implemented by Hao Kung. It inherits from ProfileProvider.

One of the methods GetPropertyValues returns a SettingsPropertyValueCollection. There is a private method called GetProfileDataFromTable. In there, I wish to access my custom attribute that I created.

Question: How do I access my attribute that I have specified on my property?

UPDATE: 07162011:1517, 7 days after question asked,

I did find a way to do this. The following is how I did it:

// In the default constructor add the following
public ProfileCommon()
{
    //  Get all properties for this class 'ProfileCommon'
    PropertyInfo[] propertyInfos = typeof(ProfileCommon).GetProperties();

    //  The ProfileBase, base class, has a property called Properties and 
    //  one can get to all attributes on that property but there are only
    //  a few attributes that ProfileBase looks for. If the developer wishes
    //  to use custom attributes on a property, it wont appear in the 
    //  ProfileCommon.Properties.Attributes list.
    //  
    //  So, what are we going to do, well, we are going to come up with a hack and solution to this problem
    //
    foreach (SettingsProperty settingsProperty in ProfileCommon.Properties)
    {
        foreach (PropertyInfo propertyInfo in propertyInfos)
        {
            if (settingsProperty.Name == propertyInfo.Name)
            {
                //  get all attributes from the associated property, but we are getting it from the propertyInfo variable
                //  which was retrieved through reflection and will list ALL attributes.
                    object[] attributes = propertyInfo.GetCustomAttributes(false);

                for (int i = 0; i < attributes.Count(); i++)
                {
                    Type type = attributes[i].GetType();

                    PropertyInfo[] attributeClassProperities = type.GetProperties();

                    foreach (PropertyInfo attributeClassProperty in attributeClassProperities)
                    {
                        //  not intested in the TypeId property for the object
                        if (!attributeClassProperty.Name.Equals("TypeId"))
                        {
                            //  if the settingsProperty.Attributes does not contain our key value pair, then add it.
                            if (settingsProperty.Attributes[attributeClassProperty.Name] == null)
                            {
                                    settingsProperty.Attributes.Add(attributeClassProperty.Name, attributes[i]);
                            }
                        }
                    }
                }
            }
        }
    }
}

解决方案

You only need to take care of implementing your ProfileCommon as you have in the example and make sure the attributes are correct.

For the specialized profile property Bookmark you would also write your own profile class just as you have in the example. There you have to implement a Count property that would query the database for the number of bookmarks of that user.

In the Profile table in the database you would have a Bookmarks column with a suitable FK type such as Guid, Int or BigInt that would be nothing more than a Foreign key to another table you could name [dbo].[Bookmarks] that would actually contain the bookmarks for each user. This Bookmarks table should have a column like UserId that would be a FK to the ASP.NET UserId.

这篇关于SqlTableProfileProvider,定制ProfileBase类,添加额外的属性特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆