views:

86

answers:

0

I have an ASP.NET MVC app with a primitive repository layer that currently serves LINQ to SQL entities back to the controllers which then send them to the views. I now want to start using some domain-centric objects in place of my LINQ to SQL entities, and I have been using AutoMapper to help accomplish some of this. For simple property-to-property mapping, it's nice and trivially easy to use, but now I am faced with the problem of mapping entities which themselves contain only template text and database connection and query info. I would like to map these templated source types to fully token-replaced destination types.

For example, I have source and destination types...

public class Source
{
    public int Id { get; set; }
    public string MarkupTemplate { get; set; }
    public string DatabaseConnectionString { get; set; }
    public string DatabaseQuery { get; set; }
}

public class Destination
{
    public int Id { get; set; }
    public string Value { get; set; }
}

A source object might look something like this:

var source = new Source() {
    Id = 123,
    MarkupTemplate = "The %noun% is %adjective%.",
    DatabaseConnectionString = "Some SQL Server conn string",
    DatabaseQuery = "SELECT noun, adjective FROM things WHERE id=@id"
}

During the course of mapping (or elsewhere, if it makes sense to do that... I'm open to suggestion!) I need to:

  1. Connect to the database described in the source's DatabaseConnectionString property.
  2. Execute the select query that's in the source's DatabaseQuery property to get a single record.
  3. Replace tokens that are in the source's MarkupTemplate value with values found in the record returned from the database.
  4. Put the token-replaced content into the destination's Value property. With the example source object above, the destination's Value property should contain "The car is red." if the database query returns "car" and "red" for noun and adjective.

In my hacked-together prototype, I created quick-and-dirty methods in the repository to handle the token replacement steps and return the token-replaced value string, but I'd like to try to clean this up into a design that's a little easier to work with. Ultimately, I'd like to have a straightforward mapping mechanism that can provide my destination objects which I can send to my views, where those objects are as free of dependencies as possible.

related questions