DCSIMG
December 2009 - Posts - Ran Wahle's blog

Ran Wahle's blog

December 2009 - Posts

Code snippet for control wrapping properties

Code snippet for control wrapping properties

Hello everyone.

many of us,  I’m sure, needed to wrap inner controls of a user\custom control 
with public properties. That’s in order to have their control encapsulated,
and to add some code flexibility (If the control is changing, all I need to do is
change the code of my property and the rest remains intact.

Therefore I’ve made a code snippet for myself that allow me to write these kind
of properties easily, and figured why not let everyone use it.

I hope you’ll find it useful.

 

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Control Property</Title>
            <Shortcut>propctrl</Shortcut>
            <Description>Code snippet for property with get and set accessors 
      that wraps an inner control</Description>
            <Author>Ran Wahle</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>string</Default>
                </Literal>
                <Literal>
                    <ID>name</ID>
                    <Default>PropName</Default>
          <ToolTip>Property name</ToolTip>
                </Literal>
        <Literal>
          <ID>controlName</ID>
          <Default>ctrlName</Default>
          <ToolTip>Control Name</ToolTip>
        </Literal>
        <Literal>
          <ID>controlInnerProperty</ID>
          <Default>Text</Default>
          <ToolTip>Control Inner property</ToolTip>
        </Literal>
            </Declarations>
            <Code Language="csharp">
        <![CDATA[
        /// <summary>
        /// Put summary for $name$
        /// </summary>
        public $type$ $name$
        {
            get
            {
                return $controlName$.$controlInnerProperty$;
            }
            set
            {
                $controlName$.$controlInnerProperty$ = value;
            }
        }
]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
kick it on DotNetKicks.com
Posted: Dec 29 2009, 03:46 PM by Ran Wahle | with 1 comment(s) |
תגים:, ,

Session manager


This post comes as an extension to a post by Shlomo Goldberg (in Hebrew) that talks
about how to work with session variable in a safer way than plain strings.

Shlomo Goldberg has suggested that instead of strings we should work with enum entries.
This way is more efficient for developers who can simply find all the uses of a certain  session
variable and can find all reference to it.

As you can see from the code attached to that post, it uses indexer with SessionParam
enum using a private method called RetrieveFromSession, which calls it’s overloading
method that accepts the good old string parameter (using the enum.ToString method).

This one, of course holds some performance penalty because it’s use of the ToString method
call for enum. Here’s another approach:

As an extension for this suggestion one might suggest the use of properties that envelopes
the session variable within.

All properties are in a class (The same as in the class suggested by Shlomo Goldberg, only doesn't have to be singleton)

A property of such can look like that:

   1:    public UserInfo DelegatingUser
   2:          {
   3:              get
   4:              {
   5:                  return Session != null ? (UserInfo)Session[DELEGATING_USER] : null;
   6:              }
   7:              set
   8:              {
   9:                  if (Session != null)
  10:                  {
  11:                      Session[DELEGATING_USER] = value;
  12:                  }
  13:              }
  14:          }
 Where Session is also a property envelopes HttpContext.Current.Session 
and the DELEGATING_USER is a string constant

 

What do we gain from it?

1. Type safety. Use of this kind of properties will prevent override one type of an object by
   another without the compiler  will notify.

2. We don’t have to use the enum.ToString anymore and therefore gain our performance
    back almost fully.

3. We can add logic to the properties.

There are two (or more) downsides however:

1. This class may be coupled to the project’s data model, you cannot simply put it on another
    project because it won’t compile.

2. You cannot add session variables through this class form another files. Adding new session
    variables requires adding new properties to the class.

 

You can download the class in here. (Note – it won’t compile because it is coupled to
my project’s data model)

kick it on DotNetKicks.com
Posted: Dec 20 2009, 09:51 PM by Ran Wahle | with 3 comment(s) |
תגים:, ,