I am trying to learn how to use Dapper.Extension but after multiple attempts installing and reinstalling the nuget package. I can't get the namespace to resolve and usable. I am creating a generic repository but when i attempt to include the namespace, VS doesn't even see it. I have looked all over their documentation and install guides but can't see anyone else having this issue. Is there something stupid i am over looking? See my Find method below using the extension.
public T Find(int id)
{
using(var conn = _ConnectionFactory.GetConnection())
{
return conn.Get<T>(id);
}
}
You have added the libraries correctly but you need to add using references at the top of your classes to import the dapper namespace and add the additional methods to the SqlConnection object. To do so add both of the lines below to the top of your class files, before any namespace declarations.
using Dapper;
using DapperExtensions;
The equivalent in VB.NET would be:
Imports Dapper
Imports DapperExtensions
There are two nuget packages "DapperExtensions" and "Dapper.Extensions". Make sure you have installed the first one. I did this mistake too
To test I used VS 2015:
Create new console application
Install-Package DapperExtensions
Install latest version of Dapper: Install-Package Dapper -version 1.50.2
Here is the test code:
using System.Data.SqlClient;
using DapperExtensions;
namespace ConsoleApplication2
{
public class Foo
{
public int Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
var result = Find(1);
}
public static Foo Find(int id)
{
using (var conn = new SqlConnection(#"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo"))
{
conn.Open();
var foo = conn.Get<Foo>(id);
return foo;
}
}
}
}
I had a similar issue but that was because I had installed "Dapper.Extension" via nuget instead of installing "DapperExtensions". Could you have done something similar?
Related
Is there a way to programmatically access the application package name/namespace/version information in Xamarin portable C# code?
Like this (link is how to access for Android, I want to know if there is a pre-existing C# cross platform way to access this in the portable class code.)
Not this.
i got answer to your problem, but it is using dependency service to work. There is no other way if there is no Xamarin plugin.
Here is the code:
Interface in pcl project:
public interface IPackageName
{
string PackageName { get; }
}
Android implementation:
public class PackageNameDroid : IPackageName
{
public PackageNameDroid()
{
}
public string PackageName
{
get { return Application.Context.PackageName; }
}
}
iOS implementation:
public class PackageNameIOS : IPackageName
{
public PackageNameIOS()
{
}
public string PackageName
{
get { return NSBundle.MainBundle.BundleIdentifier; }
}
}
Don't forget to mark platform files for dependency service in Android file:
[assembly: Xamarin.Forms.Dependency(typeof(PackageNameDroid))]
and iOS file:
[assembly: Xamarin.Forms.Dependency(typeof(PackageNameIOS))]
Now you're good to go and can use it in PCLs just like that:
string packageName = DependencyService.Get<IPackageName>().PackageName;
Hope it helps others.
I have an error in the following code when using the Headers property:
Public Function UploadImage(image As String) As String
Dim wc As System.WebClient
'create WebClient
Set wc = CreateObject("System.Net.WebClient")
Call wc.Headers.Add("Authorization", "Client-ID " & ClientId) <------- Error occurs here
I have repro'd your issue as far as possible. You didn't mention what the error you were getting was, but I got:
Automation error -2146233079 (80131509)
I tried using
CallByName(wc, "Headers", VbGet)
... but that just returns
Automation error 440.
Oh well...
I looked up online, and found this link. My guess is that because the base class of the WebHeaderCollection class is not COM Visible, this causes the error.
My work-around would be to wrap up this functionality in a small .NET component, and make that COM visible.
An example of this would be something like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
namespace WebClientWrapper
{
[ComVisible(true)]
[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
public class WebClientWrapper : WebClient
{
[ComVisible(true)]
public WebHeaderCollectionWrapper WHeaders
{
get
{
return new WebHeaderCollectionWrapper(base.Headers);
}
}
}
[ComVisible(true)]
[Guid("yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy")]
public class WebHeaderCollectionWrapper
{
WebHeaderCollection _whc;
internal WebHeaderCollectionWrapper(WebHeaderCollection whc)
{
_whc = whc;
}
[ComVisible(true)]
public void Add(string name, string value)
{
_whc.Add(name, value);
}
[ComVisible(true)]
public void Clear()
{
_whc.Clear();
}
}
}
(You will have to replace the GUIDs with your own values - use GUIDGEN.EXE).
Instantiate this component with CreateObject("WebClientWrapper.WebClientWrapper")
Now you would simply replace references to the Headers property to WHeaders (or whatever you want to call it). WHeaders gives you a true wrapper around WebHeaderCollection - you'll have to define all the other wrapped methods and properties yourself. I was hoping to define WHeaders as public WebHeaderCollectionWrapper, but that didn't seem to work.
Since WebClientWrapper inherits from WebClient, you should be able to use most of the properties and methods. Where you have troubles, just add new methods to the class wrapping up the functionality that doesn't work with VB.
Oh, and do remember to set the checkbox at Project Properties => Build => Output => Register for COM interop. And then reference the type library created.
I'm mucking about with reactive extensions and Iv'e hit a snag that I can't for the life of me work out what the cause is.
If I use a .NET 4 console mode app, where everything is static as follows:
using System;
using System.Reactive.Subjects;
using FakeDal;
using FakeDal.Entites;
using RxProducer;
namespace Runner
{
class Program
{
private static readonly Subject<DaftFrog> _subject = new Subject<DaftFrog>();
private static readonly Repository<DaftFrog> _frogRepo = new Repository<DaftFrog>();
static void Main()
{
_subject.Subscribe(RespondToNewData);
}
private static void RespondToNewData(DaftFrog frog)
{
_frogRepo.Save(frog);
}
}
}
DaftFrog is just a test class in my fake DAL class, this is a simple .NET 4 Class library project, the DaftFrog class, is a simple poco with a few fields in, the dal.save method just simply does a console.WriteLine of a field in the DaftFrog object.
Both classes are just simple stand in's for the real things once I get around to making the RX code work.
Anyway, back to the problem, so the code above works fine, and if I do a few
_subject.OnNext(new DaftFrog());
calls, the fake dal class, prints out what I expect and everything works fine...
HOWEVER>....
If I then transport this code as is, to a class library, and then new up that class library from within my "static program" as follows:
using System.Reactive.Subjects;
using FakeDal;
using FakeDal.Entites;
namespace RxProducer
{
public class Producer
{
private readonly Subject<DaftFrog> _subject = new Subject<DaftFrog>();
private readonly Repository<DaftFrog> _frogRepo = new Repository<DaftFrog>();
private int _clock;
public void Start()
{
_subject.Subscribe(RespondToNewData);
}
public void Stop()
{
}
public void Tick()
{
if(_clock % 5 == 0)
{
DaftFrog data = new DaftFrog();
_subject.OnNext(data);
}
_clock++;
}
private void RespondToNewData(DaftFrog frog)
{
_frogRepo.Save(frog);
}
}
}
And then use that class in my program
using System;
using RxProducer;
namespace Runner
{
class Program
{
private static readonly Producer _myProducer = new Producer();
static void Main()
{
_myProducer.Start();
while(!line.Contains("quit"))
{
_myProducer.Tick();
line = Console.ReadLine();
}
_myProducer.Stop();
}
}
}
Then my project fails to compile.
Specifically it fails on the line:
_subject.Subscribe(RespondToNewData);
in the RxProducer class library, mores the point, the error the compiler throws back makes little sense either:
Error 1 The best overloaded method match for 'System.Reactive.Subjects.Subject<FakeDal.Entites.DaftFrog>.Subscribe(System.IObserver<FakeDal.Entites.DaftFrog>)' has some invalid arguments H:\programming\rxtesting\RxProducer\Producer.cs 17 7 RxProducer
Error 2 Argument 1: cannot convert from 'method group' to 'System.IObserver<FakeDal.Entites.DaftFrog>' H:\programming\rxtesting\RxProducer\Producer.cs 17 26 RxProducer
At first I thought that it might have been the static thing, so I made everything in the class library static, and that made no difference at all.
Iv'e really not done much with Rx until now, but I work with C# and VS 99% of the time, so I'm aware that the error is telling me it can't convert a type of some description, I just don't understand why it's telling me that, esp when the code works perfectly in the static program, but not in a class library.
Shawty
UPDATE
Second thoughts, I just know there are going to be those who insist that I post the fakedal and daft frog definitions, even though IMHO they won't be required, but to pacify the hordes of pretenders who will ask here they are :-)
using System;
namespace FakeDal
{
public class Repository<T>
{
public void Save(T entity)
{
Console.WriteLine("Here we write T to the database....");
}
}
}
namespace FakeDal.Entites
{
public class DaftFrog
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsTotalyDaft { get; set; }
}
}
Include using System; into file where you have Producer, this will help to convert RespondToNewData to IObserver<T>.
Sounds like the compiler is having trouble inferring the Action...might be missing a using statement for the relevant extension method. Alternatively, try either of:
_subject.Subscribe ((Action<DaftFrog>) RespondToData);
Or:
var obs = Observer.Create ( I forget the overload );
_subject.Subscribe( obs);
I Have a little snag. With my asp.net mvc3 application.
When I trying to compile my app I obtain this Error
Error 2 'Blog.Domain.Concrete.EFEntryRepository' does not implement interface member 'Blog.Domain.Abstract.IEntryRepository.SaveEntry(Blog.Domain.Entities.Entry)' D:\dokumenty\Visual Studio 2010\Projects\MVC3\Blog\Blog.Domain\Concrete\EFEntryRepository.cs 10 19 Blog.Domain
This is my Interface.
namespace Blog.Domain.Abstract
{
public interface IEntryRepository
{
IQueryable<Entry> Entries { get; }
void SaveEntry(Entry entry);
void DeleteEntry(Entry entry);
}
}
And this is my implementation of it.
public class EFEntryRepository : IEntryRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Entry> Entries
{
get { return context.Entries; }
}
public void SaveEntry(Entry entry)
{
if (entry.EntryID == 0)
context.Entries.Add(entry);
context.SaveChanges();
}
public void DeleteEntry(Entry entry)
{
context.Entries.Remove(entry);
context.SaveChanges();
}
}
This is link to my project. http://sigma.ug.edu.pl/~kkubacki/Blog.zip //NEW
Now I is compiling.
What I Do Wrong ?
I have new Information about the bug. Now the solution is compiling but the application crashes with the bug information
"{"The type 'Blog.Domain.Concrete.Entry' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject."} " Visual studio shows bug in the EFEntryRepository class.
I don't know what to do please help.
OK Problem IS Solved.
You have two different Entry classes in different namespaces.
Blog.Domain.Entities.Entry
Blog.Domain.Concrete.Entry
The interface is referring to one and the implementation is referring to the other.
I am attempting to use Moles to test a non-static method in a separate assembly. When running the test without the [HostType("Moles")] tag, the test runs fine. When I replace it I receive the following error:
"The type initializer for 'HtmlAgilityPack.HtmlNode' threw an exception."
I have attached code samples that perform in an identical manner.
Any help would be great!
Class/method being called by the unit test
using System;
using HtmlAgilityPack;
using System.Web;
namespace HAPAndMoles
{
public class Class1
{
public void fooBar()
{
HtmlDocument foo = new HtmlDocument();
}
}
}
Unit Test
using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HAPAndMoles;
using Microsoft.Moles.Framework;
using HtmlAgilityPack;
using System.Web;
namespace HAPAndMoles
{
[TestClass]
public class UnitTest1
{
[TestMethod]
[HostType("Moles")]
public void TestMethod1()
{
Class1 bar = new Class1();
bar.fooBar();
}
}
}
I'm not sure I understand your example because in fact you don't use Moles.
If you just want to "Mole" our own non-virtual method, in the references of your test project you just have to right-click on the assembly of the tested project and choose Add Moles Assembly. That will create an HAPAndMoles.Moles reference.
Then add the corresponding using and you can call your class "moled" starting with M (Class1 => MCLass1). I show you an example testing the MClass1 behaviour:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HAPAndMoles;
using HAPAndMoles.Moles;
namespace HAPAndMoles {
[TestClass]
public class UnitTest1
{
[TestMethod]
[HostType("Moles")]
public void TestMethod1()
{
bool called = false;
var bar = new MClass1()
{
fooBar = () => called = true
};
((Class1)bar).fooBar();
Assert.IsTrue(called);
}
}
}
When I want Moles of mscorlib, I right-click directly on the references of the test project and I can Add Moles Assembly for mscorlib. Then the
using Microsoft.Moles.Framework;
is needed.