开发者

Serializing a dataset to a strongly type business object property

In a vb .net winforms app I am trying out something for "wide and shallow" children of a record. I use strongly typed business objects ( Strataframe ).

My plan is to have a number of "child tables" collected in a dataset I drop on the form. they have no correspondence in persisted data, so the dataset is untyped and I am creating the schema through the property sheet for the tables. Each table is the datasource for a datagridview in the interface.

In my proof of concept sample, My main businessobject (CustomerBO) interacts with a SQL Server 2008 table with fields - pk, name, - and a third column which is currently varchar(max) as I considered XML but could just as easily 开发者_开发技巧be varbinary(max) if that works better for holding the serialized dataset.

bo.bigfield will be the strongly typed prop I want to hold the byte() array or XML or whatever that represents the dataset for that record.

So, the question in a nutshell - how do I convert a dataset to a single datum and reproduce the dataset from that datum. This is my first foray into datasets, datagridviews as well so if there are better ways to accomplish any of that I'm listening.

Code and advice very much appreciated - vb or C#


There are a number of ways to slice and dice. What you're talking about is Object Relational Mapping. There are a number of tools/frameworks out there, here are my favorites:

  • nHibernate
  • SubSonic

For rapid application development, SubSonic is my favorite hands down. Using the latest 3.x release, all you have to do is create a connection String, call up the SubSonic persistance layer, and pass in your object(s). It will create the DDL for persisting your objects in a DB without you having to really monkey with things that much. Here's an example for SubSonic:

    public class BaseDAO
    {
        private const String CONNECTION_STRING = "Repository";
        private static BaseDAO dao = new BaseDAO();
        private SimpleRepository repository;

        protected BaseDAO()
        {
            if (repository == null)
            {
                repository = new SimpleRepository(CONNECTION_STRING, SimpleRepositoryOptions.RunMigrations);
            }
        }

        public BaseDAO Instance()
        {
            return dao;
        }

        public SubSonic.Repository.SimpleRepository Repository
        {
            get { return this.repository; }
            set { this.repository = value; }
        }
    }

public class ProductDAO : BaseDAO
{
    public void save(ProductVO product)
    {
        if (product != null)
        {
            if (product.ID == null)
            {
                product.ID = Guid.NewGuid();
            }

            this.Repository.Add<ProductVO>(product);
        }
        else
        {
            throw new ArgumentException("The product passed in was null!");
        }
    }

    public void update(ProductVO product)
    {
        if (product != null)
        {
            this.Repository.Update<ProductVO>(product);
        }
        else
        {
            throw new ArgumentException("The product passed in was null!");
        }
    }

    public List<ProductVO> fetchAll()
    {
        return fetchAll(null);
    }

    public List<ProductVO> fetchAll(String name)
    {
        IEnumerable<ProductVO> list = this.Repository.All<ProductVO>();

        if (name != null && name.Length > 0)
        {
            var output =
                from products in list
                where products.Name.ToLower().Contains(name.Trim().ToLower())
                select products;

            list = output.ToList();
        }

        return list.OrderBy(product => product.Name).ToList();
    }

    public ProductVO fetchByID(object ID)
    {
        return this.Repository.Single<ProductVO>(ID);
    }

    public void delete(ProductVO product)
    {
        this.Repository.Delete<ProductVO>(product.ID);
    }
}

It's that easy.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜