English 中文(简体)
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:

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签