-
-
Save anonymous/094c658e69d704e79a94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Business | |
{ | |
public List<PartnerTypeDTO> PartnerTypes { get; set; } | |
public void Main() | |
{ | |
LoadSettings(); | |
DoMore(); | |
} | |
public void LoadSettings() | |
{ | |
PartnerTypes = GetPartnerTypes(); | |
} | |
public List<PartnerTypeDTO> GetPartnerTypes() | |
{ | |
var repo = new PartnerTypeRepository(); | |
var m = new PartnerType(repo); | |
var list = m.GetAll(); | |
if (!list.Any()) throw new ApplicationException("No partner types found"); | |
return list.ToList(); | |
} | |
public void DoMore() | |
{ | |
// ... do stuff with loaded settings | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Business.Models | |
{ | |
public abstract class ModelBase<T> where T : class | |
{ | |
public readonly IRepository<T> repo; | |
private IList<T> all; | |
public ModelBase() | |
{ | |
} | |
public ModelBase(IRepository<T> repo) | |
{ | |
this.repo = repo; | |
} | |
/// <summary> | |
/// Sets local all for re-use as well as returns it for assignment | |
/// </summary> | |
/// <returns></returns> | |
public virtual IList<T> GetAll() | |
{ | |
all = repo.GetAll(); | |
return all; | |
} | |
public virtual IList<T> GetWhere(Func<T, bool> predicate) | |
{ | |
if (all == null) GetAll(); | |
return all.Where(predicate).ToList(); | |
} | |
} | |
public class PartnerType : ModelBase<PartnerTypeDTO> | |
{ | |
public PartnerTypeDTO Data { get; set; } | |
public PartnerType(IRepository<PartnerTypeDTO> repo) | |
: base(repo) | |
{ | |
Data = new PartnerTypeDTO(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Data | |
{ | |
public interface IRepository<T> | |
{ | |
IList<T> GetAll(); | |
IList<T> GetWhere(Func<T, bool> predicate); | |
} | |
public class RepositoryBase<T> : IRepository<T> | |
{ | |
public virtual IList<T> GetAll() | |
{ | |
throw new NotImplementedException(); | |
} | |
public virtual IList<T> GetWhere(Func<T, bool> predicate) | |
{ | |
var all = GetAll(); | |
return all.Where(predicate).ToList(); | |
} | |
} | |
public class PartnerTypeRepository : RepositoryBase<PartnerTypeDTO> | |
{ | |
private readonly string _connectionString; | |
public PartnerTypeRepository() | |
{ | |
_connectionString = ConfigurationManager.ConnectionStrings["MYDB"].ConnectionString; | |
} | |
public override IList<PartnerTypeDTO> GetAll() | |
{ | |
List<PartnerTypeDTO> partnerTypes = new List<PartnerTypeDTO>(); | |
using (SqlConnection cn = new SqlConnection(_connectionString)) | |
{ | |
using (SqlCommand cmd = new SqlCommand("GetPartnerTypes", cn) { CommandType = CommandType.StoredProcedure }) | |
{ | |
cn.Open(); | |
using (SqlDataReader rdr = cmd.ExecuteReader()) | |
{ | |
while (rdr.Read()) | |
{ | |
partnerTypes.Add(new PartnerTypeDTO() | |
{ | |
Id = rdr["PARTNER_TYPE_ID"].To<string>(), | |
Name = rdr["PARTNER_TYPE_NM"].To<string>() | |
}); | |
} | |
} | |
} | |
} | |
return partnerTypes; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Test | |
{ | |
[TestClass] | |
public class PartnerTypeTests | |
{ | |
private IList<PartnerDTO> partners; | |
[TestInitialize] | |
public void Setup() | |
{ | |
partnersNone = null; | |
} | |
[TestMethod] | |
[ExpectedException(typeof(ApplicationException))] | |
public void GetAll_NoPartnerTypess_ThrowsException() | |
{ | |
// Arrange | |
var repo = new Mock<IRepository<PartnerTypeDTO>>(); | |
repo.Setup(x => x.GetAll()).Returns(partners); | |
var m = new PartnerType(repo.Object); | |
// Act | |
var got = m.GetAll(); | |
// Assert | |
repo.VerifyAll(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment