Click here to Skip to main content
15,891,607 members
Home / Discussions / C#
   

C#

 
GeneralRe: Updating properties in a IEnumerable Pin
BillWoodruff25-Dec-22 5:04
professionalBillWoodruff25-Dec-22 5:04 
AnswerRe: Updating properties in a IEnumerable Pin
Mycroft Holmes24-Dec-22 11:09
professionalMycroft Holmes24-Dec-22 11:09 
Generalc# Pin
Member 1587460624-Dec-22 3:18
Member 1587460624-Dec-22 3:18 
GeneralRe: c# Pin
OriginalGriff24-Dec-22 4:06
mveOriginalGriff24-Dec-22 4:06 
GeneralRe: c# Pin
BillWoodruff24-Dec-22 18:39
professionalBillWoodruff24-Dec-22 18:39 
GeneralRe: c# Pin
OriginalGriff24-Dec-22 19:44
mveOriginalGriff24-Dec-22 19:44 
GeneralRe: c# Pin
RedDk25-Dec-22 6:30
RedDk25-Dec-22 6:30 
GeneralRe: c# Pin
OriginalGriff25-Dec-22 21:44
mveOriginalGriff25-Dec-22 21:44 
GeneralRe: c# Pin
Nitin S26-Dec-22 18:10
professionalNitin S26-Dec-22 18:10 
GeneralRe: c# Pin
RedDk27-Dec-22 8:54
RedDk27-Dec-22 8:54 
Questionconvert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
Sakhalean23-Dec-22 23:48
Sakhalean23-Dec-22 23:48 
AnswerRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
OriginalGriff24-Dec-22 0:29
mveOriginalGriff24-Dec-22 0:29 
GeneralRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
Sakhalean25-Dec-22 4:52
Sakhalean25-Dec-22 4:52 
AnswerRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
BillWoodruff24-Dec-22 18:37
professionalBillWoodruff24-Dec-22 18:37 
GeneralRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
Sakhalean24-Dec-22 22:46
Sakhalean24-Dec-22 22:46 
GeneralRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
trønderen24-Dec-22 23:13
trønderen24-Dec-22 23:13 
GeneralRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
BillWoodruff25-Dec-22 4:15
professionalBillWoodruff25-Dec-22 4:15 
GeneralRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
Sakhalean25-Dec-22 4:50
Sakhalean25-Dec-22 4:50 
GeneralRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
BillWoodruff25-Dec-22 5:13
professionalBillWoodruff25-Dec-22 5:13 
GeneralRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
trønderen25-Dec-22 11:17
trønderen25-Dec-22 11:17 
GeneralRe: convert double with decimal places to any base in C# example : 19.879 to any base or -19.345 to any base Pin
Sakhalean25-Dec-22 14:06
Sakhalean25-Dec-22 14:06 
QuestionEF Core 6 Repository Pattern Pin
Kevin Marois23-Dec-22 9:11
professionalKevin Marois23-Dec-22 9:11 
I have an old Linq-To-Sql DAL that I'm converting to a EF Core 6. I'm using the Repostory pattern.

I've looked at this article and others like it, and the all seem to do it the same way...

Interface
public interface IRepository<T> where T : class
{
    void Add(T entity);
    void AddRange(IEnumerable<T> entities);
    void Delete(T entity);
    void DeleteRange(IEnumerable<T> entities);
    T Get(int id);
    IEnumerable<T> Get(Expression<Func<T, bool>> expression);
    IEnumerable<T> GetAll();
}
Base Repository
public class BaseRepository<T> : IRepository<T> where T : class
{
    protected readonly DbContext _context;

    public BaseRepository(DbContext context)
    {
        _context = context;
    }

    // Interface methods here
}
Customer Repository
public class CustomerRepository : BaseRepository<CustomerEntity>, ICustomerRepository
{
    public CustomerRepository(DbContext context)
        :base(context)
    {
    }

    public bool IsCustomerActive(int id)
    {
        return true;
    }
}
Usage
public class CustomerController : ControllerBase
{
    public bool CheckIfCustomerIsActive(int id)
    {
        var connString = "";

        var dc = new SqlDataContext(connString);

        ICustomerRepository repository = new CustomerRepository(dc);

        return repository.IsCustomerActive(id);
    }
}
Issues I See
Everything about this seems clean and maintainable, except
1. The Connnection String is in the controller. I see this in a lot of examples.
2. By design, the controller must create and pass the data context to the repos.

It seems to me that these 2 issues create a tight coupling between the API and the DAL.Why would a controller ever need to know the conn string OR DBContext? Those are both specific to the DAL.

Why not do this instead?
public class BaseRepository<T> : IRepository<T> where T : class
{
    protected readonly DbContext _context;

    public BaseRepository()
    {
        // The base repo is the only place that gets the connection string
        var connString = Properties.Settings.Default.ConnectionString;

        // The base repo is the only place that knows about the db context
        _context = new SqlDataContext(connString);
    }

    // Interface methods here
}
Doing it this way, the controllers could use DI to get the repos they need, and the controllers never need to know about the connection string or the DBContext. I could swap out the entire data layer without ever opening a controller.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.


modified 23-Dec-22 15:29pm.

AnswerRe: EF Core 6 Repository Pattern Pin
Richard Deeming5-Jan-23 0:02
mveRichard Deeming5-Jan-23 0:02 
GeneralRe: EF Core 6 Repository Pattern Pin
Kevin Marois5-Jan-23 6:46
professionalKevin Marois5-Jan-23 6:46 
GeneralRe: EF Core 6 Repository Pattern Pin
Richard Deeming5-Jan-23 22:41
mveRichard Deeming5-Jan-23 22:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.