Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# dapper map to interface

Tags:

c#

dapper

Can i use C# dapper to do something like this:

IFoo bar = _dbConnection.Query<IFoo>("My query there");

Now I can't do it, due to not impelemented default parameterless constructor.

Is there some trick to honor gods of SOLID (especially spirits of Liskov Substitution Principle) or should i leave it as it is and map my data not to IFoo but to Foo?

I'm really worrying about respecting these SOLID stuff, but still don't know where i should do it, so looking for an advice for this concrete situation.

like image 884
Occam's chainsaw Avatar asked Feb 15 '16 13:02

Occam's chainsaw


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

You need a concrete implementation of your class to instantiate. Internally, it has to do a create a new object. You can't create a new instance of an interface:

var foo = new IFoo(); // This won't build!

You can still cast your result to an interface, but you need a concrete type to build from the database.

IEnumerable<IFoo> foo = _dbConnection.Query<Foo>("My query there");
like image 92
krillgar Avatar answered Sep 24 '22 20:09

krillgar


One way to organise repository is to use private class objects for querying, but expose results as public interfaces.

Pulbic Model:

namespace MyProject.Foo.Model
{
    public interface IFoo
    {
        string Property1 { get; set; }
        string Property2 { get; set; }
    }

    public interface IFooRepository
    {
        IEnumerbale<IFoo> GetFoos();
    }
}

Query implementation with private class:

namespace MyProject.Foo.Repositories
{
    public class FooRepository: IFooRepository
    {
        private class Foo: IFoo
        {
            public string Property1 { get; set; }
            public string Property2 { get; set; }
        }

        public IEnumerbale<IFoo> GetFoos()
        {
            IEnumerable<IFoo> foo = _dbConnection.Query<Foo>("My query there");
            return foo;
        }
    }
}
like image 24
Erikas Pliauksta Avatar answered Sep 22 '22 20:09

Erikas Pliauksta