有关接口和 DI 的问题吗?

发布于 2024-10-20 03:02:57 字数 636 浏览 8 评论 0原文

我在 MVC 应用程序中使用 Service/Repository/EF/POCO 模式,并且有几个关于接口的问题。

1)我应该为每个服务创建一个接口吗? 2)我应该为每个存储库创建一个接口吗?

或者,我应该每层都有一个通用接口(IService(Of T)、IRepository(Of T))。

我不明白的是,在控制器中如何说,它在其构造函数中采用 IService(Of Category) 接口,如何实现具体类中的方法?

Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Private _Service As IService(Of Category)

    Public Sub New(ByVal Service As IService(Of Category))
        _Service = Service

    End Sub

    Function Index() As ActionResult

        Return View()
    End Function

End Class

_Service 没有我的具体 CategoryService 类的方法?

有道理吗?

I am using the Service/Repository/EF/POCO pattern in a MVC app, and had a couple questions about the Interfaces.

1) Should I make an Interface per Service?
2) Should I make an Interface per Repository?

Or, should I have a generic interface per layer (IService(Of T), IRepository(Of T)).

What I dont understand is how in the controller say, it takes a IService(Of Category) interface in it's constructor, how do I implements the methods in the concrete class?

Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Private _Service As IService(Of Category)

    Public Sub New(ByVal Service As IService(Of Category))
        _Service = Service

    End Sub

    Function Index() As ActionResult

        Return View()
    End Function

End Class

The _Service does not have the methods of my concrete CategoryService class?

Make any sense?

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

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

发布评论

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

评论(2

决绝 2024-10-27 03:02:57

使用具体的接口进行服务。如果您的服务可以通过通用接口来描述,那么您很可能根本不需要它们。通用接口通常用于存储库,因为存储库通常提供相同的核心方法。

Use concrete interface for service. If your services can be described by generic interface you most probably don't need them at all. Generic interface is often used for repositories because repositories usually offer same core methods.

待天淡蓝洁白时 2024-10-27 03:02:57

对于我自己来说,我使用强类型的通用会话对象,这个类位于我的域项目中,包含我所有的域类。您应该看看这篇文章: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx 使用代码优先方法。

希望有帮助!

这是我的 Session 类的代码:

public class EFSession : ISession
{
    DbContext _context;

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


    public void CommitChanges()
    {
        _context.SaveChanges();
    }

    public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
    {

        var query = All<T>().Where(expression);
        foreach (var item in query)
        {
            Delete(item);
        }
    }

    public void Delete<T>(T item) where T : class, new()
    {
        _context.Set<T>().Remove(item);
    }

    public void DeleteAll<T>() where T : class, new()
    {
        var query = All<T>();
        foreach (var item in query)
        {
            Delete(item);
        }
    }

    public void Dispose()
    {
        _context.Dispose();
    }

    public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
    {
        return All<T>().FirstOrDefault(expression);
    }

    public IQueryable<T> All<T>() where T : class, new()
    {
        return _context.Set<T>().AsQueryable<T>();
    }

    public void Add<T>(T item) where T : class, new()
    {
        _context.Set<T>().Add(item);
    }

    public void Add<T>(IEnumerable<T> items) where T : class, new()
    {
        foreach (var item in items)
        {
            Add(item);
        }
    }

    /// <summary>
    /// Do not use this since we use EF4, just call CommitChanges() it does not do anything
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="item"></param>
    public void Update<T>(T item) where T : class, new()
    {
        //nothing needed here
    }
}

For myself, I use a generic session object that is strongly type, this class is in my domain project witch contains all my domain classes. You should take a look at this post : http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx using Code-First approach.

Hope it helps!

Here my code for my Session class :

public class EFSession : ISession
{
    DbContext _context;

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


    public void CommitChanges()
    {
        _context.SaveChanges();
    }

    public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
    {

        var query = All<T>().Where(expression);
        foreach (var item in query)
        {
            Delete(item);
        }
    }

    public void Delete<T>(T item) where T : class, new()
    {
        _context.Set<T>().Remove(item);
    }

    public void DeleteAll<T>() where T : class, new()
    {
        var query = All<T>();
        foreach (var item in query)
        {
            Delete(item);
        }
    }

    public void Dispose()
    {
        _context.Dispose();
    }

    public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
    {
        return All<T>().FirstOrDefault(expression);
    }

    public IQueryable<T> All<T>() where T : class, new()
    {
        return _context.Set<T>().AsQueryable<T>();
    }

    public void Add<T>(T item) where T : class, new()
    {
        _context.Set<T>().Add(item);
    }

    public void Add<T>(IEnumerable<T> items) where T : class, new()
    {
        foreach (var item in items)
        {
            Add(item);
        }
    }

    /// <summary>
    /// Do not use this since we use EF4, just call CommitChanges() it does not do anything
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="item"></param>
    public void Update<T>(T item) where T : class, new()
    {
        //nothing needed here
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文