开发者

Subsonic - can anyone provide an example of using Subsonic SimpleRepository to persist a list/array of objects?

开发者 https://www.devze.com 2022-12-09 09:47 出处:网络
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.

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 Subson开发者_开发百科ic 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;
        }
    }
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消