Dynamic Profile ASP.NET – Update

I received a request about the uses of dynamic keyword in profile declaration and in fact it was valid that while declaring the profile object we should be using concrete profile object and not declaring it as dynamic and rather its properties should be dynamic. I updated the current code by adding a new class ProfileProperties to store all the properties for Profile object.

        /// <summary>
        /// Properties
        /// Dynamic Properies object to hold Profile properties
        /// </summary>
        public dynamic Properties { get; private set; }

Now Profile class’s Init method looks like below.

        /// <summary>
        /// Init
        /// </summary>
        private void Init()
        {
            if (ProfileService != null)
            {
                var _members = ProfileService.GetPropertyValues(this.UserName);
                Properties = new ProfileProperties(_members);
            }
        }

Now it is not required to have the Profile object declared as dynamic rather we can declare it as concrete Profile class.

        public ActionResult Index()
        {
            Profile profile = new Profile(new ProfileService());

            var firstName = profile.Properties.FirstName;
            var lastName = profile.Properties.LastName;
            var name = profile.Properties.Name;
            if (string.IsNullOrWhiteSpace(name))
                profile.Properties.Name = string.Format("Hello Mr.!!! {0} {1}", firstName, lastName);

            var address = profile.Properties.Address;
            if (address == null)
                address = new Address { Street = "Jefferson", Unit = "Line1", City = "Louisville", State = "KY", ZipCode = "40220" };
            profile.Properties.Address = address;
            profile.Save();
            
            return View(profile);
        }

Updated code is available at GitHub

Advertisement

About cprakash

A developer, passionate about .Net, Architecture and Security.
This entry was posted in MVC, Profile. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s