Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm actually recreating the default architecture of my projects. In this post I want you not just help me,but think,tell and improve presented ways. This can be very useful challenge. This default architecture can be freely shared with all. So here is description: Basically MVC is our point of view. We follow OOP and layer programming concepts. Also we will use SQL Server and EF DB First. So here is what ive done till now: Ive created 4 layers in my solution:

  1. Domain: JUST domain classes
  2. DAL: responsible for accessing data, containing UOW and repositories and data access related validations.
  3. BL: unique responsible of business logic and unique boss of DAL.
  4. UI: which is not so important! (as even it maybe a console app!)

Ive implemented BL with generic funcs like getAll and getById and crud funcs.these funcs would call DAL generic funcs which is also ready. Now an important question is: is it right to implement DAL funcs as GENERIC? Please consider this scenario :

From UI we post a model to action and in action we call a BL Func (BL.insert(model)). The BL.Insert func would call something like DAL.Add(model) (notice BL will call DAL once and will tell it what it wants). now the DAL.Add func would have to insert 3 records into 3 different tables (model is passed from UI layer). I think this cant be achieved by DAL GENERIC funcs. So what is the correct and standard way of implementing Layers and relationships with noticing the Separation Of Concerns?

In my action i would have:

[HttpPost]
public ActionResult Insert()
{ 
    Bl.EntityBase.Article.Insert(new Article()); 
    return RedirectToAction("Index");
}

in BL I would have:

public void Insert(T obj)
{
    Da.Repository<T>().Insert(obj);
}

And in my DAL I have:

public virtual void Insert(T entity)
{
    DbEntityEntry dbEntityEntry = Db.Entry(entity);
    if (dbEntityEntry.State != EntityState.Detached)
    {
        dbEntityEntry.State = EntityState.Added;
    }
    else
    {
        Set.Add(entity);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
121 views
Welcome To Ask or Share your Answers For Others

1 Answer

IF the model which you are passing to DAL through your BL is a view-model then the logic will be in your DAL otherwise you should not pass a model which will perform 3 insert for 3 different tables.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...