What to return from the DAL to BLL

前端 未结 7 2012
不思量自难忘°
不思量自难忘° 2021-02-02 01:48

I currently have an application which consists of: User Interface (web page) BLL (Manager & Domain Objects) DAL (DataAccess class for each of my Domain Objects).

I u

7条回答
  •  忘了有多久
    2021-02-02 02:32

    I would probably use ExecuteReader to create an object in code from the database. The reason for this is that the datatable has more overhead than a reader, because it has more functionality (and was probably created by a reader). Since you aren't doing updates/deletes using the datatable, you don't need the overhead.

    That being said, I would make a static helper method in the BookManager class.

    internal static IBook BookFromReader(IDataReader reader)
    {
         Book B = new Book();
         B.Prop = reader.GetString(0);
         B.Rinse = reader.Repeat();
         return B;
    }
    

    The reason for this is because the reason you have an interface is because you might want to change the implementation. You may eventuallu have INovel : IBook, IReference : IBook etc and then you'll want to have an abstract factory implementation in your data layer.

    public static IBook GetBook(int id)
    {
        // SqlCommand Command = new Command("SQL or sproc", ValidConnection);
    
        using(IDataReader DR = Command.ExecuteReader(id))
        {
            // checking omitted
            switch(DR.GetInt32(1))
            {
                case 0:
                     return BookManager.BookFromReader(DR);
                case 1:
                     return BookManager.NovelFromReader(DR);
                etc
            }
        }
    }
    

    Another benefit of the DAL here is that you can cache lookups. You can have a Dictionary that holds books you've looked up, to reduce extra db calls on objects you've already returned. When an update takes place, you remove the cached entity... That's another post though.

    If you're using multiple assemblies, interfaces and helper methods will need to reside in a neutral (non-dependent) assembly. Right now in the blog-o-sphere, there is movement towards less assemblies, which means less dependencies, etc.

    Here is a link from a blog I read on this topic: http://codebetter.com/blogs/patricksmacchia/archive/2008/12/08/advices-on-partitioning-code-through-net-assemblies.aspx

    Ultimately, I think the answer is that the data layer returns an instance of your interface to the business layer.

    Good luck :-)

提交回复
热议问题