Category: nuget

Entity Framework 6 Record and play use : Unit Testing ( part 2 of 5)

 

Part 1 : What is EF record and play : http://msprogrammer.serviciipeweb.ro/2014/11/29/entity-framework-6-record-and-play-1-of-5/ 

Part 2: EF Record and play use: Testing : http://msprogrammer.serviciipeweb.ro/2014/12/08/entity-framework-6-record-and-play-use-unit-testing-part-2-of-5/

Part 3: EF Record and play use: Make demo: http://msprogrammer.serviciipeweb.ro/2014/12/14/entity-framework-6-record-and-play-use-making-demos-part-3-of-5/ 

Part 4: EF Record and play use: Record user Sql when a bug occurs: http://msprogrammer.serviciipeweb.ro/2014/12/26/ef-record-and-play-use-recording-user-sql-when-a-bug-occurred-part-4-of-5/

Part 5: EF record and play: conclusions: http://msprogrammer.serviciipeweb.ro/2015/01/05/ef-record-and-play-conclusions/

 

Let’s suppose that we have a program that have Departments and Employees.

And we want to make sure that, when we add an employee, the department must exists.

We can ensure this from database ( by foreign key) but we can pro-actively search for the department and throw a more meaningful validation .

More, I like more validation than errors.

So, let’s suppose that in the Validation for the Employee we must check in the database for the IdDepartment to see if there is such a department.

How could we make a test for that runs without a database ?

With some trick:  we first Record with a database  – then we can Play the file – and we do not need anymore the database. The test is self contained. 

 

Let’s see in action here

 

Database.SetInitializer<ContextForDatabase>(null);
            #region set record EF
            var record = new InterceptionRecordOrPlay(@"VerifyIValidatableWorks.zip", ModeInterception.Play);

            DbInterception.Add(record);
            #endregion
            var e= new Employee();
            e.ValidateEmployee = true;
            e.IDDepartment = 60000;
            var err= e.Validate(null).ToArray();
            Assert.IsNotNull(err);
            Assert.AreEqual(1, err.Length);

Source code is available at  https://github.com/ignatandrei/EFRecordAndPlay/wiki/

 

There is also a NuGet package at https://www.nuget.org/packages/EFRecordAndPlay/

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.

    Clearer as Nuget package

    It was not so difficult to make Clearer as NuGet package. Just analyzing MVC Sitemap package done the trick. So the package looks like:

    image

     

    You can install from http://nuget.org/packages/Clearer or download a sample application  http://clearer.codeplex.com

    ( What he does ?

    In every application there are some variables that are set by the ASP.NET application( ASP.NET_SessionId cookie ) and some that are set by the programmer( cached data in Application/Session/Cache/Cookies and so on).
    I wanted every time to have a page where I can “clear” / delete those – and not found. So it’s the Clearer project.
    Possible uses:
    For developers – when they want to see what happens when a cache item no longer exists
    For developers – to put to site admins some simple tool to reload data from Cache/Application . Just edit the LoadAll function to load only Cache/Application
    For developers – to test easily the session. Just delete ASP.NET_SessionId cookie – you will get another one when you refresh the page.

    )

    So please install Nuget package from http://nuget.org/packages/Clearer and tell me if it works!

    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