views:

530

answers:

2

I'm looking for possible ways to persist the following classes. Subsonic SimpleRepository looks like it might work, and people have said it should, when I asked a more general question.

But I've been unable to find a single example of how to do this - or at least one I could understand.

Can anyone point me to an example, or tell me how I could use Subsonic to map the following classes to a database?

Note that I haven't designed the database - I'm hoping Subsonic will do that for me, lazy sod that I am...

Edit: Just to expand on the previous point - I'm hoping to have Subsonic convert my object model to a relational DB, dealing with all the Parent-Child and One-To-Many relationships that are implied. Currently, I don't think Subsonic can do this. But even a working example (not a code fragment) that explicitly managed foreign keys, etc in the object model would be useful.

Some background and notes on the classes I want to persist:

  • they are used by the software that controls some measuring equipment
  • the Data class contains an array of RunData objects called RunFn, which holds the data for up to 10 individual measurement runs
  • note that RunData also contains an array of floats - RawY
  • if necessary, we can change the arrays to some other type of collection (List<>, etc)
  • developing in C#, VS2008, for SQL Server Express

Edit: I'm using Subsonic 3.0.0.3.

public class RunData

{
    public DateTime StartDateTime { get; set; }
    public TimeSpan ElapsedTime { get; set; }

    private float[] _rawY;
    public float[] RawY
    {
        get
        {
            return _rawY;
        }
        set
        {
            _rawY = value;
        }
     }
 }

public Data
{
    public string OperatorId { get; set; }
    public string SampleId { get; set; }

    // CAN SUBSONIC DEAL WITH THIS ARRAY OF OBJECTS???
    private RunData[] _runFn;
    public RunData[] RunFn
    {
        get
        {
            return _runFn;
        }
        set
        {
            _runFn = value;
        }
    }
}
+1  A: 

I'm not sure I'm going to answer everything you're asking here, but if I was implementing this using SimpleRepository I'd have the following models:

public class RawYValue
{
  public int Id { get; set; }
  public int RunDatumId { get; set; }
  public float YValue { get; set; }
}

public class RunDatum
{
   var repo = new SimpleRepository();

   public int Id { get; set; }
   public int DataId { get; set; }
   public DateTime StartDateTime { get; set; }
   public TimeSpan ElapsedTime { get; set; }

   public IQueryable<RawYValue> RawYValues 
   { 
     get { return repo.Find<RawYValue>(rawYValue => rawYValue.RunDatumId == Id); }
   }
 }

public Data
{       
  var repo = new SimpleRepository();

  public int Id { get; set; }
  public string OperatorId { get; set; }
  public string SampleId { get; set; }

  // CAN SUBSONIC DEAL WITH THIS ARRAY OF OBJECTS???
  public IQueryable<RunDatum> RunData 
  { 
     get { return repo.Find<RunDatum>(runDatum => runDatum.DataId == Id); }
  }
}

I imagine SubSonic will have trouble pluralising some of the names so you may need to change them but hopefully this will get you started.

Adam
+1 - Thanks Adam - exactly what I was looking for. I'll try it in the next day or so, and mark your answer as accepted if everything's OK.
Tom Bushell
I tried this, and Subsonic created a table for the Data class, but it just ignored the RunData list. I then modified the Data class to have a single RunDatum property, but that didn't work either. The only thing that's worked so far is to do "repository.Add(RunDatum)", which created another table, with no relationship to the first table. I was hoping Subsonic would navigate the whole class heirarchy, and build the database schema from that.
Tom Bushell
BTW, pluralization seemed to work fine with my original names. Also, I'm using Subsonic 3.0.0.3, if that makes a difference.
Tom Bushell
You'll need to tell SubSonic to run migrations by creating a SimpleRepository instance and passing SimpleRepositoryOptions.RunMigrations into the constructor. Have you looked at the docs?http://subsonicproject.com/docs/Using_SimpleRepository
Adam
Yup, doing that. I've since found some SO questions (including some of yours) which say that automatic schema generation is NOT possible with the current version of Subsonic, but that Rob is working on it e.g. http://stackoverflow.com/questions/1114691/relationships-and-lazy-loading-in-subsonic-3-0 . Most of them had "one-to-many", "parent-child", etc, in the titles.
Tom Bushell
Correction - meant to say "including some questions you answered" in previous comment.
Tom Bushell
Yeah, sorry I don't really use the SimpleRepository stuff much so I wasn't sure whether RunMigrations would navigate the full hierarchy.
Adam
Unfortunately, your answer implied you knew what you were talking about, and I wasted some time trying to make it work. Please delete it - I don't have the rep to do it myself. Thanks.
Tom Bushell
I've updated my example to make sure the db will be generated with foreign object Ids and the relationships will work.
Adam
A: 

To answer my own question...

Despite some other postings I found which imply that Subsonic SimpleRepository can automatically generate a relational schema from an object model, this turns out NOT to be the case. See Rob Conery's answer to this question:

relationships-and-lazy-loading-in-subsonic-3-0

He's working on it, however, and it will probably be well worth the wait.

In the meantime, I've looked at Fluent NHibernate, and this does what I want right out of the box. Their source code download has a demo project called Examples.FirstProject which demonstrates the functionality I'm looking for. Their documentation seems to be much more mature as well.

However, NHibernate also appears more complex overall, so it will be interesting to see what develops with Subsonic.

Edit: Heres a useful link that shows how to mangage foreign keys yourself in SimpleRepository -

subsonic-3-simplerepository

Have not tried it myself, but looks like it would actually work.

Tom Bushell
SubSonic will generate your database model for you using the SimpleRepository but you need to migrate each table. What that question is talking about is deep saving.
Adam
@Adam - I think it is more correct to say that "SimpleRepository will automatically create and migrate _individual tables_ for you, but you need to manage the relationships yourself in your object model". If I'm wrong, please post a _complete, working example_ that someone can paste into Visual Studio and have a reasonable chance of it working.
Tom Bushell
@Tom - I'd be interested to know how Fluent NHibernate worked out for you in this scenario? I am facing exactly the same problem with SimpleRespository so am thinking its worth a look.
Alex
@Alex - I've had some success with Fluent NHibernate - search for the "Automapping" tag for some of my experiences. The Automapping functionality mostly works, creating a relational DB from the object model. Very flexible and powerful. On the down side, much more complex than Subsonic, and documentation and working examples are scarce. But I've gotten help from the original authors, and continue to use it in my project. As far as I can tell, it's the only game in town if you want true automapping of reasonably complex objects.
Tom Bushell