Using Dapper in ASP.NET Core with dependency injectionUnit of work + repository + service layer with...

Reference on complex cobordism

How should I handle players who ignore the session zero agreement?

Compound Interest... with Wizard Money

Avoiding morning and evening handshakes

What is better: yes / no radio, or simple checkbox?

"On one hand" vs "on the one hand."

Why can a 352GB NumPy ndarray be used on an 8GB memory macOS computer?

Why are the books in the Game of Thrones citadel library shelved spine inwards?

Why do members of Congress in committee hearings ask witnesses the same question multiple times?

Quenching swords in dragon blood; why?

Why did Bush enact a completely different foreign policy to that which he espoused during the 2000 Presidential election campaign?

What is the time complexity of enqueue and dequeue of a queue implemented with a singly linked list?

What is the wife of a henpecked husband called?

Using only 1s, make 29 with the minimum number of digits

Pendulum Rotation

How experienced do I need to be to go on a photography workshop?

Why is working on the same position for more than 15 years not a red flag?

Can pricing be copyrighted?

Is there hidden data in this .blend file? Trying to minimize the file size

Why did the villain in the first Men in Black movie care about Earth's Cockroaches?

Does Windows 10's telemetry include sending *.doc files if Word crashed?

How would one buy a used TIE Fighter or X-Wing?

Can a person refuse a presidential pardon?

Why zero tolerance on nudity in space?



Using Dapper in ASP.NET Core with dependency injection


Unit of work + repository + service layer with dependency injectionASP.NET MVC application architecture fit for unit testing with mockingDependency injection with asp.net identity and property injection with dbcontextASP.NET Core singleton acting as an HttpClient providerUsing dapper Unit of Work in 3-tier architectureMapper made specifically to work with DapperUsing constructor dependency injection in a custom mapper classImplementing integration tests in ASP.NET core using InMemory databaseWeb Service that gets data from multiple tables in a database using EF Core Database-First approachA custom session with transaction support using Dapper













1












$begingroup$


I want to build an ASP.NET core project and I want to use Dapper and I want to know if I'm in the right way or not (I omitted some parts for simplicity):



MyApp.Domains



public class Ticket
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}


MyApp.Core



public interface ITicketService
{
void Add(Ticket m);
}

public class TicketService : ITicketService
{
string _connectionString;

public TicketService(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("DefaultConnection");
}

public void Add(Ticket m)
{
using (var db = new SqlConnection(_connectionString))
{
db.Open();

db.Insert(m);
}
}
}


MyApp.Web



Startup.cs



public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ITicketService, TicketService>();

// ...
}


TicketController.cs



ITicketService _ticketService;

public TicketController(ITicketService ticketService)
{
_ticketService = ticketService;
}

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Ticket m)
{
if (ModelState.IsValid)
{
_ticketService.Add(m);
}

return View();
}


My questions:




  1. Is using using not bad here?

  2. Have I used DI correctly?

  3. Should I call TicketService, TicketRepository?

  4. Correct me if I did something wrong.


Note: I won't use unit test, so please don't suggest improvement if this implementation is not great for unit test.










share|improve this question











$endgroup$












  • $begingroup$
    Out of curiosity: if you're not planning to have unit tests (!) then...why do you use DI? There are, of course, use-cases unrelated to tests but to know if this is one of them might make a difference.
    $endgroup$
    – Adriano Repetti
    Feb 1 at 15:54










  • $begingroup$
    @AdrianoRepetti I deleted my prev comment to answer your question more clear, I used DI because I think it makes app more flexible, extending the application becomes easier and it helps to enable loose coupling.
    $endgroup$
    – Mehdi Dehghani
    Feb 1 at 16:18










  • $begingroup$
    Let me go back one step. Did you EVER needed to switch the repository you used in your real world applications? Did you EVER needed to do it at run-time/from configuration? What has been the cost of that without DI? Now let's imagine to drop DI from all your code and see what you gain. "loose coupling, flexible, extending...easier" sound like buzzwords if you do not have a SPECIFIC use-case in mind. In which case DI (if you do not want tests (!!!)) is making your life easier?
    $endgroup$
    – Adriano Repetti
    Feb 1 at 16:21












  • $begingroup$
    Your right, DI maybe is not necessary for me, because I don't have any plan to switch the repository or any run-time configuration. I just not sure about extending, but overall I think I have not real use-case here.
    $endgroup$
    – Mehdi Dehghani
    Feb 1 at 16:26










  • $begingroup$
    1) no, why it should be? 2) yes, assuming that you WILL use DI to write your unit testing in future. 3) If you're using an ORM then I'll drop it all together. A method which resposibility is just using, open and close is pretty anemic (IMHO)
    $endgroup$
    – Adriano Repetti
    Feb 1 at 16:27
















1












$begingroup$


I want to build an ASP.NET core project and I want to use Dapper and I want to know if I'm in the right way or not (I omitted some parts for simplicity):



MyApp.Domains



public class Ticket
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}


MyApp.Core



public interface ITicketService
{
void Add(Ticket m);
}

public class TicketService : ITicketService
{
string _connectionString;

public TicketService(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("DefaultConnection");
}

public void Add(Ticket m)
{
using (var db = new SqlConnection(_connectionString))
{
db.Open();

db.Insert(m);
}
}
}


MyApp.Web



Startup.cs



public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ITicketService, TicketService>();

// ...
}


TicketController.cs



ITicketService _ticketService;

public TicketController(ITicketService ticketService)
{
_ticketService = ticketService;
}

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Ticket m)
{
if (ModelState.IsValid)
{
_ticketService.Add(m);
}

return View();
}


My questions:




  1. Is using using not bad here?

  2. Have I used DI correctly?

  3. Should I call TicketService, TicketRepository?

  4. Correct me if I did something wrong.


Note: I won't use unit test, so please don't suggest improvement if this implementation is not great for unit test.










share|improve this question











$endgroup$












  • $begingroup$
    Out of curiosity: if you're not planning to have unit tests (!) then...why do you use DI? There are, of course, use-cases unrelated to tests but to know if this is one of them might make a difference.
    $endgroup$
    – Adriano Repetti
    Feb 1 at 15:54










  • $begingroup$
    @AdrianoRepetti I deleted my prev comment to answer your question more clear, I used DI because I think it makes app more flexible, extending the application becomes easier and it helps to enable loose coupling.
    $endgroup$
    – Mehdi Dehghani
    Feb 1 at 16:18










  • $begingroup$
    Let me go back one step. Did you EVER needed to switch the repository you used in your real world applications? Did you EVER needed to do it at run-time/from configuration? What has been the cost of that without DI? Now let's imagine to drop DI from all your code and see what you gain. "loose coupling, flexible, extending...easier" sound like buzzwords if you do not have a SPECIFIC use-case in mind. In which case DI (if you do not want tests (!!!)) is making your life easier?
    $endgroup$
    – Adriano Repetti
    Feb 1 at 16:21












  • $begingroup$
    Your right, DI maybe is not necessary for me, because I don't have any plan to switch the repository or any run-time configuration. I just not sure about extending, but overall I think I have not real use-case here.
    $endgroup$
    – Mehdi Dehghani
    Feb 1 at 16:26










  • $begingroup$
    1) no, why it should be? 2) yes, assuming that you WILL use DI to write your unit testing in future. 3) If you're using an ORM then I'll drop it all together. A method which resposibility is just using, open and close is pretty anemic (IMHO)
    $endgroup$
    – Adriano Repetti
    Feb 1 at 16:27














1












1








1





$begingroup$


I want to build an ASP.NET core project and I want to use Dapper and I want to know if I'm in the right way or not (I omitted some parts for simplicity):



MyApp.Domains



public class Ticket
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}


MyApp.Core



public interface ITicketService
{
void Add(Ticket m);
}

public class TicketService : ITicketService
{
string _connectionString;

public TicketService(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("DefaultConnection");
}

public void Add(Ticket m)
{
using (var db = new SqlConnection(_connectionString))
{
db.Open();

db.Insert(m);
}
}
}


MyApp.Web



Startup.cs



public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ITicketService, TicketService>();

// ...
}


TicketController.cs



ITicketService _ticketService;

public TicketController(ITicketService ticketService)
{
_ticketService = ticketService;
}

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Ticket m)
{
if (ModelState.IsValid)
{
_ticketService.Add(m);
}

return View();
}


My questions:




  1. Is using using not bad here?

  2. Have I used DI correctly?

  3. Should I call TicketService, TicketRepository?

  4. Correct me if I did something wrong.


Note: I won't use unit test, so please don't suggest improvement if this implementation is not great for unit test.










share|improve this question











$endgroup$




I want to build an ASP.NET core project and I want to use Dapper and I want to know if I'm in the right way or not (I omitted some parts for simplicity):



MyApp.Domains



public class Ticket
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}


MyApp.Core



public interface ITicketService
{
void Add(Ticket m);
}

public class TicketService : ITicketService
{
string _connectionString;

public TicketService(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("DefaultConnection");
}

public void Add(Ticket m)
{
using (var db = new SqlConnection(_connectionString))
{
db.Open();

db.Insert(m);
}
}
}


MyApp.Web



Startup.cs



public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ITicketService, TicketService>();

// ...
}


TicketController.cs



ITicketService _ticketService;

public TicketController(ITicketService ticketService)
{
_ticketService = ticketService;
}

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Ticket m)
{
if (ModelState.IsValid)
{
_ticketService.Add(m);
}

return View();
}


My questions:




  1. Is using using not bad here?

  2. Have I used DI correctly?

  3. Should I call TicketService, TicketRepository?

  4. Correct me if I did something wrong.


Note: I won't use unit test, so please don't suggest improvement if this implementation is not great for unit test.







c# dependency-injection asp.net-core crud dapper






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 29 mins ago









Jamal

30.3k11119227




30.3k11119227










asked Feb 1 at 15:45









Mehdi DehghaniMehdi Dehghani

1063




1063












  • $begingroup$
    Out of curiosity: if you're not planning to have unit tests (!) then...why do you use DI? There are, of course, use-cases unrelated to tests but to know if this is one of them might make a difference.
    $endgroup$
    – Adriano Repetti
    Feb 1 at 15:54










  • $begingroup$
    @AdrianoRepetti I deleted my prev comment to answer your question more clear, I used DI because I think it makes app more flexible, extending the application becomes easier and it helps to enable loose coupling.
    $endgroup$
    – Mehdi Dehghani
    Feb 1 at 16:18










  • $begingroup$
    Let me go back one step. Did you EVER needed to switch the repository you used in your real world applications? Did you EVER needed to do it at run-time/from configuration? What has been the cost of that without DI? Now let's imagine to drop DI from all your code and see what you gain. "loose coupling, flexible, extending...easier" sound like buzzwords if you do not have a SPECIFIC use-case in mind. In which case DI (if you do not want tests (!!!)) is making your life easier?
    $endgroup$
    – Adriano Repetti
    Feb 1 at 16:21












  • $begingroup$
    Your right, DI maybe is not necessary for me, because I don't have any plan to switch the repository or any run-time configuration. I just not sure about extending, but overall I think I have not real use-case here.
    $endgroup$
    – Mehdi Dehghani
    Feb 1 at 16:26










  • $begingroup$
    1) no, why it should be? 2) yes, assuming that you WILL use DI to write your unit testing in future. 3) If you're using an ORM then I'll drop it all together. A method which resposibility is just using, open and close is pretty anemic (IMHO)
    $endgroup$
    – Adriano Repetti
    Feb 1 at 16:27


















  • $begingroup$
    Out of curiosity: if you're not planning to have unit tests (!) then...why do you use DI? There are, of course, use-cases unrelated to tests but to know if this is one of them might make a difference.
    $endgroup$
    – Adriano Repetti
    Feb 1 at 15:54










  • $begingroup$
    @AdrianoRepetti I deleted my prev comment to answer your question more clear, I used DI because I think it makes app more flexible, extending the application becomes easier and it helps to enable loose coupling.
    $endgroup$
    – Mehdi Dehghani
    Feb 1 at 16:18










  • $begingroup$
    Let me go back one step. Did you EVER needed to switch the repository you used in your real world applications? Did you EVER needed to do it at run-time/from configuration? What has been the cost of that without DI? Now let's imagine to drop DI from all your code and see what you gain. "loose coupling, flexible, extending...easier" sound like buzzwords if you do not have a SPECIFIC use-case in mind. In which case DI (if you do not want tests (!!!)) is making your life easier?
    $endgroup$
    – Adriano Repetti
    Feb 1 at 16:21












  • $begingroup$
    Your right, DI maybe is not necessary for me, because I don't have any plan to switch the repository or any run-time configuration. I just not sure about extending, but overall I think I have not real use-case here.
    $endgroup$
    – Mehdi Dehghani
    Feb 1 at 16:26










  • $begingroup$
    1) no, why it should be? 2) yes, assuming that you WILL use DI to write your unit testing in future. 3) If you're using an ORM then I'll drop it all together. A method which resposibility is just using, open and close is pretty anemic (IMHO)
    $endgroup$
    – Adriano Repetti
    Feb 1 at 16:27
















$begingroup$
Out of curiosity: if you're not planning to have unit tests (!) then...why do you use DI? There are, of course, use-cases unrelated to tests but to know if this is one of them might make a difference.
$endgroup$
– Adriano Repetti
Feb 1 at 15:54




$begingroup$
Out of curiosity: if you're not planning to have unit tests (!) then...why do you use DI? There are, of course, use-cases unrelated to tests but to know if this is one of them might make a difference.
$endgroup$
– Adriano Repetti
Feb 1 at 15:54












$begingroup$
@AdrianoRepetti I deleted my prev comment to answer your question more clear, I used DI because I think it makes app more flexible, extending the application becomes easier and it helps to enable loose coupling.
$endgroup$
– Mehdi Dehghani
Feb 1 at 16:18




$begingroup$
@AdrianoRepetti I deleted my prev comment to answer your question more clear, I used DI because I think it makes app more flexible, extending the application becomes easier and it helps to enable loose coupling.
$endgroup$
– Mehdi Dehghani
Feb 1 at 16:18












$begingroup$
Let me go back one step. Did you EVER needed to switch the repository you used in your real world applications? Did you EVER needed to do it at run-time/from configuration? What has been the cost of that without DI? Now let's imagine to drop DI from all your code and see what you gain. "loose coupling, flexible, extending...easier" sound like buzzwords if you do not have a SPECIFIC use-case in mind. In which case DI (if you do not want tests (!!!)) is making your life easier?
$endgroup$
– Adriano Repetti
Feb 1 at 16:21






$begingroup$
Let me go back one step. Did you EVER needed to switch the repository you used in your real world applications? Did you EVER needed to do it at run-time/from configuration? What has been the cost of that without DI? Now let's imagine to drop DI from all your code and see what you gain. "loose coupling, flexible, extending...easier" sound like buzzwords if you do not have a SPECIFIC use-case in mind. In which case DI (if you do not want tests (!!!)) is making your life easier?
$endgroup$
– Adriano Repetti
Feb 1 at 16:21














$begingroup$
Your right, DI maybe is not necessary for me, because I don't have any plan to switch the repository or any run-time configuration. I just not sure about extending, but overall I think I have not real use-case here.
$endgroup$
– Mehdi Dehghani
Feb 1 at 16:26




$begingroup$
Your right, DI maybe is not necessary for me, because I don't have any plan to switch the repository or any run-time configuration. I just not sure about extending, but overall I think I have not real use-case here.
$endgroup$
– Mehdi Dehghani
Feb 1 at 16:26












$begingroup$
1) no, why it should be? 2) yes, assuming that you WILL use DI to write your unit testing in future. 3) If you're using an ORM then I'll drop it all together. A method which resposibility is just using, open and close is pretty anemic (IMHO)
$endgroup$
– Adriano Repetti
Feb 1 at 16:27




$begingroup$
1) no, why it should be? 2) yes, assuming that you WILL use DI to write your unit testing in future. 3) If you're using an ORM then I'll drop it all together. A method which resposibility is just using, open and close is pretty anemic (IMHO)
$endgroup$
– Adriano Repetti
Feb 1 at 16:27










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212702%2fusing-dapper-in-asp-net-core-with-dependency-injection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212702%2fusing-dapper-in-asp-net-core-with-dependency-injection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...