Creating Generic MVC Controllers

Based on my previous posting regarding Generic Repositories, I decided to take this approach a step further and create Generic Controllers. The idea is simple: create a Generic Controller which can handle all of the CRUD operations by default. If we need to override or extend these methods, we can create a controller which inherits from this Generic Controller. We will also need to create a Controller Factory which will first look for a derived class, if it cannot find one, it will automatically instantiate an instance of the Generic Controller and use this instance as the controller.

First we must create a Generic Controller, capable of handling all of the CRUD requests. Below is my implementation.

public class ApplicationController<T> : Controller<T> where T: class
{
        protected ApplicationRepository<T> Repository { get; set; }
        private string[] _strLockedProperties = new string[] {
            "Id",
            "CreatedOn",
            "CreatedBy",
            "ModifiedOn",
            "ModifiedBy"
        };

        protected virtual string[] LockedProperties
        {
            get
            {
                return _strLockedProperties;
            }
        }

        public ApplicationController()
        {
            Repository = new ApplicationRepository<T>();
        }

        public virtual ActionResult Index()
        {
            return View(Repository.GetAll());
        }

        public virtual ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult Create(T record)
        {
            if (ModelState.IsValid)
            {
                Repository.Insert(record);
                Repository.Save();
            }

            return RedirectToAction("Index");
        }

        public virtual ActionResult Details(Guid id)
        {
            return View(Repository.Get(id));
        }

        public virtual ActionResult Edit(Guid id)
        {
            return View(Repository.Get(id));
        }

        [HttpPost]
        public virtual ActionResult Edit(Guid id, T record)
        {
            var properties = typeof(T).GetProperties();
            var fields = new List<string>();
            foreach (var prop in properties)
            {
                if (!LockedProperties.Contains(prop.Name))
                {
                    fields.Add(prop.Name);
                }
            }
            var allowed = fields.ToArray();
            var existing = Repository.Get(id);

            if (TryUpdateModel(existing, allowed))
            {
                Repository.Save();
                return RedirectToAction("Index");
            }

            return View(record);
        }

        public virtual ActionResult Delete(Guid id)
        {
            return View(Repository.Get(id));
        }

        [HttpPost]
        public virtual ActionResult Delete(Guid id, string confirm)
        {
            Repository.Delete(Repository.Get(id));
            Repository.Save();

            return RedirectToAction("Index");
        }

This class will be able to handle all of the default CRUD operations without worrying about the actual data types. Based on the previous posting, even the repositories will be automatically generated.

If you need any special functionality for a controller, simply create a derived class.

public class UserController : ApplicationController<UserAccount>
{
    public override ActionResult Edit(Guid id)
    {
        //Special code here.
    }
}

Now lets wire up our Controller Factory. We need to create a factory which first looks for a strongly typed controller. If it cannot find that strongly typed controller it will automatically instantiate a new Generic Controller and return this instance.

public class ApplicationControllerFactory : IControllerFactory
    {
        private string Namespace
        {
            get
            {
                return this.GetType().Namespace;
            }
        }

        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            if (string.IsNullOrEmpty(controllerName))
                throw new ArgumentNullException("controllerName");

            Type cType = Type.GetType(Namespace + ".Controllers." + controllerName + "Controller");

            if (cType == null)
            {
                cType = Type.GetType(Namespace + ".Library.ApplicationController`1[" + Namespace + ".Models." + controllerName + "]");
            }

            return Activator.CreateInstance(cType) as Controller;
        }

        public void ReleaseController(IController controller)
        {
            if (controller is IDisposable)
                (controller as IDisposable).Dispose();
            else
                controller = null;
        }

        public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
        {
            return System.Web.SessionState.SessionStateBehavior.Default;
        }
    }

Now register the Controller Factory in your Global.asax.cs file.

protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);
    ControllerBuilder.Current.SetControllerFactory(new ApplicationControllerFactory());
}

The effects on development speed of this approach is pretty profound. Essentially you have a Controller and Repository layer which is completely dynamic, powered only by the meta data of the data layer. All that must be done is creating the views, and overriding your controllers and repositories where necessary.

SQL to Delete All Users from Membership Provider

The .NET Membership Provider provides us with a stored procedure ‘aspnet_Users_DeleteUser’ to delete a user from a particular application. There are problems with this. What if you have a user who is allocated to many applications? What if you need to delete more than one user? I came up with the following script to completely remove all users (except the admin) from all applications.

DECLARE user_cursor CURSOR FOR
SELECT ApplicationName AS field1, Username AS field2, UserId AS field3
FROM aspnet_Users CROSS JOIN dbo.aspnet_Applications
WHERE username != 'admin'

OPEN user_cursor
DECLARE @field1 NVARCHAR(256)
DECLARE @field2 NVARCHAR(256)
DECLARE @field3 UNIQUEIDENTIFIER

FETCH NEXT FROM user_cursor
	INTO @field1, @field2, @field3
	WHILE @@FETCH_STATUS = 0
	BEGIN
	    PRINT @field1 + ':' + @field2
	    DELETE FROM dbo.aspnet_UsersInRoles WHERE UserId = @field3
		EXEC dbo.aspnet_Users_DeleteUser @ApplicationName = @field1,
		    @UserName = @field2,
		    @TablesToDeleteFrom = 50,
		    @NumTablesDeletedFrom = NULL
		DELETE FROM dbo.aspnet_Users WHERE UserId = @field3
		FETCH NEXT FROM user_cursor
		INTO @field1, @field2, @field3
	END
DEALLOCATE user_cursor;

The script works by first cross-joining applications and users, giving us a full list of every user to every application. We then use a cursor to loop the given set, and run our deletes against the set. This saved me countless hours of manual labor.

Repository Pattern, Lazy Style

One of the most annoying things when working with MVC is repeating the same code over and over again at each layer. We can try to abstract our classes, but it still only gets us so far, a lot of the code must still be manually typed or templated. I found myself particularly tired of creating repository objects for each of my database tables. Sure, I was inheriting from a generic base class, but a lot of the implementation I couldn’t get away from.

Using Linq2Sql was one of the reasons for my headaches. Types aren’t so easy to get at, and queries cannot be created dynamically at runtime–or could they? Dynamic Linq to the rescue. Microsoft has a seperate library available for download which actually allows you to run dynamic queries at runtime. Once added to your project, the dynamic functions are added through class extensions to the standard Linq classes.

I decided that I would start by creating a generic, templated, repository layer which could be extended if necessary, but would by default contain all of the necessary functions to interact with the model layer.

using System.Linq.Dynamic;

public abstract class Repository<R> where R : class
{
    public virtual DataContext Context { get; protected set; }

    public Repository() { }

    public Repository(DataContext context)
    {
        Context = context;
    }

    public virtual void Insert(R record)
    {
        Context.GetTable<R>().InsertOnSubmit(record);
    }

    public virtual void Delete(R record)
    {
        Context.GetTable<R>().DeleteOnSubmit(record);
    }

    public virtual void Update(R record)
    {
        return;
    }

    public virtual IEnumerable<R> GetAll()
    {
        var records = from r in Context.GetTable<R>()
                        select r;

        return records;
    }

    public virtual void Save()
    {
        Context.SubmitChanges();
    }

    public virtual void Revert(R record)
    {
        Context.Refresh(RefreshMode.OverwriteCurrentValues, record);
    }
}

Then create a class which inherits from your template, and code in all of the functions which are application specific.

public class ApplicationRepository<R> : Repository<R> where R: class
{
    public ApplicationDataContext AppContext
    {
        get
        {
            return AppContext as ApplicationDataContext;
        }
    }

    public ApplicationRepository()
    {
        AppContext = new ApplicationDataContext();
    }

    public virtual R Get(Guid Id)
    {
        var record = (from  r in AppContext.GetTable<R>().Where("Id = @0", Id)
                        select r).Single();

        return record;
    }

    public virtual IEnumerable<R> GetAll(string clause, params object[] args)
    {
        var records = from  r in AppContext.GetTable<R>().Where(clause, args)
                        select r;

        return records;
    }

    public override void Insert(R record)
    {
        var d = record as dynamic;
        d.Id = Guid.NewGuid();
        d.CreatedOn = DateTime.Now;
        d.CreatedBy = "System";

        base.Insert(record);
    }

    public override void Update(R record)
    {
        var d = record as dynamic;
        d.ModifiedOn = DateTime.Now;
        d.ModifiedBy = "System";

        base.Update(record);
    }
}

In my case, I added a narrowing ApplicationDataContext and some additional functionality. One of the key functions is the GetAll(string, param object[]) function. This allows me to essential run any queries against my database using that Dynamic Linq library. All of the functionality that I need from a repository layer will most likely already exist in this implementation. Anything else that is needed can be added later by inheriting from the ApplicationRepository class. This is going to save me a great deal of time.

Arduino Controlled Aquarium

My Latest Project: Aquarium Temperature & Lighting Control.

  • Amtega168 Programmable IC.
  • HD44780U 16×8 Parallel LCD
  • 1 Light Dependent Resistor
  • DS18B20 Waterproof Digital Temperature Sensor
  • 5 VDC SUB-MINI Relay

Read more

AutoMapper

AutoMapper is a great, free library available for .NET and accessible through nuget. It allows you to map and copy property data from one class to properties of another class. It does all of this based on naming convention.

Assume that we have the following the two classes:

class first
{
     public string A { get; set; }
     public string B { get; set; }
     public string X { get; set; }
}
class second
{
     public string A { get; set; }
     public string B { get; set; }
     public string Y { get; set; }
}

If we tell AutoMapper to map from an instance of class first to an instance of class second it will automatically copy the data stored in the public properties A and B from the first class into the second class. Properties Y and Z will not match, and therefore not be copied between the two classes. How does it do this? AutoMapper uses type reflection to automatically match the names of properties in the source class to the names of properties in the destination class.

This technique has many applications, and is available to save countless hours of mapping code. This can even be used to map properties of LINQ to SQL or Entity classes to allow for shallow copying of one object to another. Another application is for creating projection classes for use by an outside method.

How does it work? AutoMapper must be configured with knowledge about which classes to match. This must be done once per Application Domain.

Mapper.CreateMap<first, second>();
Mapper.AssertConfigurationIsValid();

For web applications it is suggested that this mapping be done in the Application_Start method of your Global.asax.cs or Global.asax.vb file. This will ensure that the mapping is only run once per domain. Now that we have told AutoMapper how to map, let’s tell AutoMapper to map something.

var fInstance = new first();
fInstance.A = “hello”;
fInstance.B = “world”;
var sInstance = Mapper.Map<first, second>(fInstance);
Console.WriteLine(sInstance.A + ” ” + sInstance.B);

The output of this application would be “hello world”. AutoMapper would copy the properties in first_instance.A and first_instance.B to a brand new instance of the second class.

At this point, if you tried to compile and run the application, you might be wondering why you are getting an exception. AutoMapper is unable to map first.X to first.Y, and therefore throws an exception. You can force AutoMapper to ignore a column when you creating your mapping using a lambda expression.

Mapper.CreateMap<first, second>().Ignore(a => a.X);

When you run the application again, AutoMapper will ignore that column and your output will be as expected. What happens when there are many columns you wish to ignore? Writing a lambda expression for each column would be tedious. Luckily somebody over at stackoverflow has come up with a handy extension method which you can use to ignore all columns which don’t match.

Running Javascript on UpdatePanel Response

I recently found the need to run a javascript function after each AJAX postback. As it turns out, the asp.net UpdatePanel supports many client side events. This makes adding a handler extremely easy. On your document.ready event, you can simply add a handler as follows:

$(document).ready(function () {
     Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
});

The function called must have the following signature:

function EndRequestHandler(sender, args) {
     if (args.get_error() == undefined) {
          /* Code Here */
     }
}

That’s all there is to it. The function EndRequestHandler will be called on each and every post back. This could also be a good place to handle any error messages that come back from the server by accessing args.get_error().

Magento Troubleshooting

If your using Magento, please do yourself a favor and make sure that your server’s date/time is synchronized:

rdate -s us.pool.ntp.org

If your time is not correct, you will be unable to login as the admin. You will be returned to the login screen without error, and probably spend the next few hours figuring out what went wrong.
Another tip: edit your php.ini and set your memory_limit variable to > 256M.

HPDetector – Detecting Stealth Ports

HPDetector is a free application which specializes in detecting hidden backdoor TCP/UDP ports. HPDetector uses a two step process to detect hidden ports. First, HPDetector compares the Linux Kernel or Windows Netstat exported list of ports with a list gathered from manually binding to ports. If a port caused a binding exception but is not shown in the Kernel/Netstat list, then it may be a hidden port. Redundant checks are done to ensure that the port is actually hidden.  This program essentially catches the operating system in a “lie” about which ports are bound to an interface. It is available for both Windows and Linux.   The Windows version requires the .NET 2.0 Framework. The Linux version requires either the latest JRE or the GNU Compiler for Java.

What does it mean if a hidden port is detected?  A hidden port being detected simply means that an inconsistancy was found between different portions of the operating system.  This is sometimes an indication that a Rootkit is hiding a backdoor.  A hidden port being detected should be taken seriously.  It provides a starting point to conduct further investigation.  For example, after detecting a hidden TCP port, an administrator can attempt to telnet to the port to see if it is actually listening for connections.  Other rootkit tools such as Rootkit Revealer will help verify the prescense of a rootkit.

HPDetector

Version: Linux/0.1 (December 8th, 2008)
License: GPL
Tarball: Download
SHA-1: 9ee1882886a153221d83ded82801cb9aafd4372d

 

Version: Windows/1.0 (January 14th, 2009)
License: GPL
Binary: Download
SHA-1: a2279f0f6de6f2e4f323bd1963a60f363c877a27
Source: Download
SHA-1: a65e58f453ec489b1d2f058692ce9683e7525919

Virtual Manager over QEMU+SSH

Virtual Manager allows you to connect to a hypervisor over SSH. This is a really nice feature for remote administration and monitoring of virtual machines using KVM or XEN.
This technique works nicely without any modification when connecting to the server as root through SSH. However, in almost any secure installation, the root user should not be allowed to log in through SSH for security reasons. What happens if you restrict the root user from logging in to the server through SSH? By default, a non-root user will not be able to connect to the libvirtd daemon, and will be notified that the connection has failed.

The workaround is fairly simple. First, create a new group called libvirtd.

[user@localhost ~]# su -
[root@localhost ~]# groupadd libvirt

Modify the libvirt configuration file located at /etc/libvirt/libvirtd.conf and uncomment the following lines:

unix_sock_group = “libvirt”
unix_sock_rw_perms = “0770”

Restart the service.

[root@localhost ~]# service libvirtd restart

Lastly, add your user account to the libvirt group.

[root@localhost ~]# useradd -G libvirt myusername

You will now be able to connect to the hypervisor without needing to log in as the root account.

Tumblr API

I’ve been doing a lot of work recently which requires integration of dynamic content, such as blogs, into existing websites. I decided to first try it out on my own website, realizing that it would make updating content a much simpler process.

I am impressed by how easy the Tumblr API is to implement. They basically give you two options for displaying content on your own website. The first method uses javascript to insert your blog’s content into an existing webpage:

<script type=”text/javascript” src=”http://blog.com/js” />

It’s as easy as that. Your existing blog posts will show up directly on the page you inserted this into. That being said, it isn’t going to look very nice without styling the various elements. The Tumblr API assigns css classes to almost every element which is inserted, making the process of styling a breeze.

The second method utilizes Tumblr’s XML based API services to grab content. Every blog’s API can be accessed via the following path: /api/read/. This request will return the contents of your blog formatted as an XML document. This leaves the dirty work of parsing the XML document up to us. An easy way to do this is to parse each element and add it to an existing Literal control on your ASP.NET page:

private void Tumblr()
{
  var wc = new WebClient()
  var xml = wc.DownloadString(“http://blog.com/api/read”);
  var xdoc = new XmlDocument();
  xdoc.LoadXml(xml);

  foreach (var xelem in xdoc.GetElementsByTagName(“post”))
  {
        var xtitle = xelem.GetElementsByTagName(“regular-title”);
        if (xtitle.Count < 1)
               continue;

          string date = xelem.GetAttribute(“date-gmt”);
          string url = xelem.GetAttribute(“url”);
          string title = xtitle[0].InnerText;
          string body = xelem.GetElementsByTagName(
               ”regular-body”)[0].InnerText;
          litTumbler.Text += string.Format(
                “<h4><a href="{0}" target="_blank">” +
                “({1})  {2}</a></h4>” +
                “{3}<br /><br />”,
                url,
                DateTime.Parse(date).ToString(),
                title,
                body);
     }
}

That’s all that you have to do to show some content from your blog. This has been extremely easy for me to add to use. My only concern is the scalability of that API read function. What happens if your blog contains thousands of posts? I’ll worry about that when the time comes.