Unable to get data from DA layer. What to do?

By splitting my C # application in layers, I solved the circular dependency problem between layers as follows:

using System;
using System.Collections.Generic;
using System.Text;

using SolvingCircularDependency.Common;
using SolvingCircularDependency.DA;

namespace SolvingCircularDependency.BO
{
    public class MyClass : IPersistent
    {
        private string _message;
        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }

        public bool Save()
        {
             return MyClassDA.Save(this);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace SolvingCircularDependency.Common
{
    public interface IPersistent
    {        
        bool Save();
        string Message { get;}
    }
}

using System;
using System.Collections.Generic;
using System.Text;

using SolvingCircularDependency.Common;

namespace SolvingCircularDependency.DA
{
    public class MyClassDA
    {
        public static bool Save(IPersistent obj)
        {
            Console.WriteLine(obj.Message);

            return true;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

using SolvingCircularDependency.BO;

namespace SolvingCircularDependency.UI
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass myobj = new MyClass();
            myobj.Message = "Goodbye Circular Dependency!";
            myobj.Save();

            Console.ReadLine();
        }
    }
}

      

alt text

Please take a look at the MyClassDA class at the DA level and at the assembly itself.

How the MyDA.Get () method returns objects of type MyClass when the data access layer is unaware of the type MyClass.

If this project is ineffective, how can it be changed or changed?

-1


source to share


2 answers


As far as I understand, you have a bi-directional relationship between your DA and Business tier. To solve this problem, I suggest you have 3 layers instead of two. I mean you have to have a model layer that just models the DB objects, then you can get the model classes in your business layer and add other behaviors like the Save method.

This is what I mean:



//Model Layer
public class UserModel
{
public virtual string Firstname{get;set;}
}
//DataAccess Layer
public class UserDao
{
List<UserModel> GetAll();
}
//BusinessLayer
public class UserDomainModel:UserModel
{
public UserDomainModel(UserModel user,UserDao dao)
{
_user=user;
_dao=dao;
}
public override string FirstName
{
get
{
return _user.FirstName;
}
set
{
_user.FirstName=value;
}

public void Save()
{
_dao.Save(_user);
}
}
}

      

I am using a decorator to combine User and UserDao as a domain model object.

+1


source


One of the reasons people make Persistent Ignorant Objects (POCOs) is to avoid this scenario. There is no easy way for the data access layer to have a reference to a class that it doesn't know about - much better if the class doesn't know about data access.

The only way you can really do this is to implement Get () for the user instead of UserDA. You can do something like this:



public class User {
  IGetFromPresistance<User> _userFetcher;
  public static IList<User> GetMatching(Specification<User> spec) {
    var values = _userFetcher.Find(spec);  //Returns a DataRow or IDictionary<string, object>
    return new User() {
      PhoneNumber = new PhoneNumber(values["phone"].ToString()),
      Name = values["name"].ToString(),
    };
  }
}

      

+1


source







All Articles