Interfaces and more

 

Summary:

If you want common behavior, you need an interface. And from the first one is a small step to re-organizing the program.

Body:

When you make a component for other people, you must make the possibility for those to

1. customize your software with their software

2. provide a default implementation

For the messaging component, the first customization is to provide a way for the site using the messaging to use their users.

The first step is to create a custom project which can provide guidance to the users that want to implement. VS provides XML documentation –I have checked and put also “warnings as error”

clip_image002

Next , I have create the User interface:

/// <summary>
    /// the user that can send messages
    /// TODO version 2: make pagination
    /// </summary>
    /// <typeparam name="UserPrimaryKey">the type of primary key</typeparam>
    public interface IUser<UserPrimaryKey>
    {
        /// <summary>
        /// the user primary key
        /// </summary>
        UserPrimaryKey Key { get; set; }
        /// <summary>
        /// the user name to be displayed
        /// </summary>
        string UserNameToDisplay { get; set; }
        /// <summary>
        /// other info for the user
        /// </summary>
        string OtherInfo { get; set; }

        /// <summary>
        /// find friends
        /// </summary>
        /// <param name="search">find user by name</param>
        /// <returns></returns>
        IEnumerable<KVPNew<UserPrimaryKey, string>> FindFriends(string search);

        /// <summary>
        /// find friends online
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        IEnumerable<KVPNew<UserPrimaryKey, string>> FindFriendsOnline(string search);

        /// <summary>
        /// sends a message 
        /// </summary>
        /// <param name="message"></param>
        void SendMessage(IUserMessage<IUser<UserPrimaryKey>, UserPrimaryKey> message);

    }
 

Two methods are interesting : SendMessage and FindFriends

Let’s take first SendMessage. It was obviously clear that I need another interface – the message to be sent. So the next interface created:

/// <summary>
    /// the message to be sent to the user
    /// TODO version 2: allow messaging for more than 1 user
    /// TODO version 2: message importance
    /// </summary>
    /// <typeparam name="TheUser">user interface</typeparam>
    ///  /// <typeparam name="UserPrimaryKey">user primary key</typeparam>
    public interface IUserMessage<TheUser, UserPrimaryKey>
        where TheUser : IUser<UserPrimaryKey>
    {
        /// <summary>
        /// subject of the message
        /// </summary>
        string Subject { get; set; }
        /// <summary>
        /// body of the message
        /// </summary>
        string Body { get; set; }
        /// <summary>
        /// to the user
        /// </summary>
        TheUser To { get; set; }
        /// <summary>
        /// from the user
        /// </summary>
        TheUser From { get; set; }
        /// <summary>
        /// cc - as in email
        /// </summary>
        TheUser CC { get; set; }

        /// <summary>
        /// date of message
        /// </summary>
        DateTime DateInserted { get; set; }

        /// <summary>
        /// if recipient have read
        /// </summary>
        bool MessageRead { get; set; }
        // <summary>
        // bcc - as in email
        // </summary>
        //TheUser BCC { get; set; }



    }

Let’s take the other : IEnumerable<KVPNew<UserPrimaryKey, string>> FindFriends(string search);

It is clear that a user can send emails to friends( maybe to anybody if we make an intranet site for an enterprise) and I must somehow have the names and id’s of the friends. I could use KeyValuePair – but it is a struct and can not be compared with null . More, usually I need more data to be transferred – so I have created long ago a KVPNew class:

 /// <summary>
    /// this is the correspondent class of KeyValuePair structure from .net
    /// The features:
    /// 1. it is a class-  can be compared fastly with null
    /// 2. can be used in a search and display <paramref name="AdditionalData">AdditionalData </paramref>
    /// </summary>
    /// <typeparam name="TKEY">Key - it is compared in equals and GetHashCode</typeparam>
    /// <typeparam name="TValue">Value to be displayed</typeparam>
    public class KVPNew<TKEY, TValue>
    {
        public KVPNew() { }
        public KVPNew(TKEY key, TValue value):this()
        {
            this.Key = key;
            this.Value = value;

        }
        public TKEY Key { get; set; }
        public TValue Value { get; set; }
        public string AdditionalData { get; set; }
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            var o = obj as KVPNew<TKEY, TValue>;
            if (o == null)
                return false;
            return this.Key.Equals(o.Key);

        }
        public override int GetHashCode()
        {
            return this.Key.GetHashCode();
        }
    }

And, being to interfaces, I have created also an interface for admin people to find users

/// <summary>

/// used by application to load plugins to find users

/// because each application can have it’s own way to find users

/// <summary>
    /// used by application to load plugins to find users
    /// because each application can have it's own way to find users
    /// used by admins to find users
    /// Improvement version 2 : pagination 
    /// </summary>
    /// <typeparam name="T">user </typeparam>
    /// <typeparam name="UserKey">user key </typeparam>
    public interface IUsersFind<T,UserKey>
        where T:IUser<UserKey>
    {
        /// <summary>
        /// used to find users online to send message
        /// </summary>
        /// <param name="UserThatMakesTheSearch"> the user that makes the search </param>
        /// <param name="Search">search string - could be null</param>
        /// <returns> a list of users</returns>
        IEnumerable<KVPNew<T, string>> FindUsersOnline(T UserThatMakesTheSearch, string Search);
        /// <summary>
        /// used to find users (online or not )to send message
        /// </summary>
        /// <param name="UserThatMakesTheSearch"> the user that makes the search </param>
        /// <param name="Search">search string - could be null</param>
        /// <returns> a list of users</returns>
        IEnumerable<KVPNew<T, string>> FindUsers(T UserThatMakesTheSearch, string Search);
        /// <summary>
        /// find a user by his key
        /// </summary>
        /// <param name="key">the user key</param>
        /// <returns></returns>
        IUser<UserKey> FindUser(UserKey key);
            
    }


/// <summary>
    /// operations with the database
    /// </summary>
    /// <typeparam name="User"></typeparam>
    /// <typeparam name="UserPrimaryKey"></typeparam>
    public interface IUserList<User, UserPrimaryKey>: IDisposable
        where User : IUser<UserPrimaryKey>
    {
        /// <summary>
        /// fast delete all from database - good for testing
        /// </summary>
        void FastDeleteAll();
        
        /// <summary>
        /// add to internal list
        /// </summary>
        /// <param name="u">the user to be added </param>
        void Add(User u);
         
        /// <summary>
        /// save changes for new and old members
        /// </summary>
        void SaveNew();
        /// <summary>
        /// save changes for old members
        /// </summary>
        void SaveExisting();

        /// <summary>
        /// for retrieving 
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        User Find(int i);
        
    }

The final interfaces are in this picture:

clip_image004

Next time we will create the database EF4.1 files from DatabaseFirst and we will modify .tt templates to behave nicely with relationships.

Homework:

  • Add to IUserMessage a BCC field
  • What if To from IUserMessage will be directed to more than one person ?How do you implement it?
  • Do you think that is something missed from KVPNew class?( Hint == operator and GetHashCode)

friday links 1

  1. Make Your Own Windows 8 Tablet with Off-the-Shelf Gear
  2. Lifehacker, tips and downloads for getting things done
  3. Cool Websites, Software and Internet Tips
  4. Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10): The Official Microsoft ASP.NET Site
  5. Thirty-Six Stratagems – Wikipedia, the free encyclopedia
  6. ASP.NET MVC 4 Mobile Features: The Official Microsoft ASP.NET Site
  7. free – List of freely available programming books – Stack Overflow
  8. Managing Transaction Logs – SQLServerCentral
  9. Workflow icons | Icon Search Engine | 1
  10. A Coder Interview With John Simmons – CodeProject
  11. Custom SSMS Shortcuts for ETL Developer. Part 1: SELECT in a Keystroke – SQLServerCentral
  12. Taskos To Do List | Task List – Android Market
  13. Shazam – Android Market
  14. Tech HQ » Blog Archive » Top 25 Must Have Android Apps
  15. xkcd: Random Number
  16. ASP.NET MVC 4: The Official Microsoft ASP.NET Site
  17. The Weekly Source Code 37 – Geolocation/Geotargeting (Reverse IP Address Lookup) in ASP.NET MVC made easy – Scott Hanselman
  18. A tale of two viewports — part two
  19. SQLServerCentral
  20. Using the HTML5 and jQuery UI Popup Calendar with ASP.NET MVC – Ricka on MVC and related Web Technologies – Site Home – MSDN Blogs
  21. Download Details – Microsoft Download Center – ASP.NET MVC 4 Developer Preview
  22. A Faster Get-Content Cmdlet — Microsoft Certified Professional Magazine Online
  23. YouTrack :: Download
  24. Continuous Testing: Think Different — Visual Studio Magazine
  25. Advanced Task Parallel Library Continuations – CodeGuru
  26. Extending the ASP.NET error page (show me the SQL edition)
  27. A Coder Interview With Marc Clifton – CodeProject
  28. Organizational Charts | Bonkers World
  29. Using the HTML5 and jQuery UI Datepicker Popup Calendar with ASP.NET MVC – Part 1: The Official Microsoft ASP.NET Site
  30. Programming Isn't Fun Any More
  31. Using Powershell to run Excel macro on all files of sharepoint document library – Powershell.com – Powershell Scripts, Tips and Resources
  32. Best Free Computer Chess
  33. Lifehacker Pack for Android: Our List of the Best Android Apps
  34. Superior Alternatives to Crappy Windows Software
  35. Ten Things Everyone Should Know About Time | Cosmic Variance | Discover Magazine
  36. Become a Good Programmer in Six Really Hard Steps – GameDev.net
  37. Internet Security: Top Ten Most Influential Programming Books of All Time
  38. The .Net community and the negative view on assembly references | Dru Sellers
  39. Why CRUD might be what they want, but may not be what they need | Ian Cooper
  40. 10 Papers Every Programmer Should Read (At Least Twice)
  41. HTML5 snippet : CSS3 transition text glow on mouse over
  42. HOW TO: Spruce Up a Boring Resume [INFOGRAPHIC]
  43. Official Google Webmaster Central Blog: Introducing Rich Snippets
  44. Thing – schema.org
  45. schema.org – Getting Started
  46. Microformat – Wikipedia, the free encyclopedia
  47. NuGet Package Explorer extension: open dlls with ILSpy
  48. Introduction To jQuery Mobile
  49. The Importance (and Ease) of Minifying your CSS and JavaScript and Optimizing PNGs for your Blog or Website – Scott Hanselman
  50. Scalable Datastores
  51. Using the .NET Stopwatch class to Profile Your Code | C# Help
  52. C#er : IMage: Properties or Fields?
  53. A Brief Introduction of Fundamental OOP
  54. Extending a C# Application Through a Scripted DLR Language — Visual Studio Magazine
  55. Introduction to Databases
  56. Script Junkie | CSS3 Animation With jQuery Fallbacks
  57. A Coder Interview With Sacha Barber – CodeProject
  58. Class Member Encapsulation in JavaScript: Advanced Data Hiding Techniques
  59. A Calculation Engine for .NET – CodeProject
  60. IBM unveils chips that mimic the human brain – 18 Aug 2011 – Computing News
  61. Linus Torvalds’s Lessons on Software Development M… – Input Output
  62. Palantir Technologies » Blog Archive » How to Rock an Algorithms Interview
  63. Common Lisp: A Gentle Introduction to Symbolic Computation
  64. repl.it
  65. Raw Materials – Mediocrity Defended – SQLServerCentral
  66. PDF reporting using ASP.NET MVC3 – CodeProject
  67. SourceForge.net: License – ikvm
  68. Serializing models for RouteValueDictionary and later model binding | mhinze.com
  69. A new breed of magic strings in ASP.NET MVC | Jimmy Bogard's Blog
  70. Simple reads with Entity Framework | mhinze.com
  71. Building forms for deep View Model graphs in ASP.NET MVC | Jimmy Bogard's Blog
  72. Simple ASP.NET MVC object serializer « …meie igapäevast IT’d anna meile igapäev…
  73. Marc Andreessen on Why Software Is Eating the World – WSJ.com
  74. .NET DEVELOPER, BUCURESTI, Software / Calculatoare, Management, Programare, Audit / Consultanta – BestJobs
  75. How To Make An eBook – Smashing Magazine
  76. calibre – About
  77. Frequently Asked Questions — calibre User Manual
  78. Harta Romania | Harta Judetelor Romania | Orase din Romania
  79. Collectible Assemblies for Dynamic Type Generation
  80. Using 51Degrees.Mobi Foundation for accurate mobile browser detection on ASP.NET MVC 3 – Steve Sanderson’s blog – As seen on YouTube™
  81. Stairway to SQL Server Indexes: Level 9, Reading Query Plans – SQLServerCentral
  82. Brain, Bytes, Back, Buns – The Programmer's Priorities – Scott Hanselman
  83. Everything SQL Server Compact: SQL Compact 3rd party tools
  84. Entity Framework 4.1: Inheritance (7) « Vincent-Philippe Lauzon's blog
  85. The DslTextTransform Command
  86. Oleg Sych – » .Config File Transformation
  87. Walkthrough: Debugging a Text Template that Accesses a Model
  88. How to authenticate against the Active Directory by using Forms authentication and Visual C# .NET
  89. Building Secure ASP.NET Applications: Authentication, Authorization, and Secure Communication

Simple messaging system -database configuration

Contents
We will create the database tables and user to access the database from the application
You will find at bottom the

  1. scripts for creating database
  2. materials to read further
  3. homework for you

As an older database programmer, I have started with database configuration.

The selected database engine is Sql Server 2008 R2 Express – the free version that can be downloaded from here : http://www.microsoft.com/express
To create tables , just start Sql Server Management Studio, point to your database, right click “Tables” node and press “New Table”
I have created the following tables in the diagram:

image

Several comments:

  1. The smsg_User table can ( and will !) be replaced by the owner of the site with his table of users . More, this will be configurable by the owner. That’s why I choose IDUser being varchar(150) – maybe the user id will be GUID ?
  2. The messages are stored in the smsg_Message
  3. The message archive table(smsg_Message_Archive) is simply to make the search faster on smsg_Message table. We will put here the messages older than (1 month? 2 months?)
  4. The smsg_MessageThread contains the possibility for the user to reply more than one time to a message.
  5. IDMessage is bigint . We can also put guid to have unlimited number of messages -but 9,223,372,036,854,775,807 messages ( * 2 -we will see it in action) will be enough for a small site.
  6. You can download the script from here:
    http://msprogrammer.serviciipeweb.ro/wp-content/uploads/MVC-4_94F/smsgV1.zip

Also you should not rely on Windows Identity to access the database. Why ? Because , usually , on Web hosting you have only some user name and password for database – not Active Directory user.

We will create an user that can access the Database and have full rights. We will manage further the rights for everyone.

Open SQL Server Management Studio and go to Security.

Please right click on “Logins ” in “Microsoft SQL Server Management Studio”
clip_image001[4]
Step 2: Please left click on “New Login… “
clip_image002[4]
Step 3: Please introduce “smsg” in “Login – New”
clip_image003[4]
Step 4: Please left click on “SQL Server authentication ” in “Login – New”
clip_image004[4]
Step 5: Please left click on “Password ” in “Login – New”
clip_image005[4]
Step 6: Please put “smsg” in “Login – New”
clip_image006[4]
Step 7: Please “smsg” in “Login – New” for confirm password
clip_image007[4]
Step 8: Please left click in “Login – New”
clip_image008[4]
Step 9: Please left click on “Enforce password policy ” in “Login – New”
clip_image009[4]
Step 10: Please left click on “Database ” in “Login – New”
clip_image010[4]
Step 11: Please keyboard input in “Login – New” […]
clip_image012[4]
Step 12: Add rights : Please left click on “SMsgS “
clip_image013[4]
Step 13: Please left click on “User Mapping ” in “Login – New”
clip_image014[4]
Step 15: Please mouse drag end on “Current view pane ” in “Login – New”
clip_image015[4]
Step 16: Please left click on “Smsg” database
clip_image016[4]
Step 17: smsg user will be in the dbo schema
clip_image017[4]
Step 18: Please put “dbo” as schema
clip_image018[4]
Step 19: Please select “db_datareader “
clip_image019[4]
Step 20: Please select on “db_datawriter ” in “Login – New”
clip_image020[4]
Step 21: Please select “db_owner ” in “Login – New”
clip_image021[4]
Step 22: Please left click on “OK ” in “Login – New”
clip_image022[4]
Now we will verify on database:
Step 23: Please left click on “Object Explorer Hierarchy ” in “Microsoft SQL Server Management Studio”
clip_image023[4]
Step 24: Please left click on “Object Explorer Hierarchy ” in “Microsoft SQL Server Management Studio”
clip_image024[4]
Step 25: Please left click on “smsg ” in “Microsoft SQL Server Management Studio”
clip_image025[4]

Summary
We have created database tables . We also generate a script for you that can be found here:
http://msprogrammer.serviciipeweb.ro/wp-content/uploads/MVC-4_94F/smsgV1.zip

But the security login ( user : smsg , pwd: smsg) you do have to do yourself.
You can also download the database backup from http://messagemvc.codeplex.com/releases/view/74250

To read:
3 Normal Form from Database Normalization, http://en.wikipedia.org/wiki/Database_normalization

Homework:
What if we want to send messages to a further date( let’s say, after on day 1 on the next month) ? What database changes do you envision ?

MVC4 and Simple messaging system

 

MVC4  Developer preview with mobile support just released. And, because best way to deal with is within an application, I decide to create a simple messaging system for any site that is made with MVC.

Description:

I begin with a registered user. What he can do:

1. Create messages ( subject + body ) to send to another registered user. The list of the users on the system will be taken either from database, either from an Application variable. The message will be recorded to a database( configured by the owner of the site )  with the possibility to be send also by email

2. When login, the registered user can see the list of messages and replies send to/by him. Also, if he has unread messages he can see an advertisement.

3. The application could be seen also from a mobile device.

What should be done also:

4. The install of the application should be easy for any developer ( xcopy or some package – like Nuget or recipe)

Wish me luck to finish !

Sql developer versus C#(.NET ) developer

I am not very well yet from my operation – so I can not write yet a tutorial.

But I want to propose you the following difference: Sql developer versus C# developer.

In my opinion , nowadays, if you program some application , you MUST have some database. More, the retrieving of data from the dtabase is a matter of relative speed to be seen by the user. So , for me, every .NET programmer should know about indexes / schema / foreign keys / users and so on. More, it has to know about connection pooling and stored procedures.

So, in my opinion ,  you can not be a .NET programmer without knowing much about databases.

What do you think, dear reader?

( This post is generated from a discussion in that the guy says that “I’m a C# developer, not a SQL developer.” and then leaving hints like “Any organization that follows a business practice of SQL users on databases needs to fire their DBA and find someone with the knowledge and foresight of how to scale security in db roles and filter the access through the domain admins (i.e., adding users to a user group in AD).”  and then “was the primary DBA for a number of years covering SQL7-SQL 2005. “)

What do you think about the discussion ?  And about the sql developer versus .net developer?

Programmer Explorer setting in windows 7

This is the first part of a series of multiple posts about how to have the explorer configured right(ok, my way)in Windows 7.

Please left click on “Organize (push button)” in “Computer”

clip_image001

Please left click on “Folder and search options (menu item)”

clip_image002

Please left click on “Open each folder in its own window (radio button)” in “Folder Options”

clip_image003

Please left click on “Show all folders (check box)” in “Folder Options”

clip_image004

Please left click on “Automatically expand to current folder (check box)” in “Folder Options”

clip_image005

Please left click on “View (page tab)” in “Folder Options”

clip_image006

Please left click on “Always show menus (check box)” in “Folder Options”

clip_image007

Please left click on “Display the full path in the title bar (Classic theme only) (check box)” in “Folder Options”

clip_image008

Please left click on “Show hidden files, folders, and drives (radio button)” in “Folder Options”

clip_image009

Please left click on “Hide empty drives in the Computer folder (check box)” in “Folder Options”

clip_image010

Please left click on “Hide extensions for known file types (check box)” in “Folder Options”

clip_image011

Please left click on “Hide protected operating system files (Recommended) (check box)” in “Folder Options”

clip_image012

Please left click on “Yes (push button)” in “Warning”

clip_image013

Please mouse drag start on “Position (indicator)” in “Folder Options”

clip_image014

Please left click on “Launch folder windows in a separate process (check box)” in “Folder Options”

clip_image015

Please left click on “Restore folder windows at logon (check box)” in “Folder Options”

clip_image016

Please left click on “Show encrypted or compressed NTFS files in color (check box)” in “Folder Options”

clip_image017

Please mouse drag start on “Position (indicator)” in “Folder Options”

Please mouse drag end on “Position (indicator)” in “Folder Options”

Please left click on “Search (page tab)” in “Folder Options”

clip_image018

Please left click on “Always search file names and contents (this might take several minutes) (radio button)” in “Folder Options”

clip_image019

Please left click on “Include compressed files (ZIP, CAB…) (check box)” in “Folder Options”

clip_image020

Please left click on “OK (push button)” in “Folder Options”

clip_image021

That will be all!

Programmer Visual Studio(Visual Web Developer Express) settings

Please left click on “Tools (menu item)” in “Start Page – Microsoft Visual Web Developer 2010 Express”
clip_image001[13]
Please left click on “Options… (menu item)”
clip_image002[12]
Please left click on “Show all settings (check box)” in “Options”
clip_image003[11]
Please left click on “Projects and Solutions (outline item)” in “Options”
clip_image004[11]
Please left click on “General (outline item)” in “Options”
clip_image005[11]
Please left click on “Track Active Item in Solution Explorer (check box)” in “Options”
clip_image006[11]
Please left click on “Always show solution (check box)” in “Options”
clip_image007[11]
Please mouse drag start on “Position (indicator)” in “Options”
clip_image008[11]
Please mouse drag end on “Position (indicator)” in “Options”
clip_image009[11]
Please left click on “Text Editor (outline item)” in “Options”
clip_image010[11]
Please left click on “HTML (outline item)” in “Options”
clip_image011[11]
Please left click on “HTML (outline item)” in “Options”
clip_image012[11]
Please left click on “Tabs (outline item)” in “Options”
clip_image013[11]
Please left click on “Formatting (outline item)” in “Options”
clip_image014[11]
Please left click on “Insert attribute value quotes when typing (check box)” in “Options”
clip_image015[11]
Please mouse drag start on “Position (indicator)” in “Options”
clip_image016[11]
Please mouse drag end in “Options”
clip_image017[11]
Please left click on “OK (push button)” in “Options”
clip_image018[11]

Programmer settings – Add shortcuts to “send to”

In this tip I will show how to add a “send to” notepad menu item(you can add for another programs too)

Please keyboard input [… Win-R]
clip_image001[6]
Please keyboard input in “Run” [Enter]
clip_image002[6]
Please right click on “Items View (list)” in “C:\Users\andrei ignat\AppData\Roaming\Microsoft\Windows\SendTo”
clip_image003[6]
Please left click on “New (menu item)”
clip_image004[6]
Please left click on “Shortcut (menu item)”
clip_image005[6]
Please keyboard input in “Create Shortcut” […]
clip_image006[6]
Please left click on ” (push button)” in “Create Shortcut”
clip_image007[6]
Please keyboard input in “Create Shortcut” […]
clip_image008[6]
Please left click on “Finish (push button)” in “Create Shortcut”
clip_image009[6]

Backup application for Windows Azure

I have written( with Vunvulea Radu and Catalin Gheorghiu)  a backup application for Azure. It is made with MVC + Azure Tables.

It was made initially for a contest, but I intend to work further and progress .

You can play at http://backupapp.cloudapp.net/

The code source is on http://myazurebackup.codeplex.com/ – and you can add also your feedback at http://myazurebackup.codeplex.com/workitem/list/advanced

And , if you are a programmer, you can also improve it – just leave a comment on codeplex!

Thank you,

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.