Category: projects

Browser history–part 5–conclusions

This is the part 5 of 5 of my implementing of a MVC Browser history

MVC browser history – idea

Browser history –2 – implementing, small bugs

Browser history 3–trying to Nuget – modifications in order to can be transformed from an application to a component

Browser history 4–NuGet again -  finally Nuget deployment

Browser history–part 5–conclusions  – conclusions

 

 

I have reached the (partial) final for my MVC Browser History .  It has started as a simple application – and now it is a NuGet component ready to be used and a demo project at app harbour

What can do for you as a MVC programmer :

  • The component can be integrated in any MVC application easily (via NuGet)
  • you will have a page that displays the list of links that are part of the project and the top 5(configurable) accessed pages
  • You can have this filtered per user basis – or per administrator( small changes in the controller action)
  • It can save the history in memory or in sql server database( configurable in History page, ) or you can make your saving data by implementing  IBrowserUserHistoryRepository
  •  

    You can see in action at app harbour

    If you use, please send me an email.

    If you worry about code source, you can find at github . If you want to improve, feel free to participate Winking smile

    Browser history 4–NuGet again

    This is the part 4 of 5 of my implementing of a MVC Browser history

    MVC browser history – idea

    Browser history –2 – implementing, small bugs

    Browser history 3–trying to Nuget – modifications in order to can be transformed from an application to a component

    Browser history 4–NuGet again – finally Nuget deployment

    Browser history–part 5–conclusions – conclusions

    Prior to Nuget, I think that will be better if the user of my app could play with sql server or memory implementation. So the new GUI is here:

    image

     

    But how could I show to the user where it is saved? Easy – to the history page

    image

     

    And now I can make the NuGet Package .

    In order to can download a controller and not overwrite the Home controller, I must create a controller of its own – so make a modification : History Controller

    Now adding dependencies, fighting with NuGet Explorer,

    In the first test  StructureMap dll reference missing and the Views folder was in the wrong place.

    In the second test , I discovered that I was missing adding the following essential lines

    filters.Add(new BrowserHistory.Models.BrowserUserHistoryFilter());
    ObjectFactory.Configure(ce => ce.For<IBrowserUserHistoryRepository>().Use<BrowserUserHistoryRepositoryMemory>());

     

    So I began reading how to add to global.asax at nuget package installing time – and coming with this:http://blogs.msdn.com/b/davidebb/archive/2010/10/11/light-up-your-nupacks-with-startup-code-and-webactivator.aspx

    Adding code,testing -  another 15 minutes.

    Adding readme.txt – in order to explain what have I done and how to use it – another 30 minutes.

    Now it is ready to be used by you.

    If you want to test it, you can see in action at http://browserhistory.apphb.com/
    Detailed history of creating project at http://msprogrammer.serviciipeweb.ro/category/browserhistory/
    Source code at https://github.com/ignatandrei/MVCbrowserHistory

     

    Next time will do a summary of what I have done in order to build this simple utility project.

    Browser history 3–trying to Nuget

    This is the part 3 of 5 of my implementing of a MVC Browser history

    MVC browser history – idea

    Browser history –2 – implementing, small bugs

    Browser history 3–trying to Nuget – modifications in order to can be transformed from an application to a component

    Browser history 4–NuGet again – finally Nuget deployment

    Browser history–part 5–conclusions – conclusions

    TL;DR : Trying to add a feature (NuGET ) will conduce you to other features – apparently easy to implement – but took 2 hours…

     

    Content:

     

    First, source control is more important – http://www.joelonsoftware.com/articles/fog0000000043.html .I choose github  – deploy is a breeze with GitHub for windows . More, appharbor integration with github is awesome – you can see the application live at http://browserhistory.apphb.com/ .

     

    Now , for generating Nuget package, I should be moving the classes on their own assembly / dll. Also , added this blog to the info. Ensure it works the same. Uploaded to GitHub

    image

    And appharbor just deployed

    image

     

    Now, it will be good if I let user switch implementation between memory saving  and database saving… in order to developers( that have installed the dll with  NuGet  ) switch easily to their database.

    Created

    BrowserUserHistoryRepositorySqlServer
    

    and , after some implementing of IBrowserUserHistoryRepository , the method

    public IBrowserUserHistoryRepository FilterByUser(string UserName)

    put some problems – but solved with a private variable.

     

    Now must decide the best way to switch between MemoryRepository and SqlServerRepository in

    public static T AddOrRetrieveFromApplication<T>(HttpApplicationStateBase app)
    where T:new()
    

    First, we must retrieve at runtime the instance of the interface – so structuremap to the rescure.

    Second, if we need to retrieve a class – must have at least a default constructor.

    So the constraint of the T was gone away   – and the code is

    T result;

    if (typeof(T).IsInterface) {

    result =(T) ObjectFactory.GetInstance(type) ;

    }

    else

    {

    result = (T)Activator.CreateInstance(type);

    }

    Also, in global.asax the following lines were added:

    //ObjectFactory.Configure(ce => ce.For<IBrowserUserHistoryRepository>().Use<BrowserUserHistoryRepositoryMemory>());             //uncomment those for sql server ce             ObjectFactory.Configure(ce => ce.For<IBrowserUserHistoryRepository>().Use<BrowserUserHistoryRepositorySqlServer>());

    Now, when deploy , AppHarbor will NOT found EntityFramework dll.

    Added HintPath to csproj to find EF

    <HintPath>..\packages\EntityFramework.5.0.0\lib\net40\EntityFramework.dll</HintPath>
    as seen at
    http://blog.appharbor.com/2012/04/24/automatic-migrations-with-entity-framework-4-3

    and it works!

    More, it works with sql server AppHarbor addon  and transformation of web.config.

    image

     
    It took 2 hours to do this thing, apparently simple.
    image
    Now you can find the code source on github at https://github.com/ignatandrei/MVCbrowserHistory
    You can browse the application at http://browserhistory.apphb.com/

    Next time: (maybe some tests and ) NuGET!

    Browser history -2

    This is the part 2 of 5 of my implementing of a MVC Browser history

    MVC browser history – idea

    Browser history –2 – implementing, small bugs

    Browser history 3–trying to Nuget – modifications in order to can be transformed from an application to a component

    Browser history 4–NuGet again – finally Nuget deployment

    Browser history–part 5–conclusions – conclusions

    Now we repair the important issues and make some GUI adjustements to be nicer to the user GUI.

    1. Why the history does not display?
      • Because OnResultExecuted  is executed AFTER the View that display the results  is executed
      • Fix: do not display the ones that have no title. Modified ViewModel to take an IEnumerable.
    2. Why the count does not display after 2 clicks?
      • Because it was not saved in BrowserUserHistoryRepositoryMemory – only after SaveToRepositoryInterval links number
      • Fix: In the action save again to have latest clicks. Also modified interface Save to take an IEnumerable – and not a class.
    3. Modified GUI interface to have links where the URL displays – add ViewUrl shared VIew in MVC.
    4. Add numbering to the table ( 1,2,…)
    5. Add texts ( instead of My MVC application – My Browser History application)
    6. Add Filter By User – to be easier for the programmers that will use the program.
    7. Maybe will be useful to add sorting and filtering   – but only if someone asks.

    The code is here

    MVC browser history

    This is the part 5 of 5 of my implementing of a MVC Browser history

    MVC browser history – idea

    Browser history –2 – implementing, small bugs

    Browser history 3–trying to Nuget – modifications in order to can be transformed from an application to a component

    Browser history 4–NuGet again – finally Nuget deployment

    Browser history–part 5–conclusions – conclusions

    I want to do another small open source utilities project – MVC browser history . That seems simple at the beginning – but , yet , there are small tasks to do that are not so easy .

    What does MVC browser history  does? Well, keep an history of the links the user clicks through an MVC site. I must eliminate the ajax requests and Child requests – so keep the just the URL’s

    So what I have learned after one hour:

    Thinking process :

    I must somehow maintain the url’s – either in database , either in memory – so a Repository concept came handy.

    Data to be saved includes :

    1. URL –easy –  HttpContext.Request.Url.ToString();
    2. Date – easy – DateTime.Now;
    3. UserName – not so easy – what if user not registered?
    4. PageName – it should belong to that, but, from a RelationalDatabase in a 3rd form, should be another table.

    Named this class BrowserUserHistoryData ( rather a long name, but descriptive)

    Now detailing:
    URL – possible problem : differentiate between Person/Edit/1 and Person/Edit/2. It’s a feature, not a bug Winking smile : browser history, not Action history. More, the User can have acces to Person/Edit/1 and NOT Person/Edit/2.
    UserName – maybe Session , if not registered? (con.User.Identity.IsAuthenticated ? con.User.Identity.Name : con.Session.SessionID); , if not WebApp, then … ok, not for this time.

    How to obtain the data? The global filter from http://bradwilson.typepad.com/blog/2010/07/aspnet-mvc-filters-and-statefulness.html

    What to obtain ? A user history and first 5 links order by count

    Programming:

    Errors repaired:

    1. Brad Wilson put data in context.HttpContext.Items . Forget that is good for one request only. Thinking about how to persist and obtain data in MVC action and Global filter- and storing to Application . Made public static T AddOrRetrieveFromApplication
    2. Adding ViewName in OnResultExecuting, not in OnResultExecuted.
    3. Reading data from Repository WITHOUT persisting there Winking smile.
    4. Thinking that the Model of the page is just the list of user history. Nope, it is also the first 5 links order by count : HistoryViewModel
      public class HistoryViewModel
          {
              public BrowserUserHistory UserHis;
              public IBrowserUserHistoryRepository rep;
          }
      

    Errors not repaired:

    1. Not showing just HIS user browser history, but all ( not to be repaired, since Cassini changes session_id all the time) . NOT TO BE REPAIRED.
    2. Latest links, history himself, not show view name

    image

    What I have enjoyed:

    1. Programming process
    2. The idea that will be useful to other people
    3. Linq:  – can be translated to database also.
       public IEnumerable<KeyValuePair<string,int>> MostUsed(int Count, DateTime? date)
              {
                  var data= historyMemory.Where(item =>(date==null || item.Date.Subtract(date.Value).Days == 0)).GroupBy(item=>item.Url).OrderByDescending(g=>g.Count()).Take(Count);
                  return data.Select(i =>new KeyValuePair<string,int>( i.Key,i.Count()));
              }
      

    What to do next:

    1. Correct the error
    2. Test more the application with a real application
    3. Make the application understandable – comments.
    4. Make the application publicly available
    5. Spread the word ( already did, but not enough).

    Code can be downloaded from BrowserHistory

    . Play with it – and, if you like, drop a comment.

    Hydrating

    My first Nuget project: Hydrating.  Also a Codeplex project : http://hydrating.codeplex.com/ 

    It can re-make an object by adding items of “property/value”

    It comes in 2 flavors: .NET 2.0 ( reflection ) and .NET 4  ( expression).

    Sample Usage:

    Sample usage:
    var Model = new HydrateGeneric<MyModel>();
    Model.AddNewProperty("OneProp", "bb");
    Model.AddNewProperty("newData.StartDate", DateTime.Now.AddDays(1).ToString());
    Model.AddNewProperty("newData.SecondProp", "AB");
    Model.AddNewProperty("newData.aOne.ThirdProp", "XXX");
    var data = Model.NewObject();
    Console.WriteLine(data.newData.SecondProp);
    Console.WriteLine(data.newData.StartDate);
    Console.WriteLine(data.newData.aOne.ThirdProp);

     

    It comes from a Paulo Morgado idea from http://msmvps.com/blogs/paulomorgado/archive/tags/ExpressionTrees/default.aspx . However, his initialization does not specify property name – so it’s rather error prone if you do not specify properties + values in right order. I have somewhat improved by

     

    Model. AddNewProperty("OneProp", "bb");

     

    Enjoy!

    http://hydrating.codeplex.com/

    https://nuget.org/packages/Hydrate

    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 !

    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.