using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using DataAccess.Core;
using DataAccess.SqlServer;
using DataAccess.Core.Interfaces;

namespace SauceExample
{
  public class CrudClass
  {
    public static IDataStore _dstore; //I recommend keeping the reference around

    static CrudClass()
    {
      _dstore = SqlServerConnection.GetDataStore(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString);
    }

    [STAThread]
    public static void Main(string[] args)
    {
      //Table will be created and object inserted
      MyObject ToSave = new MyObject() { Name = "MyObject" };
      _dstore.InsertObject(ToSave);

      //id property will be set on the object when done, so I can load based on that
      MyObject loaded = _dstore.LoadObject<MyObject>(ToSave.id);
      //MyObject loaded = (MyObject)_dstore.LoadObject(typeof(MyObject), ToSave.id); //this works too
      //MyObject loaded = (MyObject)_dstore.LoadObject(ToSave); //this will work if the primary key is set (at least) all other fields get overwritten

      //update the name property
      loaded.Name = "Some New Name";

      //persist change
      _dstore.UpdateObject(loaded);

      //look up by name
      MyObject loadedAgain = _dstore.Query<MyObject>().Where(R => R.Name == "Some New Name").FirstOrDefault();

      //All done, time to clean up
      _dstore.DeleteObject(ToSave); //I could use either object here
      //_dstore.DeleteObject<MyObject>(ToSave.id) //this works too
    }
  }
}

Last edited May 30, 2013 at 3:18 AM by iamkrillin, version 4

Comments

No comments yet.