更新时间:2022-06-28 02:34:52
public interface IDimCustomerRepository { List<DimCustomer> GetAllFromDimCustomer(); void Add(DimCustomer dimCustomer); void Remove(System.Int32 customerKey); void Save(DimCustomer dimCustomer); }
public class DimCustomerRepository : Repository<DimCustomer>, IDimCustomerRepository { public DimCustomerRepository(string databaseName) : base(databaseName) { } public DimCustomerRepository() : base() { } public List<DimCustomer> GetAllFromDimCustomer() { ISelectionFactory<NullableIdentity> selectionFactory = new GetAllFromDimCustomerSelectionFactory(); try { NullableIdentity nullableIdentity = new NullableIdentity(); return base.Find(selectionFactory, new GetAllFromDimCustomerFactory(), nullableIdentity); } catch (SqlException ex) { HandleSqlException(ex, selectionFactory); } return new List<DimCustomer>(); } public void Add(DimCustomer dimCustomer) { DimCustomerInsertFactory insertFactory = new DimCustomerInsertFactory(); try { base.Add(insertFactory, dimCustomer); } catch (SqlException ex) { HandleSqlException(ex, insertFactory); } } public void Remove(System.Int32 customerKey) { IDeleteFactory<System.Int32> deleteFactory = new DimCustomerDeleteFactory(); try { base.Remove(deleteFactory, customerKey); } catch (SqlException ex) { HandleSqlException(ex, deleteFactory); } } public void Save(DimCustomer dimCustomer) { DimCustomerUpdateFactory updateFactory = new DimCustomerUpdateFactory(); try { base.Save(updateFactory, dimCustomer); } catch (SqlException ex) { HandleSqlException(ex, updateFactory); } } private void HandleSqlException(SqlException ex, IDbToBusinessEntityNameMapper mapper) { if (ex.Number == ErrorCodes.SqlUserRaisedError) { switch (ex.State) { case ErrorCodes.ValidationError: string[] messageParts = ex.Errors[0].Message.Split(':'); throw new RepositoryValidationException( mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]), messageParts[1], ex); case ErrorCodes.ConcurrencyViolationError: throw new ConcurrencyViolationException(ex.Message, ex); } } throw new RepositoryFailureException(ex); } }
internal class DimCustomerInsertFactory : IDbToBusinessEntityNameMapper, IInsertFactory<DimCustomer> { /// <summary> /// Creates the DimCustomerInsertFactory to build an insert statement for /// the given DimCustomer object. /// </summary> /// <param name="DimCustomer">New DimCustomer to insert into the database.</param> public DimCustomerInsertFactory() { } #region IInsertFactory<DimCustomer> Members public DbCommand ConstructInsertCommand(Database db, DimCustomer dimCustomer) { DbCommand command = db.GetStoredProcCommand("dbo.InsertDimCustomer"); if (dimCustomer.AddressLine1 != null) { db.AddInParameter(command, "addressLine1", DbType.String, dimCustomer.AddressLine1); } if (dimCustomer.AddressLine2 != null) { db.AddInParameter(command, "addressLine2", DbType.String, dimCustomer.AddressLine2); } if (dimCustomer.BirthDate != null) { db.AddInParameter(command, "birthDate", DbType.DateTime, dimCustomer.BirthDate); } if (dimCustomer.CommuteDistance != null) { db.AddInParameter(command, "commuteDistance", DbType.String, dimCustomer.CommuteDistance); } if (dimCustomer.CustomerAlternateKey != null) { db.AddInParameter(command, "customerAlternateKey", DbType.String, dimCustomer.CustomerAlternateKey); } db.AddOutParameter(command, "customerKey", DbType.Int32, 4); if (dimCustomer.DateFirstPurchase != null) { db.AddInParameter(command, "dateFirstPurchase", DbType.DateTime, dimCustomer.DateFirstPurchase); } if (dimCustomer.EmailAddress != null) { db.AddInParameter(command, "emailAddress", DbType.String, dimCustomer.EmailAddress); } if (dimCustomer.EnglishEducation != null) { db.AddInParameter(command, "englishEducation", DbType.String, dimCustomer.EnglishEducation); } if (dimCustomer.EnglishOccupation != null) { db.AddInParameter(command, "englishOccupation", DbType.String, dimCustomer.EnglishOccupation); } if (dimCustomer.FirstName != null) { db.AddInParameter(command, "firstName", DbType.String, dimCustomer.FirstName); } if (dimCustomer.FrenchEducation != null) { db.AddInParameter(command, "frenchEducation", DbType.String, dimCustomer.FrenchEducation); } if (dimCustomer.FrenchOccupation != null) { db.AddInParameter(command, "frenchOccupation", DbType.String, dimCustomer.FrenchOccupation); } if (dimCustomer.Gender != null) { db.AddInParameter(command, "gender", DbType.String, dimCustomer.Gender); } if (dimCustomer.GeographyKey != null) { db.AddInParameter(command, "geographyKey", DbType.Int32, dimCustomer.GeographyKey); } if (dimCustomer.HouseOwnerFlag != null) { db.AddInParameter(command, "houseOwnerFlag", DbType.String, dimCustomer.HouseOwnerFlag); } if (dimCustomer.LastName != null) { db.AddInParameter(command, "lastName", DbType.String, dimCustomer.LastName); } if (dimCustomer.MaritalStatus != null) { db.AddInParameter(command, "maritalStatus", DbType.String, dimCustomer.MaritalStatus); } if (dimCustomer.MiddleName != null) { db.AddInParameter(command, "middleName", DbType.String, dimCustomer.MiddleName); } if (dimCustomer.NameStyle != null) { db.AddInParameter(command, "nameStyle", DbType.Boolean, dimCustomer.NameStyle); } if (dimCustomer.NumberCarsOwned != null) { db.AddInParameter(command, "numberCarsOwned", DbType.Byte, dimCustomer.NumberCarsOwned); } if (dimCustomer.NumberChildrenAtHome != null) { db.AddInParameter(command, "numberChildrenAtHome", DbType.Byte, dimCustomer.NumberChildrenAtHome); }
return command; } public void SetNewID(Database db, DbCommand command, DimCustomer dimCustomer) { System.Int32 id1 = (System.Int32)(db.GetParameterValue(command, "customerKey")); dimCustomer.CustomerKey = id1; } #endregion #region IDbToBusinessEntityNameMapper Members public string MapDbParameterToBusinessEntityProperty(string dbParameter) { switch (dbParameter) { case "addressLine1": return "AddressLine1"; case "addressLine2": return "AddressLine2"; case "birthDate": return "BirthDate"; case "commuteDistance": return "CommuteDistance"; case "customerAlternateKey": return "CustomerAlternateKey"; case "dateFirstPurchase": return "DateFirstPurchase"; case "emailAddress": return "EmailAddress";
default: throw new RepositoryInvalidParameterException(dbParameter); } } #endregion }
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="repositoryFactory" type="Microsoft.Practices.Repository.Configuration.RepositoryFactorySection, Microsoft.Practices.Repository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> </configSections> <connectionStrings> <add name="RFConnectionString" connectionString="Data Source=Esint-lhj\Sql2005;Initial Catalog=AdventureWorksDW;Persist Security Info=True;User ID=sa;Password=sql2005" providerName="System.Data.SqlClient" /> </connectionStrings> <repositoryFactory> <repositories> <add interfaceType="RepositoryFactoryDemo2.IDimEmployeeRepository, RepositoryFactoryDemo2" repositoryType="RepositoryFactoryDemo2.DimEmployeeRepositoryArtifacts.DimEmployeeRepository, RepositoryFactoryDemo2" /> <add interfaceType="RepositoryFactoryDemo2.IDimCustomerRepository, RepositoryFactoryDemo2" repositoryType="RepositoryFactoryDemo2.DimCustomerRepositoryArtifacts.DimCustomerRepository, RepositoryFactoryDemo2" /> </repositories> </repositoryFactory> </configuration>
class Program { static void Main(string[] args) { IDimEmployeeRepository repository = RepositoryFactory.Create<IDimEmployeeRepository>(); List<DimEmployee> employees = repository.GetAllFromDimEmployee(); } }
private System.String _classField; public System.String Class { get { return this._classField; } set { this._classField = value; } } private System.String _colorField; public System.String Color { get { return this._colorField; } set { this._colorField = value; } }