Listing the contents of a database file which is to be displayed in a View

I'm using an open source fingerprint recognition program written in C# (SourceAFIS) which stores the enrolled users in a .dat file.

To read from the .dat file I use:

List<MyPerson> database = new List<MyPerson>();
BinaryFormatter formatter = new BinaryFormatter();

if (File.Exists(Database))
{ 
    using (FileStream stream = System.IO.File.OpenRead(Database)) 
    database = (List<MyPerson>)formatter.Deserialize(stream); 
}

Where Database is the path of the database.dat file.

With this I can use database.Add(Enroll(Image, Name)); to enroll people into the system.

The enroll function is as follows:

static MyPerson Enroll(string filename, string name)
{
    MyFingerprint fp = new MyFingerprint();
    fp.Filename = filename;
    BitmapImage image = new BitmapImage(new Uri(filename, UriKind.RelativeOrAbsolute));
    fp.AsBitmapSource = image;
    MyPerson person = new MyPerson();
    person.Name = name;
    person.Fingerprints.Add(fp);
    person.Fingerprints[0].Finger = Finger.LeftThumb;
    Afis.Extract(person);
    return person;
}

I was wondering if it were possible to list each user in the database and have them displayed in one of my ASP.NET MVC views? How am I able to check the way in which the database is written to?

EDIT:

MyPerson and MyFingerprint classes that are declared in the controller

[Serializable]
class MyPerson : Person
{ public string Name; }

[Serializable]
class MyFingerprint : Fingerprint
{ public string Filename; }

Answers


The database object in your example isn't a database in the traditional sense. Rather, it's the List of MyPerson objects serialized as a file, including their fingerprint templates and any other values the MyPerson class contains. There is a document that describes the format used for writing the file, but since the file just represents an object of type List<MyPerson>, there's not a lot to gain by trying to read the binary format of the file. I've used serialization numerous times and have never bothered to look at the file format. It's much safer to just let the BinaryFormatter class handle the work.

If you haven't already, you should look at the documentation for serialization on MSDN. If your MyPerson class has any chance of being changed in the future, you should look at the section on Version Tolerant Serialization to avoid an potential problems down the road.

The database object can be passed directly to a view as shown in the following sample code. You will likely need to update the namespace in the view to reflect your project. The code in the Index method is just to demonstrate the concept: it's not reflective of good app architecture.

Controller

public class MyPersonController : Controller {

    public ActionResult Index() {
        List<MyPerson> database = new List<MyPerson>();
        BinaryFormatter formatter = new BinaryFormatter();
        if (File.Exists(Database))
        { 
            using (FileStream stream = System.IO.File.OpenRead(Database)) 
            database = (List<MyPerson>)formatter.Deserialize(stream); 
        }


        return View(database);
    }
}

View, listing only the Name property

@model IEnumerable<MyPerson>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
    </tr>
}

</table>

Need Your Help

AES CFB 256 bit encrypt the output is longer than the input?

.net aes encryption

I want to ask something, what makes a difference in the output below that these two programs vb.net 2010 and the dynamic c? Why the length of the encrypted data different results (vb.net - 16 byte =

jquery validation in multiple forms

jquery html jsp validation

I have a form with the following format: