Writing a generic InsertData method using Dapper.Contrib and InsertAsync - dapper

I am fairly new to C# and Dapper. I am trying to write a generic Insert Data method for my project. I come from a Delphi environment so I am still finding my way around C#.
Dapper seems fairly straight forward to use but I am experiencing some challenges. I have tried every which way to get the syntax right with the following code but have been unsuccessful.
Issues seem to be around the T (I still quite don't understand what T is) and all the combinations I have tried don't work.
public async Task<int> InsertData<T>(T list)
{
string connectionString = _config.GetConnectionString(ConnectionStringName);
using (IDbConnection connection = new SqlConnection(connectionString))
{
return await connection.InsertAsync<int>(list);
}
}
The following code does work, so where am I going wrong?
public async Task SaveData<T>(string sql, T parameters)
{
string connectionString = _config.GetConnectionString(ConnectionStringName);
using (IDbConnection connection = new SqlConnection(connectionString))
{
await connection.ExecuteAsync(sql, parameters);
}
}

Your second code (await connection.ExecuteAsync(sql, parameters);) works because you are simply executing your hand written SQL statement. The method ExecuteAsync belongs to Dapper; NOT Dapper.Contrib. The generic T is used for parameters, not for the object you are trying to insert.
With your first code (return await connection.InsertAsync<int>(list);), you are actually using Dapper.Contrib; you are not writing the SQL statement by hand. Dapper.Contrib generates it for you.
Your following code seems the problem:
return await connection.InsertAsync<int>(list);
You are passing generic parameter <int> to the method which does not make sense.
I have not tested this but I hope changing that line to below one should work:
return await connection.InsertAsync<T>(list);
Also, you have to make sure the generic type T is class by adding where T : class to it.
Following generic method should serve your purpose; you need to convert it to async to match with your current code:
public void InsertData<T>(T entity) where T : class
{
string connectionString = ....;
using(IDbConnection connection = new SqlConnection(connectionString))
{
long result = connection.Insert<T>(entity);
}
}
I did not understood few other parts in your code. Say InsertData<T>(T list). What is list? Is it as single object or list of objects? If it is list of objects, List<T> list makes more sense. If it is single object, better you rename list to actual object name, say T customer/entity/poco etc.

Related

jdbcTemplate and pagination

I am trying to modify parts of some pre-existing code to return a Page and use Pageable. However I am at a loss as to how to approach this. From the little I found I need to have "Limit" and "Offset", but am using SQL Server which doesn't recognize the syntax.
public List<TotalForAllDTO> findAll() {
String query = queryProps.getProperty("findAll");
return jdbcTemplate.query(query, new Object[]{}, new TotalForAllDTOMapper());
Any guidance would be very helpful.

adding new methods to LINQ to Entities

Is there any way to define the SQL conversion component for additional functions to Linq2Entities.
For example:
myQuery.Where(entity => entity.Contains('foo', SearchFlags.All))
Ideally I am looking for something that doesn't require editing and building a new version the EntityFramework.dll directly. Is there any way to allow extension methods to entity framework that can support SQL generation.
So far I have a template which would represent the method I need to replace for LINQ to Entities:
public static bool Contains(this object source, string searchTerms, SearchFlags flags)
{
return true;
}
Of course this causes the error:
LINQ to Entities does not recognize the method 'Boolean
CONTAINS(System.Object, System.String, SearchFlags)' method, and this method
cannot be translated into a store expression.
To be clear, I don't want to do:
myQuery.AsEnumerable().Where(entity => entity.Contains('foo', SearchFlags.All))
Because I want to be able to execute code in SQL space and not return all the entities manually.
I also cannot use the .ToString() of the IQueryable and execute it manually because I need Entity Framework to populate the objects from several .Include joins.
I don't understand your Q clearly. However if your problem is that you can't use your own methods or other linq to objects method, just use .AsEnumerable() and do your other jobs through linq to objects, not L2E:
myQuery.AsEnumerable().Where(entity => entity.Contains('foo', SearchFlags.All))
And if you need to use your myQuery several times somewhere else, first load it to memory, then use it as many as you want:
var myQuery = from e in context.myEntities
select d;
myQuery.Load();
// ...
var myOtherQuery = from d in context.myEntities.Local
select d;
// Now any L2O method is supported...
I ended up doing the following (which works but is very far from perfect):
All my entities inherit from an IEntity which defines long Id { get; set; }
I then added a redundant restriction
context.myEntities.Where(entity => entity.Id != 0) this is
redundant since the identity starts at 1, but Linq2Entities doesn't
know that.
I then call .ToString() on the IQueryable after I have done all
my other queries, since it is of type DBQuery<Entity> it returns
the SQL Command Text, I do a simple replace with my query restriction.
In order to get all the .Include(...) to work I actually execute
two different sql commands. There is no other more pretty way to tap into this because of query execution plan caching causes issues otherwise (even when disabled).
As a result my code looks like this:
public IQueryable<IEntity> MyNewFunction(IQueryable<IEntity> myQueryable, string queryRestriction)
{
string rawSQL = myQueryable.Select(entity => entity.Id).ToString().Replace("[Extent1].Id <> 0", queryRestriction);
List<long> ids = // now execute rawSQL, get the list of ids;
return myQuerable.Where(entity => ids.Contains(entity.Id));
}
In short, other than manually executing the SQL or running a similar SQL command and appending the restriction using the existing commands the only way to write your own methods to Linq-to-Entities is to manually alter and build your own EntityFramework.dll from the EF6 source.

New to AutoFixture trying to get my head around it and I can't see it helping me

Currently, I'm using custom made fake objects that behind the scenes use NSubstitute which creates the actual objects but it's becoming very hard to maintain as the project grows, so I'm trying to find alternatives and I'm hoping that AutoFixture is the right tool for the job.
I read the documentation and I'm struggling because there's very little to no documentation and I read most of the blog posts by Mark Seemann including the CheatSheet.
One of the things that I'm having hard time to grasp is how to create an object with a constructor that have parameters, in my case I need to pass argument to CsEmbeddedRazorViewEngine as well as HttpRequestBase to ControllerContext.
The way I see it is that I need to create a fake objects and finally create a customization object that injects them to
I also looked into NBuilder it seems slightly more trivial to pass arguments there but I've heard good things about AutoFixture and I would like to give it a try. :)
I'm trying to reduce the amount of fake objects I have so here is a real test, how can I do the same thing with AutoFixture?
[Theory,
InlineData("en-US"),
InlineData("en-us"),
InlineData("en")]
public void Should_return_the_default_path_of_the_view_for_enUS(string language)
{
// Arrange
const string EXPECTED_VIEW_PATH = "~/MyAssemblyName/Views/Home/Index.cshtml";
CsEmbeddedRazorViewEngine engine = CsEmbeddedRazorViewEngineFactory.Create(ASSEMBLY_NAME, VIEW_PATH, string.Empty);
string[] userLanguage = { language };
HttpRequestBase request = FakeHttpRequestFactory.Create(userLanguage);
ControllerContext controllerContext = FakeControllerContextFactory.Create(request);
// Act
ViewEngineResult result = engine.FindPartialView(controllerContext, VIEW_NAME, false);
// Assert
RazorView razorView = (RazorView)result.View;
string actualViewPath = razorView.ViewPath;
actualViewPath.Should().Be(EXPECTED_VIEW_PATH);
}
P.S. I'm using xUnit as my testing framework and NSubstitute as my mocking framework should I install both AutoFixture.Xunit and AutoFixture.AutoNSubstitute?
UPDATE: After learning more and more about it I guess it is not the right tool for the job because I tried to replace my test doubles factories with AutoFixture rather than setting up my SUT with it.
Due to odd reason I thought it's doing the same thing NBuilder is doing and from what I can see they are very different tools.
So after some thinking I think I'll go and change the methods I have on my test doubles factories to objects then use AutoFixture to create my SUT and inject my test doubles to it.
Note: I don't have the source code for the CsEmbeddedRazorViewEngine type and all the other custom types.
Here is how it could be written with AutoFixture:
[Theory]
[InlineAutoWebData("en-US", "about", "~/MyAssemblyName/Views/Home/Index.cshtml")]
[InlineAutoWebData("en-US", "other", "~/MyAssemblyName/Views/Home/Index.cshtml")]
public void Should_return_the_default_path_of_the_view_for_enUS(
string language,
string viewName,
string expected,
ControllerContext controllerContext,
CsEmbeddedRazorViewEngine sut)
{
var result = sut.FindPartialView(controllerContext, viewName, false);
var actual = ((RazorView)result.View).ViewPath;
actual.Should().Be(expected);
}
How it works:
It uses AutoFixture itself together with it's glue libraries for xUnit.net and NSubstitute:
PM> Install-Package AutoFixture.Xunit
PM> Install-Package AutoFixture.AutoNSubstitute
With InlineAutoWebData you actually combine inline values and auto-generated data values by AutoFixture – also including Auto-Mocking with NSubstitute.
internal class InlineAutoWebDataAttribute : CompositeDataAttribute
{
internal InlineAutoWebDataAttribute(params object[] values)
: base(
new InlineDataAttribute(values),
new CompositeDataAttribute(
new AutoDataAttribute(
new Fixture().Customize(
new WebModelCustomization()))))
{
}
}
Remarks:
You could actually replace the WebModelCustomization customization above with AutoNSubstituteCustomization and it could work.
However, assuming that you are using ASP.NET MVC 4, you need to customize the Fixture instance with:
internal class WebModelCustomization : CompositeCustomization
{
internal WebModelCustomization()
: base(
new MvcCustomization(),
new AutoNSubstituteCustomization())
{
}
private class MvcCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize<ControllerContext>(c => c
.Without(x => x.DisplayMode));
// Customize the CsEmbeddedRazorViewEngine type here.
}
}
}
Further reading:
Encapsulating AutoFixture Customizations
AutoData Theories with AutoFixture
I ended up doing this.
[Theory,
InlineData("en-US", "Index", "~/MyAssemblyName/Views/Home/Index.cshtml"),
InlineData("en-us", "Index", "~/MyAssemblyName/Views/Home/Index.cshtml"),
InlineData("en", "Index", "~/MyAssemblyName/Views/Home/Index.cshtml")]
public void Should_return_the_default_path_of_the_view(string language, string viewName, string expected)
{
// Arrange
CsEmbeddedRazorViewEngine engine = new CsEmbeddedRazorViewEngineFixture();
ControllerContext controllerContext = FakeControllerContextBuilder.WithLanguage(language).Build();
// Act
ViewEngineResult result = engine.FindPartialView(controllerContext, viewName, false);
// Assert
string actualViewPath = ((RazorView)result.View).ViewPath;
actualViewPath.Should().Be(expected);
}
I encapsulated the details to setup my SUT into a fixture and used the builder pattern to handle my fakes, I think that it's readable and pretty straightforward now.
While AutoFixture looks pretty cool, the learning curve seems long and I will need to invest enough time to understand it, for now, I want to clean-up my unit tests and make them more readable. :)

How to figure out which SQLDependency triggered change function?

I'm exploring query notifications with the SQLDependency class. Building a simple working example is easy, but I feel like I'm missing something. Once I step past a simple one-table/one-dependency example I'm left wondering how can I figure out which dependency triggered my callback?
I'm having a bit of trouble explaining, so I included the simple example below. When AChange() is called I cannot look at the sql inside the dependency, and i don't have a reference to the associated cache object.
So what's a boy to do?
Option 1 - create a distinct function for each object i want to track and hard code the cache-key (or relevant information) in the callback. This feels dirty & eliminates the posibility of adding new cache items without deploying new code--ewww.
Option 2 - Use the Dependency Id property and a parallel tracking structure
Am I just missing something? Is this a deficiency in the SQLDependency structure? I've I've looked at 20 different articles on the topic and all of them seem to have the same hole. Suggestions?
Code Sample
public class DependencyCache{
public static string cacheName = "Client1";
public static MemoryCache memCache = new MemoryCache(cacheName);
public DependencyCache() {
SqlDependency.Start(connString);
}
private static string GetSQL() {
return "select someString FROM dbo.TestTable";
}
public void DoTest() {
if (memCache["TEST_KEY"] != null ) {
Debug.WriteLine("resources found in cache");
return;
}
Cache_GetData();
}
private void Cache_GetData() {
SqlConnection oConn;
SqlCommand oCmd;
SqlDependency oDep;
SqlDataReader oRS;
List<string> stuff = new List<string>();
CacheItemPolicy policy = new CacheItemPolicy();
SqlDependency.Start(connString);
using (oConn = new SqlConnection(connString) ) {
using (oCmd = new SqlCommand(GetSQL(), oConn) ) {
oDep = new SqlDependency(oCmd);
oConn.Open();
oRS = oCmd.ExecuteReader();
while(oRS.Read() ) {
resources.Add( oRS.GetString(0) );
}
oDep.OnChange += new OnChangeEventHandler (AChange);
}
}
memCache.Set("TEST_KEY", stuff, policy);
}
private void AChange( object sender, SqlNotificationEventArgs e) {
string msg= "Dependency Change \nINFO: {0} : SOURCE {1} :TYPE: {2}";
Debug.WriteLine(String.Format(msg, e.Info, e.Source, e.Type));
// If multiple queries use this as a callback how can i figure
// out WHAT QUERY TRIGGERED the change?
// I can't figure out how to tell multiple dependency objects apart
((SqlDependency)sender).OnChange -= Cache_SqlDependency_OnChange;
Cache_GetData(); //reload data
}
}
First and foremost: the handler has to be set up before the command is executed:
oDep = new SqlDependency(oCmd);
oConn.Open();
oDep.OnChange += new OnChangeEventHandler (AChange);
oRS = oCmd.ExecuteReader();
while(oRS.Read() ) {
resources.Add( oRS.GetString(0) );
}
Otherwise you have a window when the notification may be lost and your callback never invoked.
Now about your question: you should use a separate callback for each query. While this may seem cumbersome, is actually trivial by using a lambda. Something like the following:
oDep = new SqlDependency(oCmd);
oConn.Open();
oDep.OnChange += (sender, e) =>
{
string msg = "Dependency Change \nINFO: {0} : SOURCE {1} :TYPE: {2}";
Debug.WriteLine(String.Format(msg, e.Info, e.Source, e.Type));
// The command that trigger the notification is captured in the context:
// is oCmd
//
// You can now call a handler passing in the relevant info:
//
Reload_Data(oCmd, ...);
};
oRS = oCmd.ExecuteReader();
...
And remember to always check the notification source, info and type. Otherwise you run the risk of spinning ad-nauseam when you are notified for reasons other than data change, like invalid query. As a side comment I would add that a good cache design does not refresh the cache on invalidation, but simply invalidates the cached item and lets the next request actually fetch a fresh item. With your 'proactive' approach you are refreshing cached items even when not needed, refresh multiple times before they are accessed etc etc. I left out from the example error handling and proper thread synchronization (both required).
Finally, have a look at LinqtoCache which does pretty much what you're trying to do, but for LINQ queries.

Problem with wcf in silverlight-return types from services have changed!

Relatively new to wcf and Silverlight. I had been banging my head against the wall trying to digest WCF and the contract concept. I found a web post that showed a way to make WCF work. As you'll see it doesn't follow the classical WCF form (no datacontracts).
Here is an example of the code. Note that the ImageData object is not part of a data contract but simply declared as a normal object in a class cs file with get and set properties.
Despite the fact that this isn't "regulation" it does work. The service reference to the service automatically moves over the ImageData class and a number of other pieces of information to make this work.
This worked fine. However I hit an error I've hit twice before where I'm told that some obscure assembly is not present to allow the service to be accessed. This has happened twice before. In all three cases I was able recreate the service and the service reference (but I had to change the names of both because it seemed they already existed in the project). The first two times this fixed my problem.
This time I used the same fix but now the code, instead of returning a list of ImageData as is indicated in the signature of the function is now returning a type called "GetImageDataReponse" and I no longer can iterate through the list of ImageData Objects that it used to return. In looking at the definition of "GetImageDataReponse" I find that it looks like another VS2010 generated function that has the list of ImageData embedded in it. It seems to have added another layer of abstraction over the return type (List).
I'm not sure what is causing the odd "missing assembly" errors which seem to occur randomly and cause me to rebuild the service. Now I'm not sure why I am getting these new return types from functions that have a clear return type specified and worked before.
Any help would be appreciated. I hope to be able to prevent having to rebuild the service and the service reference in the first place.
Below is the code that used to work and then the defination of "GetImageDataReponse" in the reference.cs file.
Thanks,
Fig000
[OperationContract]
public List<ImageData> GetImageData(int imageID)
{
List<ImageData> ImageDataList = new List<ImageData>();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CN_eLogWeb"].ConnectionString.ToString());
SqlDataReader dr = null;
string sql = #"SELECT isnull(i.ANI,'') as ANI, isnull(i.DonorCorpID,0) as DonorCorpID,isnull(i.DonorUnitID,0) as DonorUnitID,isnull(i.AgencyID,0) as AgencyID,
isnull(i.PickupDT,'1/1/1900') as PickupDT, isnull(u.InBoundSortedStatusID,1) as InboundSortedStatusID, isnull(u.FollowupRequired,0) as FollowupRequired
from InboundSorted i
left join UserSortSessionInfo u on i.IncomingID=u.IncomingID
left join Image img on i.ImageID=img.ImageID where img.ImageID=" + imageID;
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
ImageData oImageData = new ImageData
{
ANI = Convert.ToString(dr["ANI"]),
DonorCorpID = Convert.ToInt32(dr["DonorCorpID"]),
DonorUnitID = Convert.ToInt32(dr["DonorUnitID"]),
AgencyID = Convert.ToInt32(dr["AgencyID"]),
PickupDT = Convert.ToDateTime(dr["PickupDT"]),
Status = GetInboundSortedStatus(Convert.ToInt32(dr["InboundSortedStatusID"])),
FollowupRequired = Convert.ToBoolean(dr["FollowupRequired"])
};
ImageDataList.Add(oImageData);
}
}
con.Close();
return ImageDataList;
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName="GetImageDataResponse", WrapperNamespace="", IsWrapped=true)]
public partial class GetImageDataResponse {
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)]
public System.Collections.Generic.List<acSilverlightLib.ImgServiceRef2.ImageData> GetImageDataResult;
public GetImageDataResponse() {
}
public GetImageDataResponse(System.Collections.Generic.List<acSilverlightLib.ImgServiceRef2.ImageData> GetImageDataResult) {
this.GetImageDataResult = GetImageDataResult;
}
}
if (dr.HasRows)
{
while (dr.Read())
{
ImageData oImageData = new ImageData
{
ANI = Convert.ToString(dr["ANI"]),
DonorCorpID = Convert.ToInt32(dr["DonorCorpID"]),
DonorUnitID = Convert.ToInt32(dr["DonorUnitID"]),
AgencyID = Convert.ToInt32(dr["AgencyID"]),
PickupDT = Convert.ToDateTime(dr["PickupDT"]),
Status = GetInboundSortedStatus(Convert.ToInt32(dr["InboundSortedStatusID"])),
FollowupRequired = Convert.ToBoolean(dr["FollowupRequired"])
};
ImageDataList.Add(oImageData);
}
}
con.Close();
return ImageDataList;
}
Did you make any changes to ImageData between versions?
An issue that can cause this is missing a Default (parameter-less) constructor on ImageData, or one of its properties.

Resources