zoukankan      html  css  js  c++  java
  • Domain Object Layer Design and Sample Code for LiteMDA

    Source Codes in C# 2.0 Format Download

    In these sample codes, BaseObject.cs, ICondition.cs, IPersistable.cs and ObjectCollection.cs are shared classes are for all Domain Objects. And IUserData.cs, UserData.cs and User.cs represent a Domain Object “User”, mapping to Table “User” in a RDB when using a RDB.

    Sample Domain Object “User” and “ObjectCollection<User>” (C# 2.0 Generic Style Class) represent a single user and user’s collection. They are all inheriting IPersistable, so need to implementing “Save” and “Load” methods of IPersistable. Class “User” inherits BaseObject<UserData> which provides “User” the special custom batch Update support.

    With the help of the following four methods, we can do all kinds of “Read” and “Write” to “User”: User.Save, User.Load, ObjectCollection<User>.Save, ObjectCollection<User>.Load and BaseObject<UserData>.BatchUpdate.

    And with the benefit of C# 2.0’s Generic, codes become more clearly than un-generic style implementing.

    IUserData.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestDomainObject0808
    {
        
    public interface IUserData
        
    {
            
    int? ID getset; }
            
    string Name getset; }
        }

    }


    UserData.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestDomainObject0808
    {
        
    public class UserData : IUserData
        
    {
            
    #region Private Members

            
    private int? _ID;
            
    private string _Name = null;

            
    #endregion


            
    #region Attributes

            
    public int? ID
            
    {
                
    get
                
    {
                    
    return _ID;
                }

                
    set
                
    {
                    _ID 
    = value;
                }

            }


            
    public string Name
            
    {
                
    get
                
    {
                    
    return _Name;
                }

                
    set
                
    {
                    _Name 
    = value;
                }

            }


            
    #endregion


            
    #region Constructors

            
    public UserData()
            
    {
            }


            
    #endregion

        }

    }


    IPersistable.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestDomainObject0808
    {
        
    public interface IPersistable
        
    {
            
    void Save();
            
    void Load(ICondition condition);
        }

    }


    BaseObject.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestDomainObject0808
    {
        
    public abstract class BaseObject<ObjectType>
        
    {
            
    public static void BatchUpdate(ICondition condition, ObjectType obj)
            
    {
                
    //Only un-null/HasValue attributes will be treated as need-to-update attributes
                
    //
            }

        }

    }

    ICondition.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestDomainObject0808
    {
        
    public interface ICondition
        
    {
            
    //ICondition will be OO style, and should be either designtime static definition 
            
    //in configuration file or runtime dynamically constructed ones in codes or 
            
    //free combinations of them.
        }

    }

    User.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestDomainObject0808
    {
        
    public class User : BaseObject<IUserData>, IUserData, IPersistable
        
    {
            
    #region Private Members

            
    private IUserData _OldData = null;
            
    private IUserData _CurrentData = null;
            
    private void RestoreOldData()
            
    {
                _CurrentData 
    = new UserData();
                _CurrentData.ID 
    = _OldData.ID;
                _CurrentData.Name 
    = _OldData.Name;
            }


            
    #endregion


            
    #region Attributes

            
    public int? ID
            
    {
                
    get
                
    {
                    
    return _CurrentData.ID;
                }

                
    set
                
    {
                    _CurrentData.ID 
    = value;
                }

            }


            
    public string Name
            
    {
                
    get
                
    {
                    
    return _CurrentData.Name;
                }

                
    set
                
    {
                    _CurrentData.Name 
    = value;
                }

            }


            
    #endregion


            
    #region Constructors

            
    public User()
            
    {
                _OldData 
    = new UserData();
                RestoreOldData();
            }


            
    public User(IUserData ud)
            
    {
                _OldData 
    = (ud == null? new UserData() : ud);
                RestoreOldData();
            }


            
    public User(ICondition condition) : this()
            
    {
                Load(condition);
            }


            
    #endregion


            
    #region IPersistable

            
    public void Save()
            
    {
                
    //Save()'s implementing denpending on the configuration setting for current DO's primary key and user defined type attributes;
                
    //If _CurrentData.PK == null then do Creating New Object in persisting;
                
    //else _CurrentData.PK != null then do Updating Object in persisting.
                
    //When do Updating Object, compare the _OldData and _CurrentData's Attributes' values, and 
                
    //dynamically construct minimal update operation.
                
    //For user defined type attributes, need do recursively Saving on attributes of this kind.
                
    //
            }


            
    public void Load(ICondition condition)
            
    {
                
    //Load object data according to specified condition
                
    //
            }


            
    #endregion

        }

    }

    ObjectCollection.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestDomainObject0808
    {
        
    public class ObjectCollection<ObjectType> : List<ObjectType>, IPersistable
            where ObjectType : IPersistable
        
    {
            
    #region Constructors

            
    public ObjectCollection() : base()
            
    {
            }


            
    public ObjectCollection(ICondition condition)
                : 
    this()
            
    {
                Load(condition);
            }


            
    #endregion


            
    #region IPersistable

            
    public void Save()
            
    {
                
    foreach (ObjectType obj in this)
                
    {
                    obj.Save();
                }

            }


            
    public void Load(ICondition condition)
            
    {
                
    this.Clear();

                
    if (condition != null)
                
    {
                    
    //Load objects according to specified condition
                    
    //
                }

            }


            
    #endregion

        }

    }

    //The End

     

  • 相关阅读:
    4、idea使用git
    ♫【MongoDB】
    -_-#【网站优化】预加载(Pre-loader) / 预读取(Pre-fetching)
    【Node】package.json
    ☀【移动】Google Maps JavaScript API v3
    -_-#【移动】视频分段
    ☀【Grunt】package.json, Gruntfile.js, npm install, grunt
    【three.js】
    【单元测试】
    洛谷——P1125 笨小猴
  • 原文地址:https://www.cnblogs.com/teddyma/p/209933.html
Copyright © 2011-2022 走看看