Empty object mode, Object Mode

Source: Internet
Author: User

Empty object mode, Object Mode

When developing data at the business layer, I always worry that the data layer will return null to the object instance.

Therefore, every time I use the object instance returned by the data layer, I have to determine whether it is null.

  UserRepository respository = new UserRepository();  var user = respository.GetById("008");  if (user!=null)  {      user.SayHello();  }

Although this avoids exceptions caused by null values, it adds a lot of work to the client code, and once I forget to judge somewhere, my code will have a null exception; to solve this problem, we have introduced the empty object mode, killing empty objects at the data source.

    public interface IUser    {        void SayHello();    }
    public class User : IUser    {        public string Id { get; private set; }        public User(string id)        {            Id = id;        }        public void SayHello()        {            Console.WriteLine("{0}:Hello", Id);        }    }
Public class NullUser: IUser {public void SayHello () {// for an empty object instance, no processing is required for any method }}
Public class UserRepository {private ICollection <User> users; public UserRepository () {users = new List <User> () {new User ("001 "), new User ("002"), new User ("003"), new User ("004"), new User ("005 ")};} public IUser GetById (string userId) {IUser user = users. singleOrDefault (s => s. id = userId); if (user = null) // check whether the User's instance is null. if yes, a special Iuser {user = new NullUser ();} is returned ();} return user ;}}

In the code above, the data layer directly determines whether the user instance is null. If it is null, an object instance is returned for processing the null value.

 static void Main(string[] args) {     UserRepository respository = new UserRepository();     var user = respository.GetById("008");     user.SayHello();     user = respository.GetById("003");     user.SayHello();     Console.ReadLine(); }

Now, Mom no longer has to worry about calling the object instance of the data layer to report null references. Of course, this method is not limited

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.