Entity Framework Mapping - Area Code

Asked

Viewed 104 times

0

I’m starting to work on a DDD project. I happen to be having a lot of doubts, and as much as I research, it gets more and more confused.

I created this classe:

public class Teste {
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Sobrenome { get; set; }
}

And this interface

public interface ITesteRepository
{
    void Add(Teste queryResult);
    void Update(Teste queryResult);
    void Delete(Teste queryResult);
    IQueryable<Teste> GetByTesteId(int  testeId);
}

I know I need to create the mapping now, but I’m not sure how to start, I’ve already researched the Internet, if anyone has any explanation to help me, making it easier for me to understand, or some example I can follow.

Edit

It would be something like:

public class ClienteConfiguration : EntityTypeConfiguration<Cliente>
{
    public ClienteConfiguration()
    {
        HasKey(c => c.ClienteId);

        Property(c => c.Nome)
            .IsRequired()
            .HasMaxLength(150);

        Property(c => c.Sobrenome)
            .IsRequired()
            .HasMaxLength(150);

        Property(c => c.Email)
            .IsRequired();

    }
}

But I wanted to know how to do it, what the logic is.

  • @Costamilam watched the two videos, and I could not get my doubt, I edited the question.

  • @Costamilam ah yes, it is because I am still doing tests, and trying to understand rs, but still it was very valuable the videos. Thank you.

  • @Renan I created the classe and the interface of repository, but this part of mapping I have doubts. For example. in core, I create the model, then add in context, and then give a add-migration Model and then a update-database it creates the mapping. How the project is DDD do not know how to proceed. I am starting now, so I am still with enough doubts.

  • Using EF or EF Core?

  • Now EF, but only used in Core, as I reported above. I think that’s why you’re confusing me.

1 answer

0

I think this example might help you, Mariana.

 public class TestDriveRepository : GenericRepository<TestDrive>, ITestDriveRepository
{
    public TestDriveRepository(DataContext context)
        : base(context)
    {
    }

    public IEnumerable<TestDrive> GetByFilter(DateTime? initialOpeningDate = null, DateTime? finalOpeningDate = null, bool onlyEnabled = false, bool onlyExpired = false)
    {
        var query = from td in this.Table.AsNoTracking()
                    join u in base.context.Users on td.UserId equals u.Id
                    where (!onlyEnabled) || (u.Enable)
                    select td;

        if (initialOpeningDate.HasValue)
            query = query.Where(td => DbFunctions.TruncateTime(td.OpeningDate) >= DbFunctions.TruncateTime(initialOpeningDate.Value));

        if (finalOpeningDate.HasValue)
            query = query.Where(td => DbFunctions.TruncateTime(td.OpeningDate) <= DbFunctions.TruncateTime(finalOpeningDate.Value));

        if (onlyExpired)
            query = query.Where(td => DbFunctions.TruncateTime(td.ExpireDate) < DateTime.Now);

        query = query.OrderByDescending(x => x.OpeningDate);
        return query.ToList();
    }
}

Interface:

public interface ITestDriveRepository
{
    TestDrive GetById(object id);

    void Insert(TestDrive entity);

    void Update(TestDrive entityToUpdate);


    IEnumerable<TestDrive> GetByFilter(DateTime? initialOpeningDate = null, DateTime? finalOpeningDate = null, bool onlyEnabled = false, bool onlyExpired = false);

    TestDrive GetTestDriveById(long userId);

    TestDrive GetHashForValidation(string token);

    TestDrive GetTestDriveByToken(string token);

    DbContextTransaction BeginTransaction();
}
  • Vinicius, the problem that I don’t know how to do this in my project. So far I’ve already downloaded some projects with DDD to help me. For example Mapping I can generate automatic, or I need to create it ?

  • 1

    I needed to create them all. I mean, in real design. In this print, it shows the way I organized the project in DDD: http://prntscr.com/nfwx3v Obs: in the 'Application' layer is the service layer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.