如何创建可以使用任何EF Core dbContext的类库

发布于 2025-01-29 18:48:38 字数 938 浏览 5 评论 0原文

我正在尝试创建一个可以消耗DBContext的类库,该库将能够查询具有常见模式的实体。 (这是一个数据库第一个项目)

我在班级库项目中有以下内容:

    public interface ITranslate
    {
        public long Id
        public long ParentId
        public long LangId
    }

在一个单独的项目中,我将有以下内容:

    public partial class SectionTranslation : ITranslate
    {
         public string SectionName { get; set; }
    {

在另一个项目中,我可能会拥有以下

    public partial class TemplateTranslation : ITranslate
    {
         public string TemplateName { get; set; }
    {

我想做的就是让以下合同但是,现实,我不确定如何处理这个。我希望能够返回与GetBestTranslation方法中常见查询相匹配的记录,我想返回截面转换或templateTrateTranslation实体,具体取决于项目在哪个项目中消耗了库以及Savetranslation方法,保存翻译记录。

    public interface ITranslateManager
    {
        Task<T> GetBestTranslationAsync(long langId);
        Task CreateTranslationAsync<T>(long langId)
    }

谢谢大家的帮助

I am trying to create a class library that can consume a DbContext that will be able to query entities that have a common schema. (this is a DB first project)

I have the following in my class library project:

    public interface ITranslate
    {
        public long Id
        public long ParentId
        public long LangId
    }

In a seperate project, I would have the following:

    public partial class SectionTranslation : ITranslate
    {
         public string SectionName { get; set; }
    {

And in another project I might have the following

    public partial class TemplateTranslation : ITranslate
    {
         public string TemplateName { get; set; }
    {

What I would like to do is make the following contract a reality, however, I am not sure how to approach this one. I would want to be able to return a record that would match a common query in the GetBestTranslation method, I would want to return a SectionTranslation or TemplateTranslation Entity depending on which project is consuming the library and in the SaveTranslation method, save a translation record.

    public interface ITranslateManager
    {
        Task<T> GetBestTranslationAsync(long langId);
        Task CreateTranslationAsync<T>(long langId)
    }

Thank you all in advance for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最终幸福 2025-02-05 18:48:38

您可以在方法上使用一些条件。

public class TranslateManager : ITranslateManager
{
    private readonly ContextDb _contextDb;

    public TranslateManager(ContextDb contextDb)
    {
        _contextDb = contextDb;
    }

    public async Task CreateTranslationAsync<T>(long langId) where T : class, ITranslate
    {
        // ToDo
    }

    public async Task<ITranslate> GetBestTranslationAsync<T>(long langId) where T : class, ITranslate
    {
        var dbSet = _contextDb.GetDbSet<T>();
        return await dbSet.Where(x => x.LangId == langId).FirstOrDefaultAsync();
    }
}

public interface ITranslate
{
    public long Id { get; set; }
    public long ParentId { get; set; }
    public long LangId { get; set; }
}

public partial class SectionTranslation : ITranslate
{
    public long Id { get; set; }
    public long ParentId { get; set; }
    public long LangId { get; set; }
    public string SectionName { get; set; }
}

public partial class TemplateTranslation : ITranslate
{
    public long Id { get; set; }
    public long ParentId { get; set; }
    public long LangId { get; set; }
    public string TemplateName { get; set; }
}

public interface ITranslateManager
{
    Task<T> GetBestTranslationAsync<T>(long langId) where T : class, ITranslate;
    Task CreateTranslationAsync<T>(long langId) where T : class, ITranslate;
}

You can use a where condition on Method.

public class TranslateManager : ITranslateManager
{
    private readonly ContextDb _contextDb;

    public TranslateManager(ContextDb contextDb)
    {
        _contextDb = contextDb;
    }

    public async Task CreateTranslationAsync<T>(long langId) where T : class, ITranslate
    {
        // ToDo
    }

    public async Task<ITranslate> GetBestTranslationAsync<T>(long langId) where T : class, ITranslate
    {
        var dbSet = _contextDb.GetDbSet<T>();
        return await dbSet.Where(x => x.LangId == langId).FirstOrDefaultAsync();
    }
}

public interface ITranslate
{
    public long Id { get; set; }
    public long ParentId { get; set; }
    public long LangId { get; set; }
}

public partial class SectionTranslation : ITranslate
{
    public long Id { get; set; }
    public long ParentId { get; set; }
    public long LangId { get; set; }
    public string SectionName { get; set; }
}

public partial class TemplateTranslation : ITranslate
{
    public long Id { get; set; }
    public long ParentId { get; set; }
    public long LangId { get; set; }
    public string TemplateName { get; set; }
}

public interface ITranslateManager
{
    Task<T> GetBestTranslationAsync<T>(long langId) where T : class, ITranslate;
    Task CreateTranslationAsync<T>(long langId) where T : class, ITranslate;
}
绝對不後悔。 2025-02-05 18:48:38

您可以通过这样做只需访问DBContext:

protected readonly PostgisDbContext Context;

protected Service(PostgisDbContext context) : base(context)
{
    Context = context;
}

使用此上下文非常容易。您可以通过将传递到通用函数的type与现有数据集表type匹配到上下文中的数据集。不要忘记添加wery:t:class这将告诉该功能, type t是由您提供的类分配的在通话中。

public Task<T> GetAsync(long langId) where : T : class
{
    Context.Set<T>().Where(predicate).FirstOrDefault();
}

如果您需要代码更具动态性,则可以将谓词添加为其参数。这将使您无需创建新功能即可使用其他查询。
例如x =&gt; x == true

public Task<T> Get(Func<T, bool> predicate) => Context.Set<T>().Where(predicate).FirstOrDefault();

要调用该方法,您必须告诉函数通用类型是什么。您这样做:

_translationService.GetAsync<MyClass>(parameter);

You can simply access your dbContext by doing this:

protected readonly PostgisDbContext Context;

protected Service(PostgisDbContext context) : base(context)
{
    Context = context;
}

Using this context is pretty easy. You can request a Dataset in your context by matching the Type passed into the generic function with an existing Dataset table Type. Don't forget to add where : T : class This will tell the function that the generic Type T is assigned by the class that you provide in the call.

public Task<T> GetAsync(long langId) where : T : class
{
    Context.Set<T>().Where(predicate).FirstOrDefault();
}

If you need the code to be more dynamic, you can add a predicate as its parameter. This will allow you to use query different queries without having to create a new function.
For example x => x == true.

public Task<T> Get(Func<T, bool> predicate) => Context.Set<T>().Where(predicate).FirstOrDefault();

To call the method you have to tell the function what the generic Type is. You do this like this:

_translationService.GetAsync<MyClass>(parameter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文