• Repository Factory介绍


          Repository Factory是的Micrsoft Patterns and practies Team在前几年出的一个代码生成组件,主要生成基于Enlib DAAB的DataAccessLayer和Business Entity. 主要适用于VS2005,VS2008。从官方下载RepositoryFactoryForVS2008.msi,安装后,在Tools菜单将有一个选项Guidance Package Manager,看下面的图片:

    1

    打开后,Check Repository Factory

    2

    在Solution Explorer中通过右键选择各自的Project的Responsibilites:

    3 

    打开DataBase

    4

    选择你要生成Tables:

    5 

    可以选择并EDIT需要映射的SP

    7

    最后生成的Enity class:

       1:      [Serializable]
       2:      public partial class Categories
       3:      {
       4:          public Categories()
       5:          {
       6:          }
       7:   
       8:          public Categories(System.Int32 categoryID, System.String categoryName, System.String description, System.Byte[] picture)
       9:          {
      10:              this.categoryIDField = categoryID;
      11:              this.categoryNameField = categoryName;
      12:              this.descriptionField = description;
      13:              this.pictureField = picture;
      14:          }
      15:   
      16:          private System.Int32 categoryIDField;
      17:   
      18:          public System.Int32 CategoryID
      19:          {
      20:              get { return this.categoryIDField; }
      21:              set { this.categoryIDField = value; }
      22:          }
      23:   
      24:          private System.String categoryNameField;
      25:   
      26:          public System.String CategoryName
      27:          {
      28:              get { return this.categoryNameField; }
      29:              set { this.categoryNameField = value; }
      30:          }
      31:   
      32:          private System.String descriptionField;
      33:   
      34:          public System.String Description
      35:          {
      36:              get { return this.descriptionField; }
      37:              set { this.descriptionField = value; }
      38:          }
      39:   
      40:          private System.Byte[] pictureField;
      41:   
      42:          public System.Byte[] Picture
      43:          {
      44:              get { return this.pictureField; }
      45:              set { this.pictureField = value; }
      46:          }
      47:   
      48:      }

    Interface:

       1:      /// <summary>
       2:      /// Repository that lets you find Categories in the database.
       3:      /// </summary>
       4:      public interface ICategoriesRepository
       5:      {
       6:   
       7:          List<Categories> GetAllFromCategories();
       8:   
       9:          void Add(Categories categories);
      10:   
      11:          void Remove(System.Int32 categoryIDField);
      12:   
      13:          void Save(Categories categories);
      14:   
      15:      }

    具体的Repository factory class:

       1:  using System;
       2:  using System.Collections.Generic;
       3:  using System.Data;
       4:  using System.Data.Common;
       5:  using System.Text;
       6:  using Microsoft.Practices.EnterpriseLibrary.Data;
       7:  using RepositoryFactoryTest;
       8:  using RepositoryFactoryTest.BusinessEntities;
       9:  using RepositoryFactoryTest.Interfaces;
      10:  using System.Data.SqlClient;
      11:  using System.Diagnostics;
      12:  using Microsoft.Practices.Repository;
      13:  using Microsoft.Practices.Repository.SQLServer;
      14:   
      15:  namespace RepositoryFactoryTest.CategoriesRepositoryArtifacts
      16:  {
      17:      /// <summary>
      18:      /// Repository that lets you find Categories in the database.
      19:      /// </summary>
      20:      public sealed class CategoriesRepository : Repository<Categories>, ICategoriesRepository
      21:      {
      22:          public CategoriesRepository(string databaseName)
      23:              : base(databaseName)
      24:          {
      25:          }
      26:   
      27:          public CategoriesRepository()
      28:              : base()
      29:          {
      30:          }
      31:   
      32:   
      33:          public List<Categories> GetAllFromCategories()
      34:          {
      35:              ISelectionFactory<NullableIdentity> selectionFactory = new GetAllFromCategoriesSelectionFactory();
      36:   
      37:              try
      38:              {
      39:                  NullableIdentity nullableIdentity = new NullableIdentity();
      40:                  return base.Find(selectionFactory, new GetAllFromCategoriesFactory(), nullableIdentity);
      41:              }
      42:              catch (SqlException ex)
      43:              {
      44:                  HandleSqlException(ex, selectionFactory);
      45:              }
      46:   
      47:              return new List<Categories>();
      48:          }
      49:   
      50:          public void Add(Categories categories)
      51:          {
      52:              CategoriesInsertFactory insertFactory = new CategoriesInsertFactory();
      53:              try
      54:              {
      55:                  base.Add(insertFactory, categories);
      56:              }
      57:              catch (SqlException ex)
      58:              {
      59:                  HandleSqlException(ex, insertFactory);
      60:              }
      61:          }
      62:   
      63:          public void Remove(System.Int32 categoryID)
      64:          {
      65:              IDeleteFactory<System.Int32> deleteFactory = new CategoriesDeleteFactory();
      66:   
      67:              try
      68:              {
      69:                  base.Remove(deleteFactory, categoryID);
      70:              }
      71:              catch (SqlException ex)
      72:              {
      73:                  HandleSqlException(ex, deleteFactory);
      74:              }
      75:          }
      76:   
      77:   
      78:          public void Save(Categories categories)
      79:          {
      80:              CategoriesUpdateFactory updateFactory = new CategoriesUpdateFactory();
      81:              try
      82:              {
      83:                  base.Save(updateFactory, categories);
      84:              }
      85:              catch (SqlException ex)
      86:              {
      87:                  HandleSqlException(ex, updateFactory);
      88:              }
      89:          }
      90:          private void HandleSqlException(SqlException ex, IDbToBusinessEntityNameMapper mapper)
      91:          {
      92:              if (ex.Number == ErrorCodes.SqlUserRaisedError)
      93:              {
      94:                  switch (ex.State)
      95:                  {
      96:                      case ErrorCodes.ValidationError:
      97:                          string[] messageParts = ex.Errors[0].Message.Split(':');
      98:                          throw new RepositoryValidationException(
      99:                              mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]),
     100:                              messageParts[1], ex);
     101:   
     102:                      case ErrorCodes.ConcurrencyViolationError:
     103:                          throw new ConcurrencyViolationException(ex.Message, ex);
     104:   
     105:                  }
     106:              }
     107:   
     108:              throw new RepositoryFailureException(ex);
     109:          }
     110:   
     111:   
     112:      }
     113:  }
     114:   

    来看下关键的Interface:

       1:  namespace Microsoft.Practices.Repository
       2:  {
       3:      public class Repository<TDomainObject>
       4:      {
       5:          public Repository();
       6:          public Repository(string databaseName);
       7:   
       8:          protected Database Database { get; set; }
       9:   
      10:          public void Add(IInsertFactory<TDomainObject> insertFactory, TDomainObject domainObj);
      11:          public List<TDomainObject> Find<TIdentity>(ISelectionFactory<TIdentity> selectionFactory, IDomainObjectsFactory<TDomainObject> domainObjectFactory, TIdentity identity);
      12:          public TDomainObject FindOne<TIdentity>(ISelectionFactory<TIdentity> selectionFactory, ISimpleDomainObjectFactory<TDomainObject> domainObjectFactory, TIdentity identity);
      13:          public TDomainObject FindOneComplex<TIdentity>(ISelectionFactory<TIdentity> selectionFactory, IComplexDomainObjectFactory<TDomainObject> domainObjectFactory, TIdentity identity);
      14:          public TDomainObject FindOneWithSqlCache<TIdentity>(ISelectionFactory<TIdentity> selectionFactory, IDomainObjectFactory<TDomainObject> domainObjectFactory, TIdentity identity, string cacheKey, TimeSpan expiry);
      15:          public List<TDomainObject> FindWithSqlCache<TIdentity>(ISelectionFactory<TIdentity> selectionFactory, IDomainObjectFactory<TDomainObject> domainObjectFactory, TIdentity identity, string cacheKey, TimeSpan expiry);
      16:          public void Remove<TIdentityObject>(IDeleteFactory<TIdentityObject> deleteFactory, TIdentityObject identityObj);
      17:          public void Save(IUpdateFactory<TDomainObject> updateFactory, TDomainObject domainObj);
      18:      }
      19:  }

    这一套工具目前生成的Code是基于Data Access Application Block 3.1的。不过还好有SourceCode,你可以按照你的需要修改。

    感觉有点像CodeSmith的,但其中n-tiers template生成的代码要比这个复杂。这个只需要VisualStudio,下一代的代码生成应该是基于T4 template了。

    有时间我将介绍基于EF4的DAL的Repository的代码生成。

    希望对您的开发有帮助。


    作者:Petter Liu
    出处:http://www.cnblogs.com/wintersun/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    该文章也同时发布在我的独立博客中-Petter Liu Blog

  • 相关阅读:
    关于selenium中的sendKeys()隔几秒发送一个字符
    C#使用.net.mail配置163邮箱报错:不允许使用邮箱名称。 服务器响应为:authentication is required,smtp9,DcCowABHK4UYE11W2k6fAQ--.52196S2 1448940312
    Git一个本地仓库同时推送到多个远程仓库
    MySQL中的字符数据存储
    在IIS中启用net.tcp传输协议
    MS CRM 2016 二次开发知识点
    微软 CRM 2016 自定义视图顶部按钮
    CodeSmith7.0.2连接Oracle10.2
    使用Entity framework框架执行存储过程
    SQL建表公共字段脚本
  • 原文地址:https://www.cnblogs.com/wintersun/p/1784856.html
Copyright © 2020-2023  润新知