this.Blog.Find(entry => entry.IsHelpful);
 Friday, May 30, 2008
New release of Miado

This new release contains significant changes.  First and foremost, all the implementation classes have had their public scope removed, so you will now have to program exclusively against the interfaces.  I have also moved the IMiadoDao interface to IMiadoRepository in order to encourage composition and not force an inheritance hierarchy.  Combined with that change, I also moved the functionality from SimpleSql into IMiadoRepository, as well as making that interface the point where you access an IDbStatement, making the IMiadoRepository the main worker module of Miado.   Another change is that you need to use the MiadoRepositoryFactory to create an instance of the IMiadoRepository.

Enough with the talk, let's see some code...

public AdddressDao 
{
private static readonly string INSERT_ADDRESS_SQL =
"insert into Address(Address1, Address2, City, State, ZipCode) " +
"values(@Addr1, @Addr2, @City, @State, @ZipCode)";

private static readonly string SELECT_ADDRESS_SQL =
"select AddressId, Address1, Address2, City, " +
"State, ZipCode " +
"from Address ";

private static readonly string SELECT_ADDRESS_BY_ID =
SELECT_ADDRESS_SQL + "where AddressId = @Id ";

private static readonly string SELECT_ADDRESS_BY_ZIP_CODE =
SELECT_ADDRESS_SQL + "where ZipCode = @ZipCode ";

private static readonly string SELECT_COUNT_BY_STATE =
"select count(*) from Address where State = @State ";

// uses MiadoRepositoryFactory.CreateMiadoRepository()
public AddressDao()
: this(MiadoRepositoryFactory.CreateMiadoRepository(
SqlClientFactory.Instance,
ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString)) {}

public AdddressDao(IMiadoRepository repository)
{
this.MiadoRepository = repository;
}

protected IMiadoRepository MiadoRepository
{
get;
set;
}

public void SaveAddress(Address addr)
{
// uses SQL right in IMiadoRepository
this.MiadoRepository
.ExecuteNonQuery(INSERT_ADDRESS_SQL,
addr.Address1,
addr.Address2,
addr.City,
addr.State,
addr.ZipCode)
}

public ICollection<Address> FindAddressesByZipCode(string zipCode)
{
// build IDbStatement and do the mapping in a lambda expression
ICollection<Address> addresses =
this.MiadoRepository
.CreateDbStatement(SELECT_ADDRESS_BY_ZIP_CODE)
.AddParameter("ZipCode", zipCode)
.QueryForResults<Address>(
row => new Address()
{
AddressId = row.GetValue<int>("AddressId"),
Address1 = row.GetValue<string>("Address1"),
Address2 = row.GetValue<string>("Address2"),
City = row.GetValue<string>("City"),
State = row.GetValue<string>("State"),
ZipCode = row.GetValue<string>("ZipCode")
});
return addresses;
}

public Address LoadAddress(int addrId)
{
// uses delegate method to do the mapping
return
this.MiadoRepository
.CreateDbStatement(SELECT_ADDRESS_BY_ID)
.AddParameter("Id", addrId)
.QueryForOne<Address>(CreateAddress);
}

public int FindCountByState(string state)
{
return
this.MiadoRepository
.CreateDbStatement(SELECT_COUNT_BY_STATE)
.AddParameter("State", state)
.QueryForOne<int>(row => row.GetValue<int>(0));
}

private static Address CreateAddress(IResultSetRow row)
{
return
new Address()
{
AddressId = row.GetValue<int>("AddressId"),
Address1 = row.GetValue<string>("Address1"),
Address2 = row.GetValue<string>("Address2"),
City = row.GetValue<string>("City"),
State = row.GetValue<string>("State"),
ZipCode = row.GetValue<string>("ZipCode")
};
}

}

Kick it on DotNetKicks.com
Friday, May 30, 2008 10:57:03 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  .Net | C# | Miado