Thursday, January 7, 2016

Lazy Loading using Windsor Castle

If you are not using Castle Windor and you want to do lazy loading of the myRepository you could write some code like the following.

      
public class MyClass
{
        private IRepository myRepository;
       
        public IRepository MyRepository
        {
            get
            {

                if (myRepository == null)
                {
                    myRepository = new Repository();
                }
                return myRepository;
            }
        }

}

The is quite repetitive code and hard codes the dependency and creation of the Repository object. The  Factory pattern could be used to abstract its creation to remove this dependency, but again writing a proper Factory without Castle Windsor is a tedious task. Check out the previous link for details on this.

With Castle Windsor this is trivial.

public Lazy MyRepository { get; set; }

You will need to turn on lazy loading. This is done in your Installer class.

    public class MyInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(

                Component.For()
                         .ImplementedBy().LifestyleTransient()

                Component.For()
                         .ImplementedBy()
                );
           
        }
    }


There is a slight difference in how you use the Property now though.

Instead of something like this as you would do in our first:

MyClass obj = new MyClass();
obj.MyRepository.SomeMethod();

Using the Castle Windsor code we need to call .Value to trigger the creation and dependency injection (lazy loading).

MyClass obj = new MyClass();
obj.MyRepository.Value.SomeMethod();

The best part about this solution is that it required very little work to implement, and IRepository and Repository are mapped in the Installer where they should be, not in the classes themselves. We also aren't calling Resolve or interacting with the IoC container anywhere except as recommended (see Three Calls Pattern).

Some technical ideas are based on discussions here.

Wednesday, January 6, 2016

Send CONTROL-ALT-DELETE to Remote Desktop to all changing password

As it turns out CONTROL-ALT-DELETE can't be sent to a Remote Desktop session. However, CONTROL-ALT-END can and this brings up the same screen on a Remote Desktop and allows you to change password.

If you are using Citrix instead of MS Remote Desktop you can try Control-F1.

If you are using Mac OSX, you can try FN-CONTROL-ALT-DEL.

If all else fails, you can bring up the Microsoft on screen keyboard and click the CONTROL-ALT-DELETE keys.

I got these tricks from here.

Thursday, December 24, 2015

How to configure one-to-one relationships with Entity Framework and Code First



Configure One-to-One Relationship - this site is focused on EF in general, but also has this specific article.

Must have Graphics Tools

Below is a short list of some really great tools for editing or creating graphics, editing photos, etc.

The Gimp - It is the cross platform FREE and Open Source alternative to  Photoshop.

Paint.NET - This is my long time favorite because it is easier to install and use than Gimp, opens faster, and has most the features I need.

Pixlr - Has most of the important functionality of The Gimp, but runs in any browser that supports flash.

Please suggest any other graphics apps that should be included here.

Tuesday, December 22, 2015

Map Windows Share to a Drive Letter using batch file / command line and prompt for password

The line of code can be pasted on a command prompt in Windows 2000 and newer or pasted into a text file with a .bat extension. It will prompt the user for a password to the Windows share. The username is hard coded

@ECHO OFF
rem net use /del V:
SET /P %password%=Password:
net use v:
\\Wtjwh175\c$\dev\dropbox /u:"yourDomain\yourUsername" %password%

Technically, you will get an error saying "The local device is already in use.", if you run it twice. If you don't want that to happen you can uncomment line 2 (change rem net use /del V: to net use /del V:). This will unmap the drive and then map it again each time the batch file is executed.

Thursday, December 3, 2015

Passing additional view data to a Edit to make sharing a EditorTemplate on an Edit and Create view.


Imagine you have a Editor in Shared\EditorTemplates\Person.cs for an object (Person in this example) and two views for an Order entity that is showing edit fields for Order obeject and the Person object. The two EditorTemplates are Person\Edit.cshtml and Person\Create.cshtml. To use the Person EditorTemplate in the Order Edit AND Create view we need to make sure we always pass a value for the Person.OrderID. If we don't do this, the problem is that when we use the editor in the context of the Order\Create.cshtml View the Model is null and validation for the hidden field will have a value of null. When it is on Order\Edit.cshtml the model will not be null and we will have a value for model (a Person object) and thus have the value Model.OrderID. It is really the Create that requires this work around, but we want to be able to use the EditorTemplate even when it is on a Create view. Below is the code needed

Code located in the Order\Edit.cshtml View
model in this context is the Order object
NOTE: We ARE passing MyOrderID value here since we do need the editor to know and keep the value.

@Html.EditorFor(model => model.Person, new { MyOrderID = Model.OrderID })

NOTE: When we set MyOrderID, it is scoped to the EditorTemplate that we are passing the value to. However, if we put the value in the ViewBag or ViewData directly (at the top or Order\Edit.cshtml for example) then it is available to all Views and EditorTemplates in this context.

Code located in the Order\Create.cshtml View
model in this context is the Order object
NOTE: we are not passing the MyOrderID value here since we just need any value (-1 was arbitrarily chosen to be safe) and will be assigned by the Entity Framework when it is saved to the database. Most importantly it will pass the validation and the model will be valid until then.

@Html.EditorFor(model => model.Person)
Code in the partial view ( in EditorTemplates folder makes it a template) for the Person object.
model in this context is the Person object
NOTE: If no value is passed using ViewBag.MyOrderID then we use -1, otherwise we use the passed value (usually from the Order\Edit.cshtml View)

@Html.HiddenFor(model => model.OrderID, new { Value = ViewBag.MyOrderID ?? -1 })


You can also use a strongly-typed ViewModel and pass all data to it instead of using the ViewBag.

Wednesday, December 2, 2015

Configure Code First migrations to use Singular Table Names

If you are using Code First Migration with Entity Framework you will probably have noticed that the table names it generates are all plural. Personally, I dislike this because all the contstraints including foreign keys are very confusing to read when they are plurarlized. Before you do an update-database for the first time be sure to override a method (OnModelCreating()) on your class (typicaly ApplicaionDbContext) that inherits from IdentityDbContext. The default class is called ApplicationDbContext.



public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        public DbSet People{ get; set; }
      

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // make the table names singular instead of plural
            //when code-first generates the table names

            modelBuilder.Conventions.Remove();
            base.OnModelCreating(modelBuilder);
        }


    }