<repo_name>skykrf/Guadi.ALL<file_sep>/Guadi.Web.Edit/Models/ProductModels.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Guadi.Web.Edit.Models
{
    public class ProductAddModel
    {
        /// <summary>
        /// 产品编号
        /// </summary>
        public string ProductCode { get; set; }

        /// <summary>
        /// 产品中文名
        /// </summary>
        public string ProductChineseName { get; set; }

        /// <summary>
        /// 成本价
        /// </summary>
        public decimal CostPrice { get; set; }

        /// <summary>
        /// 销售价
        /// </summary>
        public decimal SalePrice { get; set; }

        /// <summary>
        /// 市场价
        /// </summary>
        public decimal MarketPrice { get; set; }

        /// <summary>
        /// 最低价
        /// </summary>
        public decimal LowstPrice { get; set; }

        /// <summary>
        /// 是否上架
        /// </summary>
        public bool IsOnSale { get; set; }

        /// <summary>
        /// 产品分类ID
        /// </summary>
        public int CategoryId { get; set; }

    }
}<file_sep>/Guadi.Common.Tools/DataAccessException.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Guadi.Common.Tools
{

    /// <summary>
    /// 数据访问层异常类，用于封装数据访问层引发的异常，以供业务逻辑层抓取
    /// </summary>
    [Serializable]
    public class DataAccessException:Exception
    {

        public DataAccessException()
        {
        }

        /// <summary>
        /// 使用异常消息实例化一个新实例
        /// </summary>
        /// <param name="message"></param>
        public DataAccessException(string message) : base(message)
        {
        }

        /// <summary>
        /// 使用异常消息与一个内部异常实例化一个新实例
        /// </summary>
        /// <param name="message"></param>
        /// <param name="inner"></param>
        public DataAccessException(string message, Exception inner) : base(message, inner)
        {
        }

        /// <summary>
        ///     使用可序列化数据实例化一个新实例
        /// </summary>
        /// <param name="info">保存序列化对象数据的对象。</param>
        /// <param name="context">有关源或目标的上下文信息。</param>
        protected DataAccessException(SerializationInfo info, StreamingContext context)
            : base(info, context) { }
    }
}
<file_sep>/Guadi.Common/DataBase.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;

namespace Guadi.Common.Data
{
    /// <summary>
    /// Guadi数据基础类(所有数据库表映射实体都必须继承该类)
    /// </summary>
    [Serializable]
    public class DataBase<TKey>
    {

        [Required,Column("Id")]
        public TKey Id { get; set; }

        [Required,Column("CreateTime")]
        public DateTime CreateTime { get; set; }

        [Required,Column("UpdateTime")]
        public DateTime UpdateTime { get; set; }

        [NotMapped]
        public EnumDataStatus DataStatus
        {
            get { return (EnumDataStatus) Enum.ToObject(typeof (EnumDataStatus), DataState); }
            set { DataState =(byte)value; }
        }

        [Required,Column("DataState")]
        public byte DataState { get; private set; }

    }


    /// <summary>
    /// 数据状态枚举
    /// </summary>
    public enum  EnumDataStatus:byte
    {
        /// <summary>
        /// 正常(默认)
        /// </summary>
        Normal=0,

        /// <summary>
        ///删除
        /// </summary>
        Deleted=1
    }
}
<file_sep>/Guadi.Product.BLL/Impl/ServiceBase.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Guadi.Common.Data;

namespace Guadi.Product.BLL.Impl
{
    /// <summary>
    /// 核心业务实现基类
    /// </summary>
    internal abstract class ServiceBase
    {
        /// <summary>
        /// 获取或设置 工作单元对象，用于处理同步业务的实务操作
        /// </summary>
        [Import]
        protected IUnitOfWork UnitOfWork { get; set; }
    }
}
<file_sep>/Guadi.Common/EfRepositoryBase.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Guadi.Common.Tools;

namespace Guadi.Common.Data
{
    /// <summary>
    ///  EntityFramework仓储操作基类
    /// </summary>
    /// <typeparam name="TEntity">动态实体类型</typeparam>
    /// <typeparam name="TKey">实体主键类型</typeparam>
    public abstract class EfRepositoryBase<TEntity,TKey> : IRepository<TEntity,TKey> where TEntity : DataBase<TKey>
    {
        #region 属性

        /// <summary>
        /// 获取 仓储上下文的实例
        /// </summary>
        [Import]
        public IUnitOfWork UnitOfWork { get; set; }

        /// <summary>
        ///  获取 EntityFramework的数据仓储上下文
        /// </summary>
        protected UnitOfWorkContextBase EfContext
        {
            get
            {
                if (UnitOfWork is UnitOfWorkContextBase)
                {
                    return UnitOfWork as UnitOfWorkContextBase;
                }
                throw new DataAccessException(string.Format("数据仓储上下文对象类型不正确，应为UnitOfWorkContextBase,实际为{0}",UnitOfWork.GetType().Name));
            }
        }

        /// <summary>
        /// 当前实体的查询数据集
        /// </summary>
        public IQueryable<TEntity> Entities
        {
            get { return EfContext.Set<TEntity,TKey>(); }
        }

        #endregion

        #region 公共方法

        /// <summary>
        /// 插入实体记录
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="isSave">是否执行保存</param>
        /// <returns>操作影响的行数</returns>
        public virtual int Insert(TEntity entity, bool isSave = true)
        {
            EfContext.RegisterNew<TEntity,TKey>(entity);
            return isSave ? EfContext.Commit() : 0;
        }

        /// <summary>
        /// 批量插入实体记录
        /// </summary>
        /// <param name="entities">实体记录集合</param>
        /// <param name="isSave">是否执行保存</param>
        /// <returns>操作影响的行数</returns>
        public virtual int Insert(IEnumerable<TEntity> entities, bool isSave = true)
        {
            EfContext.RegisterNew<TEntity,TKey>(entities);
            return isSave ? EfContext.Commit() : 0;
        }

        /// <summary>
        /// 删除指定编号的记录
        /// </summary>
        /// <param name="id">实体记录编号</param>
        /// <param name="isSave">是否执行保存</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(TKey id, bool isSave = true)
        {
            TEntity entity = EfContext.Set<TEntity,TKey>().Find(id);
            return entity != null ? Delete(entity, isSave) : 0;
        }

        /// <summary>
        /// 删除实体记录
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="isSave">是否执行保存</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(TEntity entity, bool isSave = true)
        {
            EfContext.RegisterDeleted<TEntity,TKey>(entity);
            return isSave ? EfContext.Commit() : 0;
        }

        /// <summary>
        /// 删除实体记录集合
        /// </summary>
        /// <param name="entities">实体记录集合</param>
        /// <param name="isSave">是否执行保存</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(IEnumerable<TEntity> entities, bool isSave = true)
        {
            EfContext.RegisterDeleted<TEntity,TKey>(entities);
            return isSave?EfContext.Commit():0;
        }

        /// <summary>
        /// 删除所有符合特定表达式的数据
        /// </summary>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="isSave">是否执行保存</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(Expression<Func<TEntity, bool>> predicate, bool isSave = true)
        {
            List<TEntity> entities = EfContext.Set<TEntity,TKey>().Where(predicate).ToList();
            return entities.Count > 0 ? Delete(entities, isSave) : 0;
        }

        /// <summary>
        /// 更新实体记录
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="isSave">是否执行保存</param>
        /// <returns>操作影响的行数</returns>
        public int Update(TEntity entity, bool isSave = true)
        {
            EfContext.RegisterModified<TEntity,TKey>(entity);
            return isSave ? EfContext.Commit() : 0;
        }

        /// <summary>
        /// 使用附带新值的实体信息更新指定实体属性的值
        /// </summary>
        /// <param name="propertyExpression">属性表达式</param>
        /// <param name="entity">附带新值的实体信息，必须包含主键</param>
        /// <param name="isSave">是否执行保存</param>
        /// <returns>操作影响的行数</returns>
        public int Update(Expression<Func<TEntity, object>> propertyExpression, TEntity entity, bool isSave = true)
        {
            throw  new NotSupportedException("上下文公用，不支持按需更新功能");
        }

        /// <summary>
        /// 查找指定主键的实体记录
        /// </summary>
        /// <param name="key">指定主键</param>
        /// <returns>符合编号的记录，不存在返回null</returns>
        public TEntity GetByKey(TKey key)
        {
            return EfContext.Set<TEntity,TKey>().Find(key);
        }

        #endregion
    }
}
<file_sep>/Guadi.User.BLL/Impl/UserServices.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Guadi.Common.Tools;
using Guadi.User.Models;
using Guadi.User.DAO;

namespace Guadi.User.BLL.Impl
{
    public abstract class UserServices:ServicesBase,IUserContract
    {
        #region 属性

        #region 保护属性

        /// <summary>
        /// 获取或设置 用户信息数据访问对象
        /// </summary>
        [Import]
        protected IUserRepository UserRepository { get; set; }

        #endregion

        #region 公共属性
        public IQueryable<Users> Users { get { return UserRepository.Entities; }}
        #endregion

        #endregion

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="loginInfo">登录信息</param>
        /// <returns>业务操作结果</returns>
        public virtual OperationResult Login(LoginInfo loginInfo)
        {
            Users user = UserRepository.Entities.SingleOrDefault(p => p.UserName == loginInfo.Account);
            if (user == null)
            {
                return new OperationResult(OperationResultType.QueryNull,"指定账户的用户不存在。");
            }
            if (user.Password != loginInfo.Password)
            {
                return new OperationResult(OperationResultType.Warning,"登录密码不正确。");
            }
            return new OperationResult(OperationResultType.Success,"登录成功。",user);
        }

    }
}
<file_sep>/Guadi.Web.Edit/Controllers/PartialController.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Guadi.Web.Edit.Controllers
{
    public class PartialController : Controller
    {
        public ActionResult Head()
        {
            return PartialView();
        }

        public ActionResult Left()
        {
            return PartialView();
        }

        public ActionResult Foot()
        {
            return PartialView();
        }

    }
}
<file_sep>/Guadi.Web.Edit/Controllers/UserManageController.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guadi.Web.Edit.Models;

namespace Guadi.Web.Edit.Controllers
{
    public class UserManageController : Controller
    {

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Add(UserModel model)
        {
            return View();
        }


        [HttpGet]
        public JsonResult GetUserList(int rows = 10, int page = 1)
        {

            Dictionary<object, object> data = new Dictionary<object, object>();
            data.Add("total",200);
            var list = new List<UserModel>();
            UserModel p = new UserModel()
                {
                    ContractEmail = "<EMAIL>",
                    ContractNumber = "18721856450",
                    Id = 1000,
                    Level = "金牌会员",
                    NickName = "水帘洞行者孙",
                    Status = "正常"
                };

            list.Add(p);
            list.Add(p);
            list.Add(p);
            list.Add(p);
            list.Add(p);
            list.Add(p);
            list.Add(p);
            list.Add(p);
            list.Add(p);
            list.Add(p);
            list.Add(p);
            list.Add(p);
            data.Add("rows",list);

            return Json(data, JsonRequestBehavior.AllowGet);
        }

    }
}
<file_sep>/Guadi.Product.BLL/IAreaContract.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Guadi.Product.Models;
using Guadi.Common.Tools;
using Guadi.Product.Models.ContractModels.Area;
using Guadi.Product.Models.DbModels.Area;

namespace Guadi.Product.BLL
{

    /// <summary>
    /// 地域模块核心业务契约
    /// </summary>
    public interface IAreaContract
    {
        #region 属性

        /// <summary>
        /// 获取  国际信息查询数据集
        /// </summary>
        IQueryable<Countrys> Countrys { get; }

        #endregion

        #region 公共方法

        /// <summary>
        /// 添加新的国家
        /// </summary>
        /// <returns></returns>
        OperationResult AddNewCountry(CountryEditModel model);

        #endregion
    }
}
<file_sep>/Guadi.Web.Edit/Controllers/ProductManageController.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guadi.Web.Edit.Models;

namespace Guadi.Web.Edit.Controllers
{
    public class ProductManageController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(ProductAddModel model)
        {
            return View();
        }
    }
}
<file_sep>/Guadi.Product.Models/ContractModels/Area/CountryEditModel.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Guadi.Product.Models.ContractModels.Area
{
    public class CountryEditModel
    {
        /// <summary>
        /// 名字
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 经度
        /// </summary>
        public double Longitude { get; set; }

        /// <summary>
        /// 纬度
        /// </summary>
        public double Latitude { get; set; }
    }
}
<file_sep>/Guadi.Common/IEntityMapper.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;

namespace Guadi.Common.Data
{

    /// <summary>
    /// 实体映射接口
    /// </summary>
    [InheritedExport]
    public interface  IEntityMapper
    {
        /// <summary>
        /// 将当前实体映射对象注册到当前访问数据访问上下文实体映射配置注册器中
        /// </summary>
        /// <param name="configurations"></param>
        void RegistTo(ConfigurationRegistrar configurations);

    }
}
<file_sep>/Guadi.Product.DAO/Area/ICountryRepository.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Guadi.Common.Data;
using Guadi.Product.Models;
using Guadi.Product.Models.DbModels.Area;

namespace Guadi.Product.DAO.Area
{

    /// <summary>
    ///仓储操作接口--国家信息
    /// </summary>
    public partial interface ICountryRepository:IRepository<Countrys,int>
    {}
}
<file_sep>/Guadi.Product.Models/DbModels/Area/Countrys.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using Guadi.Common.Data;

namespace Guadi.Product.Models.DbModels.Area
{
    /// <summary>
    /// 国家
    /// </summary>
    public class Countrys:DataBase<int>
    {
        /// <summary>
        /// 名称
        /// </summary>
        [Required]
        public string Name { get; set; }

        /// <summary>
        /// 经度
        /// </summary>
        [Required]
        public double Longitude { get; set; }

        /// <summary>
        /// 纬度
        /// </summary>
        [Required]
        public double Latitude { get; set; }
    }
}
<file_sep>/Guadi.Common/DbConnectionConfig.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Guadi.Common.Data
{
    /// <summary>
    /// 数据库连接串名称
    /// </summary>
    public sealed class DbConnectionConfig
    {
        /// <summary>
        /// 用户数据库连接串名称
        /// </summary>
        public static string UserDbConnectionString = "UserDbConnectionString";

        /// <summary>
        /// 产品数据库连接串名称
        /// </summary>
        public static string ProductDbConnectionString = "ProductDbConnectionString";

        /// <summary>
        /// 订单数据库连接串名称
        /// </summary>
        public static string OrderDbConnectionString = "OrderDbConnectionString";

        /// <summary>
        /// 基础数据库连接串名称
        /// </summary>
        public static string CommonDbConnectionString = "CommonDbConnectionString";
    }
}
<file_sep>/Guadi.Web.Edit/Models/UserModel.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Guadi.Web.Edit.Models
{
    public class UserModel
    {
        /// <summary>
        /// ID
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 昵称
        /// </summary>
        public string NickName { get; set; }

        /// <summary>
        /// 联系邮箱
        /// </summary>
        public string ContractEmail { get; set; }

        /// <summary>
        /// 联系电话
        /// </summary>
        public string ContractNumber { get; set; }

        /// <summary>
        /// 会员等级
        /// </summary>
        public string Level { get; set; }

        /// <summary>
        /// 会员状态
        /// </summary>
        public string Status { get; set; }

    }


    /// <summary>
    /// 
    /// </summary>
    public class UserListData
    {
        public int total { get; set; }

        public List<UserModel> rows { get; set; }
    }

}<file_sep>/Guadi.Web.Tool/Helper/Ioc/MefDependencySolver.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;


namespace Guadi.Web.Tool.Helper.Ioc
{
    public class MefDependencySolver:IDependencyResolver
    {
        private readonly ComposablePartCatalog _catalog;

        private const string MefContainerKey = "MefContainerKey";

        public MefDependencySolver(ComposablePartCatalog catalog)
        {
            _catalog = catalog;
        }

        public CompositionContainer Container
        {
            get
            {
                if (!HttpContext.Current.Items.Contains((MefContainerKey)))
                {
                    HttpContext.Current.Items.Add(MefContainerKey,new );
                }
            }
        }


    }

}
<file_sep>/Guadi.User.DAO/Impl/UserRepository.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Guadi.Common.Data;
using Guadi.User.Models;

namespace Guadi.User.DAO.Impl
{

    /// <summary>
    /// 仓储操作层实现--用户信息
    /// </summary>
    [Export(typeof(IUserRepository))]
    public partial class UserRepository:EfRepositoryBase<Users,long>,IUserRepository
    {
    }
}
<file_sep>/Guadi.Common/IUnitOfWorkContext.cs
﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq.Expressions;

namespace Guadi.Common.Data
{
    /// <summary>
    /// 数据单元操作接口
    /// </summary>
    public interface IUnitOfWorkContext:IUnitOfWork,IDisposable
    {
        /// <summary>
        /// 注册一个新的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="entity">要注册的对象</param>
        void RegisterNew<TEntity,TKey>(TEntity entity) where TEntity : DataBase<TKey>;

        /// <summary>
        /// 批量注册多个新的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="entities">要注册的对象集合</param>
        void RegisterNew<TEntity,TKey>(IEnumerable<TEntity> entities) where TEntity : DataBase<TKey>;

        /// <summary>
        /// 注册一个更改的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="entity">要注册的对象</param>
        void RegisterModified<TEntity,TKey>(TEntity entity) where TEntity : DataBase<TKey>;

        /// <summary>
        /// 使用指定的属性表达式指定注册更改的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="propertyExpression">属性表达式，包含要更新的实体属性</param>
        /// <param name="entity">附带新值的实体信息，必须包含主键</param>
        void RegisterModified<TEntity,TKey>(Expression<Func<TEntity, object>> propertyExpression, TEntity entity)
            where TEntity : DataBase<TKey>;

        /// <summary>
        /// 注册一个删除的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="entity">要注册的对象</param>
        void RegisterDeleted<TEntity,TKey>(TEntity entity) where TEntity : DataBase<TKey>;


        /// <summary>
        ///  批量注册多个删除的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="entities">要注册的对象集合</param>
        void RegisterDeleted<TEntity,TKey>(IEnumerable<TEntity> entities) where TEntity : DataBase<TKey>;
    }
}
<file_sep>/Guadi.Product.DAO/Area/Impl/CountryRepository.cs
﻿using System.ComponentModel.Composition;
using Guadi.Common.Data;
using Guadi.Product.Models.DbModels.Area;

namespace Guadi.Product.DAO.Area.Impl
{

    /// <summary>
    /// 仓储操作层实现
    /// </summary>
    [Export(typeof(ICountryRepository))]
    public partial class CountryRepository:EfRepositoryBase<Countrys,int>,ICountryRepository
    {}
}
<file_sep>/Guadi.Common.Tools/StringHelper.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace Guadi.Common.Tools
{
    /// <summary>
    /// 字符串辅助操作类
    /// </summary>
    public class StringHelper
    {
        /// <summary>
        /// 把对象转换成Json字符串表示形式
        /// </summary>
        /// <param name="jsonObject"></param>
        /// <returns></returns>
        public static string BuildJsonString(object jsonObject)
        {
            return JsonConvert.SerializeObject(jsonObject);
        }

        /// <summary>
        /// 大写字母
        /// </summary>
        protected static readonly char[] BigAlphabets =
            {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
                'V', 'W', 'X', 'Y', 'Z'
            };

        /// <summary>
        /// 小写字母
        /// </summary>
        protected static readonly char[] SmallAlphabets =
            {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
                'v', 'w', 'x', 'y', 'z'
            };

        /// <summary>
        /// 数字
        /// </summary>
        protected static readonly char[] Digitals =
            {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            };

        /// <summary>
        /// 生成指定长度指定类型的随机字符串(默认为大写字母随机串)
        /// </summary>
        /// <param name="type">1:大写字母，2：小写字母，3：数字，4：大写字母和小写字母，5：大写字母和数字，6：小写字母和数字，7：大小写字母和数字</param>
        /// <param name="length">随机字符创长度</param>
        /// <returns></returns>
        public static string GenerateRandomString(int length,int type=1)
        {
            if (length < 0) return "";
            StringBuilder randomStrB=new StringBuilder(length);
            Random rd=new Random(Guid.NewGuid().GetHashCode());
            char[] sourceStr = BigAlphabets;
            string bigAlphabets = BigAlphabets.ToString();
            string smaAlphabets = SmallAlphabets.ToString();
            string digitals = Digitals.ToString();
            switch (type)
            {
                case 1:
                    sourceStr = BigAlphabets;
                    break;
                case 2:
                    sourceStr = SmallAlphabets;
                    break;
                case 3:
                    sourceStr = Digitals;
                    break;
                case 4:
                    sourceStr = (bigAlphabets+smaAlphabets).ToArray();
                    break;
                case 5:
                    sourceStr = (bigAlphabets + digitals).ToArray();
                    break;
                case 6:
                    sourceStr = (smaAlphabets + digitals).ToArray();
                    break;
                case 7:
                    sourceStr = (bigAlphabets + digitals + smaAlphabets).ToArray();
                    break;
            }
            randomStrB.Append(sourceStr[rd.Next(0, sourceStr.Length)]);
            return randomStrB.ToString();
        }

    }
}
<file_sep>/Guadi.Product.BLL/Impl/AreaServices.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Guadi.Common.Data;
using Guadi.Product.Models;
using Guadi.Product.DAO.Area;
using Guadi.Common.Tools;
using Guadi.Product.Models.ContractModels.Area;
using Guadi.Product.Models.DbModels.Area;

namespace Guadi.Product.BLL.Impl
{

    /// <summary>
    /// 地域模块业务实现
    /// </summary>
    [Export(typeof(IAreaContract))]
    internal abstract class AreaServices:ServiceBase,IAreaContract
    {
        #region 属性

        #region 受保护的属性

        /// <summary>
        /// 获取或设置 国家数据访问对象
        /// </summary>
        [Import]
        protected ICountryRepository CountryRepository { get; set; }

        #endregion

        #region 公共属性

        public IQueryable<Countrys> Countrys {
            get { return CountryRepository.Entities; }
        }

        #endregion

        #endregion

        #region 方法
        /// <summary>
        /// 添加新的国家
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual OperationResult AddNewCountry(CountryEditModel model)
        {
            Countrys country =
                CountryRepository.Entities.SingleOrDefault(
                    p => p.Name.Equals(model.Name) && p.DataStatus == EnumDataStatus.Normal);
            if (country != null)
            {
                return new OperationResult(OperationResultType.Warning,"该国家已经存在。");
            }
            Countrys newModel=new Countrys()
                {
                    CreateTime = DateTime.Now,
                    DataStatus = EnumDataStatus.Normal,
                    Latitude = model.Latitude,
                    Longitude = model.Longitude,
                    Name = model.Name,
                    UpdateTime = DateTime.Now
                };
            CountryRepository.Insert(newModel);
            return new OperationResult(OperationResultType.Success,"添加国家成功。");
        }
        #endregion
    }
}
<file_sep>/Guadi.Common/EfDbContext.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.Common;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace Guadi.Common.Data
{

    /// <summary>
    /// EF数据访问上下文
    /// </summary>
    [Export("EF",typeof(DbContext))]
    public class EfDbContext:DbContext
    {
        public EfDbContext()
            : base("default")
        {
        }

        public EfDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
        }

        public EfDbContext(DbConnection existingConnection)
            : base(existingConnection, true)
        {
            
        }
    }
}
<file_sep>/Guadi.User.Models/Users.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Guadi.Common.Data;

namespace Guadi.User.Models
{

    public class Users:DataBase<long>
    {
        /// <summary>
        /// 登录名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
    }
}
<file_sep>/Guadi.Product.Web.Tests/UnitTest1.cs
﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Guadi.Product.Web.Tests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var controller =new  Guadi.Product.Web.Controllers.CountryController();
            var result = controller.Index();
            Assert.AreEqual("Index",result.ToString());

        }
    }
}
<file_sep>/Guadi.User.DAO/IUserRepository.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Guadi.Common.Data;
using Guadi.User.Models;

namespace Guadi.User.DAO
{

    /// <summary>
    /// 仓库操作接口-用户信息
    /// </summary>
    public partial interface IUserRepository:IRepository<Users,long>
    {
    }
}
<file_sep>/Guadi.Product.Web/Controllers/CountryController.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guadi.Common.Tools;
using Guadi.Product.Models.ContractModels.Area;
using Guadi.Product.BLL;

namespace Guadi.Product.Web.Controllers
{
    [Export]
    public class CountryController : Controller
    {
        #region 属性

        [Import]
        public IAreaContract AreaContract { get; set; }

        #endregion

        public ActionResult Index()
        {
            CountryEditModel model=new CountryEditModel()
                {
                    Latitude = 10.11,
                    Longitude = 11.1,
                    Name = "中国"
                };
            var result = AreaContract.AddNewCountry(model);
            return View();
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(CountryEditModel model)
        {
            var result = AreaContract.AddNewCountry(model);
            if(result.ResultType==OperationResultType.Success)
            return RedirectToAction("Index");
            else
            {
                return View();
            }
        }

    }
}
<file_sep>/Guadi.Common/UnitOfWorkContextBase.cs
﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Contexts;
using System.Text;
using Guadi.Common.Data.Extensions;
using Guadi.Common.Tools;

namespace Guadi.Common.Data
{

    /// <summary>
    /// 单元操作实现基类
    /// </summary>
    public abstract class UnitOfWorkContextBase:IUnitOfWorkContext
    {

        /// <summary>
        /// 获取 当前使用的数据访问上下文对象
        /// </summary>
        protected abstract DbContext Context { get; }

        /// <summary>
        /// 获取 当前单元操作是否已被提交
        /// </summary>
        public bool IsCommitted { get; private set; }

        /// <summary>
        /// 数据库上下文对象(对外)
        /// </summary>
        public DbContext DbContext {
            get { return Context; }
        }


        /// <summary>
        /// 提交当前单元操作的结果
        /// </summary>
        /// <param name="validateOnSaveEnabled">保存时是否自动验证跟踪实体</param>
        /// <returns></returns>
        public int Commit(bool validateOnSaveEnabled=true)
        {
            if (IsCommitted)
                return 0;
            try
            {
                int result = Context.SaveChanges(validateOnSaveEnabled);
                IsCommitted = true;
                return result;
            }
            catch (DbUpdateException ex)
            {
                if (ex.InnerException != null && ex.InnerException.InnerException is SqlException)
                {
                    SqlException sqlEx = ex.InnerException.InnerException as SqlException;
                    string msg = DataHelper.GetSqlExceptionMessage(sqlEx.Number);
                    throw PublicHelper.ThrowDataAccessException("提交数据更新时发生异常：" + msg, sqlEx);
                }
                throw;
            }
        }

        /// <summary>
        /// 把当前单元操作回滚成未提交状态
        /// </summary>
        public void Rollback()
        {
            IsCommitted = false;
        }

        public void Dispose()
        {
            if (!IsCommitted)
            {
                Commit();
            }
            Context.Dispose();
        }

        /// <summary>
        /// 为指定的类型返回 System.Data.Entity.DbSet，这将允许对上下文中的给定实体执行 CRUD 操作。
        /// </summary>
        /// <typeparam name="TEntity">应为其返回一个集的实体类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <returns>给定实体类型的 System.Data.Entity.DbSet 实例</returns>
        public DbSet<TEntity> Set<TEntity,TKey>() where TEntity : DataBase<TKey>
        {
            return Context.Set<TEntity>();
        }

        /// <summary>
        /// 注册一个新的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="entity">要注册的对象</param>
        public void RegisterNew<TEntity,TKey>(TEntity entity) where TEntity : DataBase<TKey>
        {
            EntityState state = Context.Entry(entity).State;
            if (state == EntityState.Detached)
            {
                Context.Entry(entity).State=EntityState.Added;
            }
            IsCommitted = false;
        }

        /// <summary>
        /// 批量注册多个新的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="entities">要注册的对象集合</param>
        public void RegisterNew<TEntity,TKey>(IEnumerable<TEntity> entities) where TEntity : DataBase<TKey>
        {
            try
            {
                Context.Configuration.AutoDetectChangesEnabled = false; //关闭自动更新
                foreach (var entity in entities)
                {
                    RegisterNew<TEntity,TKey>(entity);
                }

            }
            finally
            {
                Context.Configuration.AutoDetectChangesEnabled = true;
            }

        }

        /// <summary>
        /// 注册一个更改的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="entity">要注册的对象</param>
        public void RegisterModified<TEntity,TKey>(TEntity entity) where TEntity : DataBase<TKey>
        {
            Context.Update<TEntity,TKey>(entity);
            IsCommitted = false;
        }

        /// <summary>
        /// 使用指定的属性表达式指定注册更改的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="propertyExpression">属性表达式，包含要更新的实体属性</param>
        /// <param name="entity">附带新值的实体信息，必须包含主键</param>
        public void RegisterModified<TEntity, TKey>(Expression<Func<TEntity, object>> propertyExpression, TEntity entity)
            where TEntity : DataBase<TKey>
        {
            Context.Update<TEntity,TKey>(propertyExpression,entity);
            IsCommitted = false;
        }

        /// <summary>
        /// 注册一个删除的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的类型</typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="entity">要注册的对象</param>
        public void RegisterDeleted<TEntity, TKey>(TEntity entity) where TEntity : DataBase<TKey>
        {
            Context.Entry(entity).State = EntityState.Deleted;
            IsCommitted = false;
        }

        /// <summary>
        /// 批量注册多个删除的对象到仓储上下文中
        /// </summary>
        /// <typeparam name="TEntity">要注册的对象</typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="entities">要注册的对象集合</param>
        public void RegisterDeleted<TEntity,TKey>(IEnumerable<TEntity> entities) where TEntity : DataBase<TKey>
        {
            try
            {
                Context.Configuration.AutoDetectChangesEnabled = false;
                foreach (var entity in entities)
                {
                    RegisterDeleted<TEntity,TKey>(entity);
                }
            }
            finally
            {
                Context.Configuration.AutoDetectChangesEnabled = true;
            }
        }
    }
}
