Unmei
Unmei
CC#
Created by Unmei on 5/29/2024 in #help
Export Nuget Package (.targets and .nuspec): what am I doing wrong?
No description
1 replies
CC#
Created by Unmei on 2/1/2024 in #help
How to effectively dispose unmanaged resources?
i have some questions about disposing objects in .NET. I work with a .NET API that connects to a SQL Server database via Entity Framework. Basically, the hierarchy of calls are: Controller -> Scoped Service -> Scoped Repository -> DbContext. As we are increasing number of users of the API and our application isn't very optmized, i went into studying about disposing objects, specially unmanaged resources (those not "deletable" by GC right? How to know if a object is unmanaged btw?). I wrote some questions in the code snippet bellow. As I'm still new to this (never needed to write explicitly dispose methods before), I'm open to opinions and experiences you all had in your projects and investigations!
// UserService and BookService are scoped services. Are these unmanaged resources?
public class UserService : IDisposable
{
private IBookService _bookService;
private IUserRepository _userRepository;

public UserService(
IBookService bookService,
IUserRepository userRepository
) {
_bookService = bookService;
_userRepository = userRepository;
}

// ... methods

public void Dispose()
{
// is it effective to use Dispose like that?
// are _bookService and _userRepository unmanageable resources?
_bookService?.Dispose();
_userRepository?.Dispose();

// is it needed/effective to set those to null?
_bookService = null;
_userRepository = null;
}
}

// UserRepository is scoped
public class UserRepository : IUserRepository, IDisposable
{
private readonly DbContext _context;

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

// ... methods

public void Dispose()
{
// is it effective to use Dispose like that?
_context?.Dispose();

// is it needed/effective to set it to null?
_context = null;
}
}
// UserService and BookService are scoped services. Are these unmanaged resources?
public class UserService : IDisposable
{
private IBookService _bookService;
private IUserRepository _userRepository;

public UserService(
IBookService bookService,
IUserRepository userRepository
) {
_bookService = bookService;
_userRepository = userRepository;
}

// ... methods

public void Dispose()
{
// is it effective to use Dispose like that?
// are _bookService and _userRepository unmanageable resources?
_bookService?.Dispose();
_userRepository?.Dispose();

// is it needed/effective to set those to null?
_bookService = null;
_userRepository = null;
}
}

// UserRepository is scoped
public class UserRepository : IUserRepository, IDisposable
{
private readonly DbContext _context;

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

// ... methods

public void Dispose()
{
// is it effective to use Dispose like that?
_context?.Dispose();

// is it needed/effective to set it to null?
_context = null;
}
}
12 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
Hi all, this is my first post here. I want to check if it's possbile to chain Expression.Or operations to be able to use it in EF 6 (Or in a IEnumerable.Where() in general. Let me explain the context: - I have two entities called Orders and Users. Users can create multiple orders, and one Order is related to one single User. - Orders have a "CreatedDate" field (so that it is populated with DateTime.Now in the database) and also "CreatedBy" representing the username that created it. - Users have a field called "DateDisabled" nullable. When it is null, I can bring all his orders, but if it has a value, i will just bring orders dating from before he was disabled: order.DateCreated <= user.DateDisabled. To simplify:
public class OrderSummaryDpid {
public int Id { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
}

public class User {
public DateTime? DateDisabled {get; set;}
public string UserName {get;set;}
}
public class OrderSummaryDpid {
public int Id { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
}

public class User {
public DateTime? DateDisabled {get; set;}
public string UserName {get;set;}
}
a filter to bring orders from a single user would be :
var dateDisabled = user.DateDisabled.HasValue ? userDateDisabled.Value : DateTime.MaxValue; // overkill? yes

Expression<Func<OrderSummaryDpid, bool>> filter = order =>
order.CreatedDate <= dateDisabled &&
order.CreateBy == user.UserName;

var userOrders = _repository.Where(filter); // IQueryable<OrderSummaryDpid>
var dateDisabled = user.DateDisabled.HasValue ? userDateDisabled.Value : DateTime.MaxValue; // overkill? yes

Expression<Func<OrderSummaryDpid, bool>> filter = order =>
order.CreatedDate <= dateDisabled &&
order.CreateBy == user.UserName;

var userOrders = _repository.Where(filter); // IQueryable<OrderSummaryDpid>
since we can have many users, we would have many filter, one for each user: var manyUsersOrders = _repository.Where(filter1 || filter2 || filter3 || ... || filterN); Here comes the question: since it's not known how many users there are in the array, How can I create such an expression that chain Or operations like that dinamically? I tried to build Expressions and use Expression.Or to join them together, but It returns a BinaryExpression and I don't know exactly how to use it to build the final expression. Any help?
30 replies